【Python tkinter】基础控件到简单bind,了解tkinter一篇就够
讲了什么:Python tkinter 可视化界面编辑库
2022/1/27 First Update 14947字/390行
1.主页面设置
(1) 实例化
import tkinter as tk
window=tk.Tk()
window.title("tkinter 基础")
window.geometry("300x200+400+200")
(2) 设置属性 window.attributes()
alpha (double) 透明度
window.attributes("-alpha",0.4)
transparentcolor (color) 面板中有该颜色的透明
window.attributes("-transparentcolor","red")
canvas1 = tk.Canvas(window,bg="red", width=100, height=100)
canvas1.pack()
canvas2 = tk.Canvas(window,bg="yellow", width=100, height=100)
canvas2.pack()
disabled (bool) 锁定界面
def com(*args):
print(1)
tk.Button(window,text="按钮",command=com).pack()
window.attributes("-disabled",True)
自己写出来试试
fullscreen (bool) 满屏
window.attributes("-fullscreen",True)
自己写出来试试
toolwindow (bool) 工具窗口
window.attributes("-toolwindow",True)
topmost (bool) 置顶
window.attributes("-topmost",True)
panel = tk.Toplevel(window)
panel.geometry("200x200")
(3) focus 聚焦设置
focus_set() ,focus_force()
panel = tk.Toplevel(window)
panel.geometry("200x200")
def com(*args):
window.focus_set()
window.focus_force()
tk.Button(panel,text="按钮",command=com).pack()
focus_get()
panel = tk.Toplevel(window)
panel.geometry("200x200")
def com(*args):
window.focus_set()
window.focus_force()
a=window.focus_get()
print(a)
def com1(*args):
panel.focus_set()
panel.focus_force()
a=panel.focus_get()
print(a)
tk.Button(panel,text="按钮1",command=com).pack()
tk.Button(panel,text="按钮2",command=com1).pack()
(4) 界面运行
window.mainloop()
def mainloop(self, n=0):
"""Call the mainloop of Tk."""
self.tk.mainloop(n)
1.控件介绍
综述
控件 | 描述 |
---|
Button | 按钮控件;在程序中显示按钮 | Canvas | 画布控件;显示图形元素如线条或文本 | Checkbutton | 多选框控件;用于在程序中提供多项选择框 | Entry | 输入控件;用于显示简单的文本内容 | Frame | 框架控件;在屏幕上显示一个矩形区域,多用来作为容器 | Label | 标签控件;可以显示文本和位图 | Listbox | 列表框控件;在Listbox窗口小部件是用来显示一个字符串列表给用户 | Menubutton | 菜单按钮控件,用于显示菜单项。 | Menu | 菜单控件;显示菜单栏,下拉菜单和弹出菜单 | Message | 消息控件;用来显示多行文本,与label比较类似 | Radiobutton | 单选按钮控件;显示一个单选的按钮状态 | Scale | 范围控件;显示一个数值刻度,为输出限定范围的数字区间 | Scrollbar | 滚动条控件,当内容超过可视化区域时使用,如列表框 | Text | 文本控件;用于显示多行文本 | Treeview | 表格 | Toplevel | 容器控件;用来提供一个单独的对话框,和Frame比较类似 | Spinbox | 输入控件;与Entry类似,但是可以指定输入范围值 | PanedWindow | 窗口布局管理的插件;可以包含一个或者多个子控件 | LabelFrame | 简单的容器控件;常用与复杂的窗口布局 | tkMessageBox | 用于显示你应用程序的消息框 |
上述表格借鉴 菜鸟编程
基础参数
(
基
本
汇
总
)
_{(基本汇总)}
(基本汇总)?
常见参数
参数名 | 那些控件包含 |
---|
background | PanedWindow,Label,Button,Listbox,Canvas,Checkbutton,Entry,Frame,Radiobutton,Scale,Scrollbar,Text,Toplevel,Spinbox,LabelFrame | borderwidth | PanedWindow,Label,Button,Listbox,Canvas,Checkbutton,Entry,Frame,Radiobutton,Scale,Scrollbar,Text,Toplevel,Spinbox,LabelFrame | cursor | PanedWindow,Label,Button,Listbox,Canvas,Checkbutton,Entry,Frame,Radiobutton,Scale,Scrollbar,Text,Toplevel,Spinbox,LabelFrame | height | PanedWindow,Label,Button,Listbox,Canvas,Checkbutton,Frame,Radiobutton,Text,Toplevel,LabelFrame | relief | PanedWindow,Label,Button,Listbox,Canvas,Checkbutton,Entry,Frame,Radiobutton,Scale,Scrollbar,Text,Toplevel,Spinbox,LabelFrame | activebackground | Label,Button,Checkbutton,Radiobutton,Scale,Scrollbar,Spinbox | font | Label,Button,Listbox,Checkbutton,Entry,Radiobutton,Scale,Text,Spinbox,LabelFrame | foreground | Label,Listbox,Checkbutton,Entry,Radiobutton,Scale,Text,Spinbox,LabelFrame | highlightbackground | Label,Listbox,Canvas,Checkbutton,Entry,Frame,Radiobutton,Scale,Scrollbar,Text,Toplevel,Spinbox,LabelFrame | highlightcolor | Label,Button,Listbox,Canvas,Checkbutton,Entry,Frame,Radiobutton,Scale,Scrollbar,Text,Toplevel,Spinbox,LabelFrame | highlightthickness | Label,Button,Listbox,Canvas,Checkbutton,Entry,Frame,Radiobutton,Scale,Scrollbar,Text,Toplevel,Spinbox,LabelFrame | takefocus | Label,Button,Listbox,Canvas,Checkbutton,Entry,Frame,Radiobutton,Scale,Scrollbar,Text,Toplevel,Spinbox,LabelFrame | width | Label,Listbox,Button,Canvas,Checkbutton,Entry,Frame,Radiobutton,Scale,Scrollbar,Text,Toplevel,Spinbox,LabelFrame | bd=borderwidth | Listbox,Canvas,Checkbutton,Entry,Frame,Radiobutton,Scale,Scrollbar,Toplevel | bg=background | Listbox,Canvas,Checkbutton,Entry,Frame,Radiobutton,Scale,Scrollbar,Toplevel |
较常见参数
参数名 | 那些控件包含 |
---|
disabledforeground | Label,Button,Checkbutton,Radiobutton,Spinbox | command | Checkbutton,Radiobutton,Scale,Scrollbar,Spinbox,Button | justify | Label,Button,Checkbutton,Entry,Radiobutton,Spinbox | padx | Label,Button,Checkbutton,Radiobutton,Text,LabelFrame | pady | Label,Button,Checkbutton,Radiobutton,Text,LabelFrame | text | Label,Button,Checkbutton,Radiobutton,LabelFrame | textvariable | Label,Button,Checkbutton,Entry,Radiobutton,Spinbox | fg=foreground | Listbox,Checkbutton,Entry,Radiobutton,Scale | underline | Label,Button,Checkbutton,Radiobutton | selectbackground | Listbox,Canvas,Entry,Text,Spinbox | selectborderwidth | Listbox,Canvas,Entry,Text,Spinbox | selectforeground | Listbox,Canvas,Entry,Text,Spinbox | xscrollcommand | Listbox,Canvas,Entry,Text,Spinbox | anchor | Label,Button,Checkbutton,Radiobutton | bitmap | Label,Button,Checkbutton,Radiobutton | image | Label,Button,Checkbutton,Radiobutton | wraplength | Label,Checkbutton,Radiobutton | repeatdelay | Button,Scale,Scrollbar,Spinbox | repeatinterval | Button,Scale,Scrollbar,Spinbox | exportselection | Listbox,Entry,Text,Spinbox | insertbackground | Canvas,Entry,Text,Spinbox | insertborderwidth | Canvas,Entry,Text,Spinbox | insertofftime | Canvas,Entry,Text,Spinbox | insertontime | Canvas,Entry,Text,Spinbox | insertwidth | Canvas,Entry,Text,Spinbox | class | Frame,Toplevel,LabelFrame | colormap | Frame,Toplevel,LabelFrame | container | Frame,Toplevel,LabelFrame | visual | Frame,Toplevel,LabelFrame | yscrollcommand | Listbox,Canvas,Text |
个别参数
控件 | 控件包含 |
---|
PanedWindow | widthhandlepad,handlesize,opaqueresize,sashcursor,sashpad,sashrelief,sashwidth,showhandle | Button | foregroundhighlightbackground,wraplength,compound,default,overrelief | Listbox | selectmode,setgrid,listvariable | Text | setgrid,autoseparators,maxundo,spacing1,spacing2,spacing3,tabs,undo,wrap | Canvas | closeenough,confine,offset,scrollregion,xscrollincrement,yscrollincrement | Checkbutton | offvalue,indicatoron,onvalue,selectcolor,selectimage | Radiobutton | indicatoron,selectcolor,selectimage,value | Entry | invcmd,show,invalidcommand,validate,validatecommand,vcmd | Spinbox | invalidcommand,validate,from,label,to,wrap,buttonbackground,buttoncursor,buttondownrelief,buttonuprelief,disabledbackground,format,increment,readonlybackground,validatecommandvalues | Scale | bigincrement,digits,from,label,length,resolution,showvalue,sliderlength,sliderrelief,tickinterval,to,troughcolor | Scrollbar | activerelief,troughcolor,elementborderwidth,jump | Toplevel | menu,screen,use | LabelFrame | labelanchor,labelwidget |
详细解释(建议点目录)
使用方法
Label = tk.Label(window,text="Hello World!")
background 背景颜色=bg
borderwidth 按钮边框的大小
默
认
为
2
个
像
素
_{默认为 2 个像素}
默认为2个像素?
cursor 鼠标形状
取图自 不离鞘 arrow,circle,clock,cross,dotbox,exchange,fleur, heart,man,mouse,pirate,plus,shuttle,sizing, spider,spraycan,star,target,tcross,trek,watch
height 控件的高度
注意:Listbox等列表控件的height是列表有几行而不是px(像素)
relief 边框样式
设置控件3D效果,可选的有:FLAT、SUNKEN、RAISED、GROOVE、RIDGE。默认为 FLAT。
activebackground 当鼠标按压时,按钮的背景色
Button=tk.Button(window,text="按钮",activebackground="green")
Button.pack()
font 文本字体
支持字体
挺全的: by 南风丶轻语
样式
bold(粗体) 默认值为normal italic(斜体) 默认值为roman underline(下划线) 默认值为false overstrike(删除线)) 默认值为false
yangshi=["bold","italic","underline","overstrike"]
for i in range(len(yangshi)):
string = str(yangshi[0:i+1]).replace("[","").replace("]","")
label = tk.Label(window, text="样例",font=("华文新魏",30,eval(string)))
label.grid(row=i,column=0)
foreground 字体颜色
Button=tk.Button(window,text="按钮",foreground="red")
Button.pack()
HighLight
by 火鸟网络.
Frame=tk.Frame(
window,
width = 100,
height = 100,
bg="yellow",
highlightbackground="blue",
highlightcolor="red",
highlightthickness=4
)
def com(*args):
Frame.focus_set()
Frame.focus_force()
a=Frame.focus_get()
print(a)
Button=tk.Button(Frame,text="按钮",command=com).pack()
Frame.pack()
Frame1=tk.Frame(
window,
width = 100,
height = 100,
bg="yellow",
highlightbackground="blue",
highlightcolor="red",
highlightthickness=4
)
Frame1.pack()
def com1(*args):
Frame1.focus_set()
Frame1.focus_force()
a=Frame1.focus_get()
print(a)
Button1=tk.Button(Frame1,text="按钮",command=com1).pack()
highlightbackground 高亮边框的颜色
- 指定当控件没有获得焦点的时候高亮边框的颜色
- 默认值由系统指定,通常是标准背景颜色
highlightcolor 高亮边框的颜色
- 指定当控件获得焦点的时候高亮边框的颜色
- 默认值由系统指定
highlightthickness 边框的宽度
- 指定高亮边框的宽度
- 默认值是 0(不带高亮边框)
takefocus 如果为真,用户可以使用tab键移动到这个小部件
disabledforeground 控件被禁用时的文字颜色
控件被禁用时,文字颜色的使用。如果省略或空白,前景是用来代替标准
def com1(*args):
Button1.config(state='normal')
def com2(*args):
Button1.config(state='disabled')
Button1=tk.Button(window,text="按钮1",fg="red",disabledforeground="blue")
Button1.pack()
Button2=tk.Button(window,text="禁用",command=com2)
Button2.pack()
Button3=tk.Button(window,text="激活",command=com1)
Button3.pack()
command 按下按钮时调用的函数或方法
回调函数可以是函数、绑定方法,或者任何其他可调用的Python对象 【实例如上】
justify 显示多行文本的时候,设置不同行之间的对齐方式
可选项包括left, right, center
def com(*args):
Text1.config(justify="right")
def com1(*args):
Text1.config(justify="center")
def com2(*args):
Text1.config(justify="left")
Text1=tk.Button(window,text="多行文本\n多行多行文本\n多行多行多行文本\n多多行行文本\n多行文本",width=30,height=5)
Text1.pack()
Button1=tk.Button(window,text="右对齐",command=com)
Button1.pack()
Button2=tk.Button(window,text="左对齐",command=com2)
Button2.pack()
Button3=tk.Button(window,text="居中对齐",command=com1)
Button3.pack()
padx 文本或图像和边框之间的额外水平填充
pady 文本或图像和边框之间的额外垂直填充
x=10
y=10
Button1=tk.Button(window,text="按钮",padx=x,pady=y)
Button1.pack()
def xadd(*args):
global x
x=x+1
Button1.config(padx=x)
def yadd(*args):
global y
y=y+1
Button1.config(pady=y)
Button2=tk.Button(window,text="变胖",command=xadd)
Button2.pack()
Button3=tk.Button(window,text="变高",command=yadd)
Button3.pack()
textvariable 文案变量
通常是一个StringVar。如果变量被更改,按钮文本将被更新。
num=0
text=tk.StringVar()
Lable=tk.Label(window,textvariable=text,width=8,height=2)
Lable.pack()
text.set("你好")
def click(*args):
global num
num+=1
text.set(f"点击了{num}次")
Button=tk.Button(window,text="点击",command=click)
Button.pack()
window.mainloop()
selectbackground 选择背景色
selectborderwidth 选择边框宽度
selectforeground 选择文本颜色
ListBox=tk.Listbox(
window,width=10,height=7,
selectbackground="yellow",
selectborderwidth=4,
selectforeground="green")
for k in range(7):
ListBox.insert("end",f"条目({k})")
ListBox.pack()
引用
python3内置的tkinter参数释疑 by wozijisun python中Tkinter的鼠标样式cursor(带图示)by 不离鞘 python tkinter 基本使用 by 火鸟网络 Python GUI编程(Tkinter)
|