1 tkinter

  TkInter是标准的Python GUI库。的Python与Tkinter的结合提供了一个快速和容易的方法来创建GUI应用程序。 Tkinter的提供了一个强大的面向对象的接口Tk的GUI工具包.

  1.1 使用Tkinter创建一个GUI应用程序是一件容易的事。所有你需要做的是执行以下步骤:

    1.1.1 导入Tkinter模块

    1.1.2 创建GUI应用程序的主窗口

    1.1.3 添加上述部件之一或更多的GUI应用程序

    1.1.4 进入主事件循环的由用户触发每个事件响应

 from tkinter import * # 导入模块

 root = Tk() # 创建主窗口对象root
root.title("测试窗口") # 设定主窗口的名称
root.geometry("400x400+400+100") # 设定主窗口的大小和位置
root.resizable(width = False, height = False) # 设定主窗口大小是否可改变 label01 = Label(root, text = "我是标签") # 添加标签对象在主窗口中
label01.pack() # 对label01对象进行布局 root.mainloop()
# 主循环 -->> 当所有对象都创建完成并且进行布局后就会进入主循环,等待事件触发

步骤实例

2 Label

  2.1 说明:标签类

  2.2 创建实例对象:label01 = Label(text = "warrior")

  2.3 属性

    text -->> 要显示的文本

    bg -->> 背景颜色

    bd -->> 外围边界宽度(相当于HTML中的marging属性)

    font -->> 字体(还可以添加大小)

    width -->> 宽度  宽度和高度不指定就会自动被撑开

    height -->> 高度

  2.4 简例

 from tkinter import *
root = Tk()
root.geometry("400x400+400+200") # 实例化标签对象
label01 = Label(text = "warrior", bg = "red", bd = "", font = ("楷体", 10), width = 20, height = 20)
label01.pack() # 位标签对象布局 root.mainloop()

Label实例

3 Frame

  3.1 说明:作为容器力气布局窗口

  3.2 创建实例对象:fra01 = Frame(root)

  3.3 简例

 from tkinter import *

 root = Tk()
root.title("测试")
root.geometry("400x400+400+200")
root.resizable(width = False, height = False) lab01 = Label(text = "我是标签", bg = "red", bd = "")
lab01.pack() fra = Frame(root) # 创建一个容器对象, 根对象是root fra_l = Frame(fra) # 创建一个容器对象,根对象是fra # 创建两个标签对象,这俩个标签对象都在fra_l这个容器中
lab_l1 = Label(fra_l, text = "左上", bg = "blue").pack(side = TOP)
lab_l2 = Label(fra_l, text = "左下", bg = "red").pack(side = TOP)
fra_l.pack(side = LEFT) fra_r = Frame(fra) # 创建一个容器对象,根对象是fra # 创建两个标签对象,这两个标签对象都在fra_r这个容器中
lab_r1 = Label(fra_r, text = '右上', bg = "red").pack(side = TOP)
lab_r2 = Label(fra_r, text = '右下', bg = "blue").pack(side = TOP)
fra_r.pack(side = RIGHT) fra.pack() root.mainloop()

Frame实例

4 Entry

  4.1 说明:创建单行文本框

  4.2 创建实例对象:e = Entry(root, textvariable = var)

  4.3 简例

 from tkinter import *

 root = Tk()
root.title("测试")
root.geometry("400x400+400+200")
root.resizable(width = False, height = False) var = Variable() # 创建一个变量对象
e = Entry(root, textvariable = var) # var变量对象被绑定在文本框的内容属性
# 改变变量对象var的值时文本框对象的内容就会相应改变 var.set("warrior") # 为变量对象var设定内容
e.pack() root.mainloop()

Entry

5 Text

  5.1 说明:向该空间内输入文本

  5.2 创建实例对象:t = Text(root)

  5.3 方法

    5.3.1  t.insert(1.0, "warrior\n")  t.insert(END, "fury")

    5.3.2 t.delete(1.0, 2.0)

  5.4 简例

 from tkinter import *

 root = Tk()
root.title("测试")
root.geometry("400x400+400+200")
root.resizable(width = False, height = False) t = Text(root) # 创建多行文本实例
t.insert(1.0, "warrior\n")
t.insert(END, "fury")
t.insert(END, "HELLO")
t.insert(1.0, "WORLD\n")
t.delete(1.0, 2.0)
t.pack() b = Button(root, text = "按钮")
b.pack() root.mainloop()

Text

6 Button

  6.1 说明:按钮

  6.2 实例化对象:b2 = Button(root, text = "点击退出", command = root.quit)  备注:root.quit 是退出实例 root 的意思

  6.3 简例

 from tkinter import *

 root = Tk()
root.title("测试")
root.geometry("400x400+400+200")
root.resizable(width = False, height = False) t = Text(root) # 创建多行文本实例
t.pack() def print_hello() :
t.insert(END, "重庆.足智\n") b1 = Button(root, text = "点击打印", command = print_hello)
b1.pack() b2 = Button(root, text = "点击退出", command = root.quit)
b2.pack() root.mainloop()

Button

7 Listbox

  7.1 说明:Listbox为列表框控件,它可以包含一个或多个文本项(text item),可以设置为单选或多选

  7.2 实例化对象:lb = Listbox(root)

  7.3 通过设定selectmode属性可以设定为单选或者多选(默认是单选)

  7.4 简例

 from tkinter import *

 root = Tk()
root.title("测试")
root.geometry("400x400+400+200")
root.resizable(width = False, height = False) lb = Listbox(root) # 创建一个列表实例对象
for item in ['python', 'java', 'php']: # 向列表对象插入数据
lb.insert(END, item) lb.pack() lb01 = Listbox(root, selectmode = MULTIPLE) # 多选模式
for item in ['java', 'c++', 'node.js'] :
lb01.insert(END, item)
lb01.pack()
root.mainloop()

Listbox

  7.5 问题:怎么绑定事件

    带更新......

8 滚动条

  待更新......

最新文章

  1. javascript中变量提升的理解
  2. 用R去做文本处理
  3. 下载旧版本的NDK
  4. mvc 3 Mvc 4 使用Forms 登录验证随笔一
  5. PRML读书笔记——Introduction
  6. CodeIgniter报错: You must use the "set" method to update an entry
  7. 服务器发布WebService返回DataTable
  8. 设置session的生命周期(php)
  9. hdoj 2085 核反应堆【水】
  10. C++primer拾遗(第一章:开始)
  11. MVC+EF 入门教程(四)
  12. Linux根文件系统介绍
  13. spring boot 自定义sql分页查询
  14. cmd代码:查端口占用,查进程号,杀进程
  15. NOIP2012 普及组 寻宝
  16. CTSC被虐记
  17. Linux 各类设置、配置、使用技巧参考,Linux使用集锦
  18. JS地毯式学习二
  19. Python之threading多线程
  20. Android_程序未处理异常的捕获与处理

热门文章

  1. 51nod 1449 贪心
  2. javascript queue 打字效果
  3. MySql简单分页存储过程
  4. Switching from Redhat Linux to Oracle Linux in about 5,000 easy steps
  5. mysql 视图、触发器、事物、存储过程、函数
  6. Eclipse插件开发_学习_01_Maven+Tycho 构建RCP程序
  7. 骨骼动画 cocos2d-x + cocoStudio <cocos2d-x : version 2.2.0>
  8. 【LeetCode】002 Add Two Numbers
  9. python函数之sorted与sort
  10. 多版本python管理miniconda(集成了virtualenv和pip功能)