Tkinter模块是Python的标准Tk GUI工具包的接口。可以在大多数的Unix平台下使用,同样可以应用在Windows和Mac系统里。
最基本的使用:
显示一个窗口,窗口里面有一个标签
import tkinter as tk
#top level, root window
app = tk.Tk()
app.title("CXY demo")
mLabel = tk.Label(app,text="this is a label")
#自动调节组件尺寸
mLabel.pack()
app.mainloop()
按钮的基本使用方法:
设置按钮字体颜色,设置按下后的回调函数
import tkinter as tk
class APP:
def __init__(self, master):
#一个框架,通常用于给控件分组
frame = tk.Frame(master)
#设置对齐方式和边距
frame.pack(side = tk.LEFT, padx = 10, pady = 10)
#创建一个按钮,设置文言和前景色, 设置点击按钮后触发的事件
self.button = tk.Button(frame,text = "say hello", fg = "blue",
command = self.say_hello)
self.button.pack()
def say_hello(self):
print("hello world!")
root = tk.Tk()
app = APP(root)
root.mainloop()
from tkinter import *
def m_callback():
var.set("Run!Quick!")
root = Tk()
frame1 = Frame(root)
frame2 = Frame(root)
var = StringVar()
var.set("Warning! \nnuclear weapon detected!")
textLabel = Label(frame1, textvariable = var,
justify = LEFT,
padx = 10)
textLabel.pack(side = LEFT)
#不支持jpg格式。。。
photo = PhotoImage(file = "nuclear.gif")
imgLabel = Label(frame1, image = photo)
imgLabel.pack(side = RIGHT)
mButton = Button(frame2, text = "Help!", command = m_callback)
mButton.pack()
frame1.pack(padx = 10, pady = 10)
frame2.pack(padx = 10, pady = 10)
mainloop()
让文字显示在图片之上(Label的compound参数)
from tkinter import *
root = Tk()
photo = PhotoImage(file = "nuclear.gif")
#compound 让文字显示在图片之上
imgLabel = Label(root, text = "Warning\n!!!!!",
justify = LEFT, image = photo,
compound = CENTER, font = ("微软雅黑", 20),
fg = "yellow")
imgLabel.pack()
mainloop()
多选按钮的基本使用方法:
按下按钮可以显示用户选择结果(0为未选中,1为选中)
from tkinter import *
root = Tk()
root.title("这个周末去干啥?")
Options = ["去沙漠滑冰", "去火星度假", "在海底遛狗","洗洗睡吧。。。"]
#记录到底选了什么
selected = []
for opt in Options:
#创建一个变量用于存储用户的选择
selected.append(IntVar())
#创建多选按钮
temp = Checkbutton(root, text = opt, variable = selected[-1])
#挂在最左边(用WESN表示上下左右(东南西北))
temp.pack(anchor = W)
def m_callback():
for sel in selected:
print(sel.get())
mButton = Button(root, text = "决定就是你了!", command = m_callback)
mButton.pack()
mainloop()
单选按钮的基本使用方法:
from tkinter import *
root = Tk()
v = IntVar()
#variable一组Radiobutton必须用一个变量,比如选中Two时,v的值就为后面的value的值,即2
Radiobutton(root, text = "One", variable = v, value = 1).pack(anchor = W)
Radiobutton(root, text = "Two", variable = v, value = 2).pack(anchor = W)
Radiobutton(root, text = "Three", variable = v, value = 3).pack(anchor = W)
Radiobutton(root, text = "Four", variable = v, value = 4).pack(anchor = W)
#做一个LabelFrame来存放单选按钮
group = LabelFrame(root, text = "你认为最厉害的人是谁?", padx = 5, pady = 5)
group.pack(padx = 10, pady = 10)
Options = [
("超人", 1),
("孙悟空", 2),
("比尔盖茨", 3),
("你自己", 4),
]
v = IntVar()
v.set(1)
#indicatoron = False使前面不再是小圆圈的形式,fill = X为横向填充
for name, num in Options:
b = Radiobutton(group, text = name, variable = v, value = num, indicatoron = False)
b.pack(fill = X)
mainloop()
输入框的基本使用方法:
from tkinter import *
root = Tk()
e = Entry(root)
e.pack(padx = 20, pady = 20)
#清空
e.delete(0, END)
#插入默认文本
e.insert(0,"defaults")
mainloop()
加入验证和隐藏功能的输入框
from tkinter import *
root = Tk()
#用表格的方式布局控件
Label(root, text = "账户:").grid(row = 0, column = 0)
Label(root, text = "密码:").grid(row = 1, column = 0)
e1 = Entry(root)
def m_test():
if e2.get() == "good":
print("OK")
return True
else:
print("NG")
e2.delete(0, END)
return False
def m_invalid():
print("try again!")
#密码输入框要求显示*,并且必须为good,当失去焦点时进行验证,验证函数注册在validatecommand里
#validatecommand为False时,会调用invalidcommand
e2 = Entry(root, show = "*", validate = "focusout",
validatecommand = m_test, invalidcommand = m_invalid)
e1.grid(row = 0, column = 1, padx = 10, pady = 5)
e2.grid(row = 1, column = 1, padx = 10, pady = 5)
def m_show():
print("账户:%s" % e1.get())
print("密码:%s" % e2.get())
Button(root, text = "确认", width = 10, command = m_show)\
.grid(row = 3, column = 0, sticky = W, padx = 10, pady = 5)
#quit方法要想正常退出,需要双击打开py文件,在IDE里启动不好使
Button(root, text = "退出", width = 10, command = root.quit)\
.grid(row = 3, column = 1, sticky = E, padx = 10, pady = 5)
mainloop()