1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. Tkinter的标准组件介绍
上文中我们已经罗列过tk支持的所有组件,本文将详细介绍这些组件和常用的组合方式https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.
控件名类型 | 意义 |
---|---|
Toplevel | 顶层框架 |
Frame | 框架 |
LabelFrame | 标签框架 |
Menu | 菜单栏 |
Menubutton | 菜单按钮 |
OptionMenu | 弹出菜单 |
Label | 标签 |
Button | 按钮 |
Entry | 输入框 |
Radiobutton | 单选框 |
Checkbutton | 复选框 |
Scale | 滑块 |
Text | 文本框 |
Canvas | 画板 |
Listbox | 列表框 |
Message | 信息栏 |
PanedWindow | 中分栏窗口 |
Scrollbar | 滚动条 |
Spinbox | 指定输入范围值的输入框 |
Combobox | 组合框,包含文本字段和一个包含可选值的下拉列表 |
Notebook | 标签页,形式参见chrome中的标签页 |
Progressbar | 进度条 |
Separator | 分离器,显示一个水平或垂直分隔条 |
Sizegrip | 控制TopLevel的窗口大小 |
Treeview | TreeView控件显示一个项目的树状分层集合 |
本文只是略微介绍标准库的组件,让大家对各个组件有个映像,对应接口还是要去http://effbothttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.org/tkinterbook/查看
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. 窗口的构建,Toplevel和常用操作
使用tkinter
中窗口是一个隐含的对象,最低一层实际上是toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.构建窗口就是实例化一个Tk
对象,一个Tk
对象,一个Tk
对象会实例化一个Toplevel
控件,也就是一个包含窗口的帧https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.
Tk
实例可以有如下常用的属性和方法
title(str) 窗口的标题
iconbitmap(path_str) 设置窗口的标题图标
geometry 定义窗口的长宽和出现位置,单位是像素,使用形如
"600x400+100+400"
的字符串设定表示长x宽+左上角x像素位+左上角y像素位wm_maxsize(width=800, height=600) 设置窗口最大长宽值
- wm_minsize(width=400, height=300) 设置窗口最小长宽值
mainloop() 主循环,执行则相当于启动窗口
attributes()设置window,这个参数的调用方式略奇葩,使用的是形如
attributes('-alpha',0https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.5)
的键值对式的格式,可选的参数包括
字段 | 平台 | 意义 |
---|---|---|
alpha | win,mac | 透明度,范围是0~1之间,0代表完全透明年 |
disabled | win | 如果设置,则禁用这个窗口 |
modified | mac | 标记窗口为已修改 |
titlepath | mac | 窗口代理图标的路径 |
toolwindow | win | 设置窗口样式为工具窗口 |
topmost | win | 设置窗口总是在其他窗口前 |
- configure() 设置
Toplevel
控件,可以设置的内容主要包括
字段 | 意义 |
---|---|
bd/borderwidth | 边框宽,默认是o |
menu | 设置菜单Menu 对象 |
relief | 边框样式,可选的FLAT,SUNKEN,RAISED,GROOVE,RIDGE 默认为FLAT |
background/bg | 背景色,可以是这里定义的字符串,也可以是#FFFFFF 这样的RGB |
colormap | 设置需要是Colormap 的实例, |
container | 设置需要是Container 的实例 |
cursor | 鼠标光标在其中的位置 |
height | 高度 |
width | 宽度 |
highlightbackground | 要高亮的背景色 |
highlightcolor | 要高亮的颜色 |
highlightthickness | 高亮的宽度 |
padx | 水平padding |
pady | 垂直padding |
takefocus | 指示用户可以使用tab键移动到这个部件 |
%%writefile src/first_toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py from tkinter import Tk win = Tk() winhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title("first window") winhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.iconbitmap(r"C:\Users\87\Documents\GitHub\my\TutorialForPython\ipynbs\人机交互\GUI\src\myredishttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.ico") winhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400") winhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.configure(background="Blue") winhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.attributes('-alpha',0https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.5) winhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.mainloop()
Overwriting src/first_toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
%exec_py src/first_toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
通常我们并不会直接实例化Tk()
来显示地创建窗口,而是通过继承的方式来构建应用https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. 多窗口
像gimp这种工具,一起动就是几个窗口各司其职,这种就需要利用toplevel构造多窗口了https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.
虽然是构造了多个窗口,但主循环还是只执行主窗口的https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.其他窗口则更多的是作为辅助https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.
%%writefile src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py from tkinter import Frame,Label,Button,Toplevel class Application(Frame): def __init__(self, master=None): Framehttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(self, master) #窗口大小位置 selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world1!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() class App2(Toplevel): def __init__(self, master=None): Toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(self, master) selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world2!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() if __name__ =="__main__": app1 = Application() # 设置窗口标题: app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title('Hello World1') app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y # 主消息循环: app2 = App2() app2https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title("helloword2") app2https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("200x200+0+0")#长x宽+x+y app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.mainloop()
Overwriting src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
%exec_py src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.2https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. 框架的构建和常用操作
tk中的帧有两种:
Frame
最基本的框架
LabelFrame
基本的框架的变体,它在它的子窗口周围绘制一个边框,并且它也可以显示标题
而ttk中也有Frame
对象https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.
框架可以设置的内容包括
字段 | 意义 |
---|---|
background/bg | 背景色 |
bd/borderwidth | 边框宽,默认是o |
colormap | 调色板,设置需要是Colormap的实例, |
container | 设置需要是Container的实例 |
cursor | 鼠标光标在其中的位置 |
highlightbackground | 要高亮的背景色 |
highlightcolor | 要高亮的颜色 |
highlightthickness | 高亮的宽度 |
padx | 水平padding |
pady | 垂直padding |
relief | 边框样式,可选的FLAT,SUNKEN,RAISED,GROOVE,RIDGE默认为FLAT |
takefocus | 指示用户可以使用tab键移动到这个部件 |
height | 高度 |
width | 宽度 |
Frame对象实例化需要传入一个master
参数,这master
可以是None
或者一个它的父组件(通常是一个Toplevel
对象)https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.如果是空它则会自己实例化一个Toplevel
作为其父组件https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.访问这个父组件可以使用framehttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.master
https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.
通常我们的app主体会继承一个Frame
并以其为基本容器构建应用https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.2https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. 框架的例子
我们来自己写个例子,体会下框架的用法https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.
%%writefile src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py #coding:utf-8 from tkinter import Frame,Label,Button class Application(Frame): def __init__(self, master=None): super()https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(master) #窗口大小位置 selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quitButton = Button(self, text='Quit',fg="red", command=selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quit) selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quitButtonhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() if __name__ =="__main__": app = Application() # 设置窗口标题: apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title('Hello World') apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y # 主消息循环: apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.mainloop()
Overwriting src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
%exec_py src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
运行后出现如图小对话框
%%writefile src/ttk_framehttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py #coding:utf-8 from tkinterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.ttk import Frame,Label,Button,Style style = Style() stylehttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.configure("RWhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.TLabel", foreground="red", background="white") class Application(Frame): def __init__(self, master=None): super()https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(master) #窗口大小位置 selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quitButton = Button(self,text='Quit',style="RWhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.TLabel",command=selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quit) selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quitButtonhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() if __name__ =="__main__": app = Application() # 设置窗口标题: apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title('Hello World') apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y # 主消息循环: apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.mainloop()
Overwriting src/first_toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
0
Overwriting src/first_toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
1
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.2https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.3https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. LabelFrame
labelFrame是frame的子类,区别在于这个的意思似乎更加接近框架
它提供的是一个有边界的框,并且这个框可以使用text
设置付名,其他额外的设置还有:
字段 | 意义 |
---|---|
background/bg | 背景色 |
bd/borderwidth | 边框宽,默认是o |
colormap | 调色板,设置需要是Colormap的实例, |
container | 设置需要是Container的实例 |
cursor | 鼠标光标在其中的位置 |
highlightbackground | 要高亮的背景色 |
highlightcolor | 要高亮的颜色 |
highlightthickness | 高亮的宽度 |
padx | 水平padding |
pady | 垂直padding |
relief | 边框样式,可选的FLAT,SUNKEN,RAISED,GROOVE,RIDGE默认为FLAT |
takefocus | 指示用户可以使用tab键移动到这个部件 |
height | 高度 |
width | 宽度 |
foreground/fg | 前景 |
font | 字体 |
labelanchor | 标签位置(e:右侧横置,en:右测偏上横置,es:右测偏下横置,n:,ne,nw,s,se,sw,w,wn,ws) |
labelwidget | 标签使用的组件,如果省略,框架使用Text 文本组件 |
text | 标签文本 |
labelanchor可选的参数为:
标志 | 说明 |
---|---|
e | 右侧 |
en | 右侧偏上 |
es | 右侧偏下 |
n | 顶部 |
ne | 顶部偏右 |
nw | 顶部偏左 |
s | 下侧 |
se | 下侧偏右 |
sw | 下侧偏左 |
w | 左侧 |
wn | 左侧偏上 |
ws | 左侧偏下 |
文本要竖排只能通过插入回车了
Overwriting src/first_toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
2
Overwriting src/first_toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
3
Overwriting src/first_toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
4
ttk中的labelframe
在ttk中labelframe的设置方式略有改变
Overwriting src/first_toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
5
Overwriting src/first_toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
6
Overwriting src/first_toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
7
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.3https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. 菜单 Menu
Menu 和其他的组件一样,第一个是 parent,这里通常都是TopLevel,同时还需要给win
对象设置menu
https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.
然后我们可以用add(type, **options)
或者addxxxx
接口为其添加组件https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.支持的菜单类型有:
- 菜单命令
commmand
- 子菜单
cascade
- 分割线
separator
- 复选菜单
checkbutton
- 单选菜单
radiobutton
options
则是对各个项进行的设置,包括
字段 | 意义 |
---|---|
activebackground | 激活状态的背景色 |
activeforeground | 激活状态的前景色 |
accelerator | 指定快捷键 |
background | 背景色 |
bitmap | 位图 |
columnbreak | 分栏符 |
command | 选中后执行的回调 |
font | 字体 |
foreground | 前景色 |
hidemargin | 隐藏边缘 |
image | 图片 |
indicatoron | 指示器开启 |
label | 标签 |
menu | 菜单对象 |
offvalue | off状态下的值 |
onvalue | on状态下的值 |
selectcolor | 选中后的颜色 |
selectimage | 选中后的图片 |
state | 状态 |
underline | 下划线 |
value | 值 |
variable | 变量 |
这些设置可以在添加是加上,也可以使用entryconfig(index, **options)
接口后设置https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.注意index
是最开始设置的label
另一个菜单相关的对象Menubutton
现在基本已经不再使用了,因为菜单相关的都可以使用menu实现https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.3https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. 菜单命令
add_commmand
方法来为它添加菜单项https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.
如果该菜单是顶层菜单,则添加的菜单项依次向右添加; 如果该菜单是顶层菜单的一个子菜单项(配合add_cascade
),则它添加的是子菜单的菜单项https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.
add_command
中的参数常用的有:
- label 属性,用来指定的是菜单项的名称
- command 属性用来指定被点击的时候调用的方法
- acceletor 属性指定的是快捷键
- underline 属性 是是否拥有下划线。
最后可以用窗口的menu
属性指定我们使用哪一个作为它的顶层菜单https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.
Overwriting src/first_toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
8
Overwriting src/first_toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
9
%exec_py src/first_toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
0
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.3https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.2https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. 子菜单
如果有子菜单,我们需则需要使用add_cascade
cascade 可以理解为“级联”,即它 的作用只是为了引出后面的菜单。
add_cascade属性:
- menu 属性,它指明了要把那个菜单级联到该菜单项上
- label 属性,用于指定该菜单项的名称。
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.3https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.3https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. 分割线
本身没有什么功能,只是为了美观,使用add_separator()
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.3https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.4https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. 单选菜单和复选菜单
单选菜单类型为radiobutton
可以使用接口add_radiobutton
; 复选菜单类型为checkbutton
可以使用接口add_checkbutton
这两个的添加方式和command
的添加方式一样,不同之处在于他们通常是作为顶层菜单的一个子菜单存在的https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.
%exec_py src/first_toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
1
%exec_py src/first_toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
2
%exec_py src/first_toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
3
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.3https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.5https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. 右键弹出菜单
一般弹出菜单是右键点击后出现的菜单,tk中的弹出菜单比较原始的,具体思路是这样:
- 我们先新建一个菜单,
- 然后向菜单项中添加各种功能,
- 最后我们监听鼠标右键消息,如果是鼠标 右键被单击,
- 此时可以根据需要判断下鼠标位置来确定是哪个弹出菜单被弹出,
- 然后使用 Menu 类的 pop 方法来弹出 菜单。
Menu 类里面有一个post方法,它接收两个参数,即 x 和 y 坐标,它会在相应的位置弹出菜单
例子: 一个菜单栏
%exec_py src/first_toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
4
%exec_py src/first_toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
5
%exec_py src/first_toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
6
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.4https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. OptionMenu
弹出菜单,通常用于点击某个对象后选择值
%exec_py src/first_toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
7
%exec_py src/first_toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
8
%exec_py src/first_toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
9
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.5https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. 其他组件用法
tkinter的控件上文中已有介绍,下面详细介绍各个组件的功能和设置方式https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.5https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. 标签 Label
标签可以定义的属性主要有:
字段 | 意义 |
---|---|
background/bg | 背景色 |
bd/borderwidth | 边框宽,默认是o |
colormap | 调色板,设置需要是Colormap的实例, |
container | 设置需要是Container的实例 |
cursor | 鼠标光标在其中的位置 |
highlightbackground | 要高亮的背景色 |
highlightcolor | 要高亮的颜色 |
highlightthickness | 高亮的宽度 |
padx | 水平padding |
pady | 垂直padding |
relief | 边框样式,可选的FLAT,SUNKEN,RAISED,GROOVE,RIDGE默认为FLAT |
takefocus | 指示用户可以使用tab键移动到这个部件 |
height | 高度 |
width | 宽度 |
foreground/fg | 前景 |
font | 字体 |
text | 标签文本 |
activebackground | 激活状态的背景色 |
activeforeground | 激活状态的前景色 |
anchor | 文本的对齐位置,可选的有N, NE, E, SE, S, SW, W, NW,CENTER |
image | 是否使用图标 |
bitmap | 按钮的图标 |
compound | 图片和文字的围绕方式,可选的有BOTTOM, LEFT, RIGHT, TOP,CENTER |
justify | 多行文本如何排版,可选项包括LEFT, RIGHT, CENTER |
state | 按钮状态,可选的有NORMAL, ACTIVE, DISABLED https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.默认NORMAL https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. |
textvariable | 使用Tkinter variable设置按钮文本 |
underline | 下划线 |
wraplength | 确定按钮的文本应何时被包装成多行 |
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.5https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.2https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. 按钮 Button
按钮算是最常用的控件之一了,它的属性主要有:
字段 | 意义 |
---|---|
background/bg | 背景色 |
bd/borderwidth | 边框宽,默认是o |
colormap | 调色板,设置需要是Colormap的实例, |
container | 设置需要是Container的实例 |
cursor | 鼠标光标在其中的位置 |
highlightbackground | 要高亮的背景色 |
highlightcolor | 要高亮的颜色 |
highlightthickness | 高亮的宽度 |
padx | 水平padding |
pady | 垂直padding |
relief | 边框样式,可选的FLAT,SUNKEN,RAISED,GROOVE,RIDGE默认为FLAT |
takefocus | 指示用户可以使用tab键移动到这个部件 |
height | 高度 |
width | 宽度 |
foreground/fg | 前景 |
font | 字体 |
text | 标签文本 |
activebackground | 激活状态的背景色 |
activeforeground | 激活状态的前景色 |
anchor | 文本的对齐位置,可选的有N, NE, E, SE, S, SW, W, NW,CENTER |
image | 是否使用图标 |
bitmap | 按钮的图标 |
command | 点击按钮的回调函数 |
compound | 图片和文字的围绕方式,可选的有BOTTOM, LEFT, RIGHT, TOP,CENTER |
disabledforeground | 无效前景色 |
justify | 多行文本如何排版,可选项包括LEFT, RIGHT, CENTER |
overrelief | 鼠标移动到上面时显示的内容 |
repeatdelay | 重复延迟 |
repeatinterval | 重复间隔 |
state | 按钮状态,可选的有NORMAL, ACTIVE, DISABLED https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.默认NORMAL https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. |
textvariable | 使用Tkinter variable设置按钮文本 |
underline | 下划线 |
wraplength | 确定按钮的文本应何时被包装成多行 |
最值得注意的是command
参数,注意它的回调函数没有参数
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.5https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.3https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. 输入框 Entry
字段 | 意义 |
---|---|
background/bg | 背景色 |
bd/borderwidth | 边框宽,默认是o |
colormap | 调色板,设置需要是Colormap的实例, |
container | 设置需要是Container的实例 |
cursor | 鼠标光标在其中的位置 |
highlightbackground | 要高亮的背景色 |
highlightcolor | 要高亮的颜色 |
highlightthickness | 高亮的宽度 |
padx | 水平padding |
pady | 垂直padding |
relief | 边框样式,可选的FLAT,SUNKEN,RAISED,GROOVE,RIDGE默认为FLAT |
takefocus | 指示用户可以使用tab键移动到这个部件 |
height | 高度 |
width | 宽度 |
foreground/fg | 前景 |
font | 字体 |
text | 标签文本 |
justify | 多行文本如何排版,可选项包括LEFT, RIGHT, CENTER |
state | 按钮状态,可选的有NORMAL, ACTIVE, DISABLED https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.默认NORMAL https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. |
textvariable | 使用Tkinter variable设置按钮文本 |
wraplength | 确定按钮的文本应何时被包装成多行 |
disabledbackground | 无效背景 |
disabledforeground | 无效前景 |
exportselection | 选中的文本是否自动保存到剪切板 |
insertbackground | 光标颜色 |
insertborderwidth | 光标边框宽度 |
insertofftime | 光标闪烁间隔 |
insertontime | 光标闪烁单次亮起的时间 |
insertwidth | 光标宽度 |
readonlybackground | 当状态为readonly 时的背景色 |
selectbackground | 选中区域的背景色 |
selectborderwidth | 选中区域的宽度 |
selectforeground | 选中区域的前景色 |
show | 常用在密码这种,覆盖显示文本 |
validate | 指定validate |
vcmd/validatecommand | validate时执行的回调 |
xscrollcommand | 水平滚动条 |
属性:
- get() 获取输入(返回一个str)
例:一个用户登录界面
%%writefile src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py from tkinter import Frame,Label,Button,Toplevel class Application(Frame): def __init__(self, master=None): Framehttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(self, master) #窗口大小位置 selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world1!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() class App2(Toplevel): def __init__(self, master=None): Toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(self, master) selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world2!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() if __name__ =="__main__": app1 = Application() # 设置窗口标题: app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title('Hello World1') app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y # 主消息循环: app2 = App2() app2https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title("helloword2") app2https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("200x200+0+0")#长x宽+x+y app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.mainloop()
0
%%writefile src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py from tkinter import Frame,Label,Button,Toplevel class Application(Frame): def __init__(self, master=None): Framehttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(self, master) #窗口大小位置 selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world1!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() class App2(Toplevel): def __init__(self, master=None): Toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(self, master) selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world2!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() if __name__ =="__main__": app1 = Application() # 设置窗口标题: app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title('Hello World1') app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y # 主消息循环: app2 = App2() app2https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title("helloword2") app2https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("200x200+0+0")#长x宽+x+y app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.mainloop()
1
%%writefile src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py from tkinter import Frame,Label,Button,Toplevel class Application(Frame): def __init__(self, master=None): Framehttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(self, master) #窗口大小位置 selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world1!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() class App2(Toplevel): def __init__(self, master=None): Toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(self, master) selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world2!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() if __name__ =="__main__": app1 = Application() # 设置窗口标题: app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title('Hello World1') app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y # 主消息循环: app2 = App2() app2https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title("helloword2") app2https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("200x200+0+0")#长x宽+x+y app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.mainloop()
2
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.5https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.4https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. 单选按钮 Radiobutton
一般是几个里面选一个用
可选的设置有:
字段 | 意义 |
---|---|
background/bg | 背景色 |
bd/borderwidth | 边框宽,默认是o |
colormap | 调色板,设置需要是Colormap的实例, |
container | 设置需要是Container的实例 |
cursor | 鼠标光标在其中的位置 |
highlightbackground | 要高亮的背景色 |
highlightcolor | 要高亮的颜色 |
highlightthickness | 高亮的宽度 |
padx | 水平padding |
pady | 垂直padding |
relief | 边框样式,可选的FLAT,SUNKEN,RAISED,GROOVE,RIDGE默认为FLAT |
takefocus | 指示用户可以使用tab键移动到这个部件 |
height | 高度 |
width | 宽度 |
foreground/fg | 前景 |
font | 字体 |
text | 标签文本 |
activebackground | 激活状态的背景色 |
activeforeground | 激活状态的前景色 |
anchor | 文本的对齐位置,可选的有N, NE, E, SE, S, SW, W, NW,CENTER |
image | 是否使用图标 |
bitmap | 按钮的图标 |
command | 点击按钮的回调函数 |
compound | 图片和文字的围绕方式,可选的有BOTTOM, LEFT, RIGHT, TOP,CENTER |
disabledforeground | 无效前景色 |
justify | 多行文本如何排版,可选项包括LEFT, RIGHT, CENTER |
overrelief | 鼠标移动到上面时显示的内容 |
repeatdelay | 重复延迟 |
repeatinterval | 重复间隔 |
state | 按钮状态,可选的有NORMAL, ACTIVE, DISABLED https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.默认NORMAL https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. |
textvariable | 使用Tkinter variable设置按钮文本 |
underline | 下划线 |
wraplength | 确定按钮的文本应何时被包装成多行 |
indicatoron | 是否使用标准的单选样式,默认为Ture,否则样式为SUNKEN |
offrelief | 按钮默认是否被按下 |
value | 选项对应的值 |
variable | 与此按钮关联的变量 |
直接看代码:
单选框和复选框往往会需要用到一个变量用于保存选择的内容,一般的变量无法起到效果,需要使用tk提供的变量类型:
BooleanVar, DoubleVar, IntVar, StringVar
要使用的话set
方法就是赋值,get
方法就是取值https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.
%%writefile src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py from tkinter import Frame,Label,Button,Toplevel class Application(Frame): def __init__(self, master=None): Framehttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(self, master) #窗口大小位置 selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world1!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() class App2(Toplevel): def __init__(self, master=None): Toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(self, master) selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world2!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() if __name__ =="__main__": app1 = Application() # 设置窗口标题: app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title('Hello World1') app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y # 主消息循环: app2 = App2() app2https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title("helloword2") app2https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("200x200+0+0")#长x宽+x+y app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.mainloop()
3
%%writefile src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py from tkinter import Frame,Label,Button,Toplevel class Application(Frame): def __init__(self, master=None): Framehttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(self, master) #窗口大小位置 selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world1!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() class App2(Toplevel): def __init__(self, master=None): Toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(self, master) selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world2!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() if __name__ =="__main__": app1 = Application() # 设置窗口标题: app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title('Hello World1') app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y # 主消息循环: app2 = App2() app2https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title("helloword2") app2https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("200x200+0+0")#长x宽+x+y app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.mainloop()
4
%%writefile src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py from tkinter import Frame,Label,Button,Toplevel class Application(Frame): def __init__(self, master=None): Framehttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(self, master) #窗口大小位置 selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world1!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() class App2(Toplevel): def __init__(self, master=None): Toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(self, master) selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world2!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() if __name__ =="__main__": app1 = Application() # 设置窗口标题: app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title('Hello World1') app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y # 主消息循环: app2 = App2() app2https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title("helloword2") app2https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("200x200+0+0")#长x宽+x+y app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.mainloop()
5
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.5https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.5https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. 复选框 Checkbutton
复选框通常是用来选择信息的时候的一种选择,它前面 有个小正方形的方块,如果选中则有一个对号,也可以再 次点击以取消该对号来取消选中。
字段 | 意义 |
---|---|
background/bg | 背景色 |
bd/borderwidth | 边框宽,默认是o |
colormap | 调色板,设置需要是Colormap的实例, |
container | 设置需要是Container的实例 |
cursor | 鼠标光标在其中的位置 |
highlightbackground | 要高亮的背景色 |
highlightcolor | 要高亮的颜色 |
highlightthickness | 高亮的宽度 |
padx | 水平padding |
pady | 垂直padding |
relief | 边框样式,可选的FLAT,SUNKEN,RAISED,GROOVE,RIDGE默认为FLAT |
takefocus | 指示用户可以使用tab键移动到这个部件 |
height | 高度 |
width | 宽度 |
foreground/fg | 前景 |
font | 字体 |
text | 标签文本 |
activebackground | 激活状态的背景色 |
activeforeground | 激活状态的前景色 |
anchor | 文本的对齐位置,可选的有N, NE, E, SE, S, SW, W, NW,CENTER |
image | 是否使用图标 |
bitmap | 按钮的图标 |
command | 点击按钮的回调函数 |
compound | 图片和文字的围绕方式,可选的有BOTTOM, LEFT, RIGHT, TOP,CENTER |
disabledforeground | 无效前景色 |
justify | 多行文本如何排版,可选项包括LEFT, RIGHT, CENTER |
overrelief | 鼠标移动到上面时显示的内容 |
repeatdelay | 重复延迟 |
repeatinterval | 重复间隔 |
state | 按钮状态,可选的有NORMAL, ACTIVE, DISABLED https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.默认NORMAL https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. |
textvariable | 使用Tkinter variable设置按钮文本 |
underline | 下划线 |
wraplength | 确定按钮的文本应何时被包装成多行 |
indicatoron | 是否使用标准的单选样式,默认为Ture,否则样式为SUNKEN |
offrelief | 按钮默认是否被按下 |
value | 选项对应的值 |
variable | 与此按钮关联的变量 |
indicatoron | 是否画上指示器,状态有SUNKEN 和RAISED |
offvalue | 未被选中的值 |
onvalue | 选中的值 |
看个例子:
%%writefile src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py from tkinter import Frame,Label,Button,Toplevel class Application(Frame): def __init__(self, master=None): Framehttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(self, master) #窗口大小位置 selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world1!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() class App2(Toplevel): def __init__(self, master=None): Toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(self, master) selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world2!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() if __name__ =="__main__": app1 = Application() # 设置窗口标题: app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title('Hello World1') app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y # 主消息循环: app2 = App2() app2https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title("helloword2") app2https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("200x200+0+0")#长x宽+x+y app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.mainloop()
6
%%writefile src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py from tkinter import Frame,Label,Button,Toplevel class Application(Frame): def __init__(self, master=None): Framehttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(self, master) #窗口大小位置 selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world1!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() class App2(Toplevel): def __init__(self, master=None): Toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(self, master) selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world2!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() if __name__ =="__main__": app1 = Application() # 设置窗口标题: app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title('Hello World1') app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y # 主消息循环: app2 = App2() app2https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title("helloword2") app2https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("200x200+0+0")#长x宽+x+y app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.mainloop()
7
%%writefile src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py from tkinter import Frame,Label,Button,Toplevel class Application(Frame): def __init__(self, master=None): Framehttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(self, master) #窗口大小位置 selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world1!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() class App2(Toplevel): def __init__(self, master=None): Toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(self, master) selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world2!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() if __name__ =="__main__": app1 = Application() # 设置窗口标题: app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title('Hello World1') app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y # 主消息循环: app2 = App2() app2https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title("helloword2") app2https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("200x200+0+0")#长x宽+x+y app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.mainloop()
8
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.5https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.6https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. 文本域 Text
也就是用来存放字符串的大空间
基本的定义也就是宽度width和高度height了,但由于有类编辑的需求,因此需要引入标签来定位光标https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.可以用的定位标签包括
- "
, " 行号,列号组成的坐标字符串 - "
https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.end" 行号对应行的末尾 - INSERT 光标的当前位置
- CURRENT 光标最近的字符
- END 文末
另外还有一些复杂的用法一般用不到
%%writefile src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py from tkinter import Frame,Label,Button,Toplevel class Application(Frame): def __init__(self, master=None): Framehttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(self, master) #窗口大小位置 selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world1!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() class App2(Toplevel): def __init__(self, master=None): Toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(self, master) selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world2!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() if __name__ =="__main__": app1 = Application() # 设置窗口标题: app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title('Hello World1') app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y # 主消息循环: app2 = App2() app2https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title("helloword2") app2https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("200x200+0+0")#长x宽+x+y app1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.mainloop()
9
Overwriting src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
0
Overwriting src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
1
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.5https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.7https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. 画布 Canvas
和html5中的画布一样,tk中的画布也是用来绘图的,直接看代码吧:
Overwriting src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
2
Overwriting src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
3
Overwriting src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
4
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.5https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.8https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. Listbox 列表框
文本按列表的形式排列,列表框是用来从一组文本项目选择。根据ListBox的配置,用户可以选择从列表中一个或多个项目,通常,这个任务可以由多选实现
Overwriting src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
5
Overwriting src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
6
Overwriting src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
7
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.5https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.9https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. Message 信息栏
使用单字体显示短文本消息https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.通常可以使用普通标签代替https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.
Overwriting src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
8
Overwriting src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
9
%exec_py src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
0
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.5https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.10https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. PanedWindow 中分栏窗口
可用于实现2-pane和3-pane的布局
%exec_py src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
1
%exec_py src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
2
%exec_py src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
3
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.5https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.11https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. Scale 滑块
通常如果是有界输入,那么就可以使用滑块
%exec_py src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
4
%exec_py src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
5
%exec_py src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
6
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.5https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.12https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. Scrollbar 滚动条
滚动条通常不会单独使用,而是配合文本,列表框,画板使用https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.
%exec_py src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
7
%exec_py src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
8
%exec_py src/toplevelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
9
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.5https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.13https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. Spinbox 指定输入范围值的输入框
Spinbox可以用来代替只有有限数量的命令值的输入框https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.
%%writefile src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py #coding:utf-8 from tkinter import Frame,Label,Button class Application(Frame): def __init__(self, master=None): super()https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(master) #窗口大小位置 selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quitButton = Button(self, text='Quit',fg="red", command=selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quit) selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quitButtonhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() if __name__ =="__main__": app = Application() # 设置窗口标题: apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title('Hello World') apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y # 主消息循环: apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.mainloop()
0
%%writefile src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py #coding:utf-8 from tkinter import Frame,Label,Button class Application(Frame): def __init__(self, master=None): super()https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(master) #窗口大小位置 selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quitButton = Button(self, text='Quit',fg="red", command=selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quit) selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quitButtonhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() if __name__ =="__main__": app = Application() # 设置窗口标题: apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title('Hello World') apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y # 主消息循环: apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.mainloop()
1
%%writefile src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py #coding:utf-8 from tkinter import Frame,Label,Button class Application(Frame): def __init__(self, master=None): super()https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(master) #窗口大小位置 selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quitButton = Button(self, text='Quit',fg="red", command=selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quit) selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quitButtonhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() if __name__ =="__main__": app = Application() # 设置窗口标题: apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title('Hello World') apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y # 主消息循环: apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.mainloop()
2
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.5https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.14https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. Combobox 组合框
这个控件和单选框功能类似,一般也会借助tk中的变量类型https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.
%%writefile src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py #coding:utf-8 from tkinter import Frame,Label,Button class Application(Frame): def __init__(self, master=None): super()https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(master) #窗口大小位置 selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quitButton = Button(self, text='Quit',fg="red", command=selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quit) selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quitButtonhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() if __name__ =="__main__": app = Application() # 设置窗口标题: apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title('Hello World') apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y # 主消息循环: apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.mainloop()
3
%%writefile src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py #coding:utf-8 from tkinter import Frame,Label,Button class Application(Frame): def __init__(self, master=None): super()https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(master) #窗口大小位置 selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quitButton = Button(self, text='Quit',fg="red", command=selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quit) selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quitButtonhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() if __name__ =="__main__": app = Application() # 设置窗口标题: apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title('Hello World') apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y # 主消息循环: apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.mainloop()
4
%%writefile src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py #coding:utf-8 from tkinter import Frame,Label,Button class Application(Frame): def __init__(self, master=None): super()https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(master) #窗口大小位置 selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quitButton = Button(self, text='Quit',fg="red", command=selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quit) selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quitButtonhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() if __name__ =="__main__": app = Application() # 设置窗口标题: apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title('Hello World') apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y # 主消息循环: apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.mainloop()
5
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.5https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.15https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. Notebook标签页
标签页是ttk中的控件通常用在同时管理多个资源的情况下,形式参见chrome中的标签页https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.
%%writefile src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py #coding:utf-8 from tkinter import Frame,Label,Button class Application(Frame): def __init__(self, master=None): super()https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(master) #窗口大小位置 selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quitButton = Button(self, text='Quit',fg="red", command=selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quit) selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quitButtonhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() if __name__ =="__main__": app = Application() # 设置窗口标题: apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title('Hello World') apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y # 主消息循环: apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.mainloop()
6
%%writefile src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py #coding:utf-8 from tkinter import Frame,Label,Button class Application(Frame): def __init__(self, master=None): super()https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(master) #窗口大小位置 selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quitButton = Button(self, text='Quit',fg="red", command=selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quit) selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quitButtonhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() if __name__ =="__main__": app = Application() # 设置窗口标题: apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title('Hello World') apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y # 主消息循环: apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.mainloop()
7
%%writefile src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py #coding:utf-8 from tkinter import Frame,Label,Button class Application(Frame): def __init__(self, master=None): super()https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(master) #窗口大小位置 selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quitButton = Button(self, text='Quit',fg="red", command=selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quit) selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quitButtonhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() if __name__ =="__main__": app = Application() # 设置窗口标题: apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title('Hello World') apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y # 主消息循环: apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.mainloop()
8
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.5https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.16https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. Progressbar 进度条
ttk的独有控件显示,一个长期运行的操作状态指示https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.它可以在两种模式下运行:
确定模式,显示完成的工作量与完成工作的总量;
不确定模式,提供动画显示,让用户知道工作正在进行中
下面的例子中还用到了ttk中的Separator
用于做分割,Sizegrip
用于缩放窗口
%%writefile src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py #coding:utf-8 from tkinter import Frame,Label,Button class Application(Frame): def __init__(self, master=None): super()https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.__init__(master) #窗口大小位置 selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.createWidgets() def createWidgets(self): selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabel = Label(self, text='Hello, world!') selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.helloLabelhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quitButton = Button(self, text='Quit',fg="red", command=selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quit) selfhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.quitButtonhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.pack() if __name__ =="__main__": app = Application() # 设置窗口标题: apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.title('Hello World') apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.masterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.geometry("600x400+100+400")#长x宽+x+y # 主消息循环: apphttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.mainloop()
9
Overwriting src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
0
Overwriting src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
1
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.5https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.17https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. Treeview
TreeView控件显示一个项目的树状分层集合,通常这用来展示数据或者文件系统等结构https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.
Overwriting src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
2
Overwriting src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
3
Overwriting src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
4
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.5https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.18https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. ScrolledText
带滚动条的文本框,这个组件在tkinterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.scrolledtext
下
Overwriting src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
5
Overwriting src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
6
Overwriting src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
7
1https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.6https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//. 对话框 Dialog和消息弹窗 messagebox
对话框和消息弹窗也是常见的控件,在,但他们独立于其他控件,而是放在tkinterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.filedialog
,tkinterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.simpledialog
和tkinterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.messagebox
下https://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.
在tkinter
下提供了如下几种常见的对话框形式:
messagebox下的方法都是继承自messageboxhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.dialoghttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.Dialog
只有一个按钮(确定)的消息弹窗
tkinterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.messageboxhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.showinfo(title,message)
信息弹窗tkinterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.messageboxhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.showwarning(title,message)
警告弹窗tkinterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.messageboxhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.showerror(title,message)
错误弹窗
有两个按钮的消息弹窗
tkinterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.messageboxhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.askokcancel(title,message)
(确定,取消)点击确定返回Ture
取消返回False
tkinterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.messageboxhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.askquestion(title,message)
(确定,取消)点击确定返回"yes"
取消返回"no"
tkinterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.messageboxhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.askyesno(title,message)
(是,否)点击是返回Ture
;否返回False
tkinterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.messageboxhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.askretrycancel(title,message)
(重试,取消)点击重试返回Ture
;取消返回False
有三个按钮的消息弹窗
tkinterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.messageboxhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.askyesnocancel(title,message)
是:True
;否:False
;取消:None
输入值的消息弹窗
tkinterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.simpledialoghttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.askinteger(title, prompt)
问询整数的消息弹窗tkinterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.simpledialoghttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.askfloat(title, prompt)
问询浮点数的消息弹窗tkinterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.simpledialoghttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.askstring(title, prompt)
询问字符创的消息弹窗
选择文件弹窗
tkinterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.filedialoghttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.askopenfilename()
打开一个文件的弹窗,返回文件名列表tkinterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.filedialoghttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.asksaveasfilename()
保存一个文件的弹窗tkinterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.filedialoghttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.askopenfile()
打开一个文件的弹窗,返回文件对象tkinterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.filedialoghttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.askopenfiles()
打开多个文件的弹窗,返回文件对象的列表tkinterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.filedialoghttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.asksaveasfile()
保存一个文件的弹窗,返回文件对象tkinterhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.filedialoghttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.askdirectory()
打开一个文件夹的弹窗,返回文件地址
Overwriting src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
8
Overwriting src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
9
%exec_py src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
0
%exec_py src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
1
%exec_py src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
2
%exec_py src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
3
%exec_py src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
4
%exec_py src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
5
%exec_py src/firstGUIhttps://blog.hszofficial.site/TutorialForPython/输入输出篇/人机交互/GUI//.py
6
还没有评论,来说两句吧...