讲了什么:Python tkinter 可视化界面编辑库
2022/1/27 First Update 14947字/390行
import tkinter as tk
window=tk.Tk() # 实例化
window.title("tkinter 基础")
# 长x宽+左侧边距+上侧边距
window.geometry("300x200+400+200")
# This subcommand returns or sets platform specific attributes <__int__.py>
window.attributes("-alpha",0.4)
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() # 效果画板为不为红色画板区域不透明
# 无法移动窗口,点击按钮
def com(*args): # 此方法必须留一个参数
print(1)
tk.Button(window,text="按钮",command=com).pack()
window.attributes("-disabled",True) # False 不锁定
自己写出来试试
window.attributes("-fullscreen",True)
自己写出来试试
window.attributes("-toolwindow",True)
window.attributes("-topmost",True)
panel = tk.Toplevel(window)
panel.geometry("200x200")
panel = tk.Toplevel(window)
panel.geometry("200x200")
def com(*args):
window.focus_set()
window.focus_force()
tk.Button(panel,text="按钮",command=com).pack() # 点击按钮主窗口显示在最顶层
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() # 点击按钮1主界面置顶并打印.
tk.Button(panel,text="按钮2",command=com1).pack() # 点击按钮2子界面置顶并打印.!toplevel
window.mainloop()
# 原理 循环自调 程序中断
# 在这一句后面的代码运行时无法运行
def mainloop(self, n=0):
"""Call the mainloop of Tk."""
self.tk.mainloop(n)
控件 | 描述 |
---|---|
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 |
# 控件自定义标志 = tk.控件(依附在谁上面,参数名="...")
Label = tk.Label(window,text="Hello World!")
取图自 不离鞘
arrow,circle,clock,cross,dotbox,exchange,fleur,
heart,man,mouse,pirate,plus,shuttle,sizing,
spider,spraycan,star,target,tcross,trek,watch
注意:Listbox等列表控件的height是列表有几行而不是px(像素)
设置控件3D效果,可选的有:FLAT、SUNKEN、RAISED、GROOVE、RIDGE。默认为 FLAT。
Button=tk.Button(window,text="按钮",activebackground="green")
Button.pack()
挺全的: by 南风丶轻语
bold(粗体) 默认值为normal
italic(斜体) 默认值为roman
underline(下划线) 默认值为false
overstrike(删除线)) 默认值为false
# 代码原创
# Label=tk.Label(master,text='string',font=('字体','字号','样式'))
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)
Button=tk.Button(window,text="按钮",foreground="red")
Button.pack()
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()
# 按按钮1时按钮1所在的Frame高亮变红
# 按按钮2时按钮2所在的Frame高亮变红
控件被禁用时,文字颜色的使用。如果省略或空白,前景是用来代替标准
# 代码原创
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()
回调函数可以是函数、绑定方法,或者任何其他可调用的Python对象 【实例如上】
可选项包括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()
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()
通常是一个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()
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)