Tkinter Entry
Entry极简实例
from tkinter import *
root = Tk()
Entry(root, text='input your text here').pack() # text毫无作用
root.mainloop()
Entry与变量
from tkinter import *
root = Tk()
e = StringVar()
entry = Entry(root, textvariable=e)
e.set('entry your text here') # 设定初始文本
entry.pack()
root.mainloop()
Entry设置只读
from tkinter import *
root = Tk()
e = StringVar()
entry = Entry(root, textvariable=e, state='readonly')
e.set('entry your text here')
entry.pack()
root.mainloop()
密码输入框
from tkinter import *
root = Tk()
Label(root, text='input your password below').pack()
e = StringVar()
entry = Entry(root, textvariable=e, show='*')
entry.pack()
root.mainloop()
前景色/背景色/外观/宽度/高度/对齐方式/状态与Lable相同。
Tkinter CheckButton
多选按钮,可以表示两种状态:On或Off,可以设置回调函数,每当点击此按钮时回调函数被调用。
CheckButton极简例子
from tkinter import *
root = Tk()
Checkbutton(root, text='python').pack()
root.mainloop()
CheckButton事件处理函数
from tkinter import *
def callback():
print('checked this button')
root = Tk()
Checkbutton(root, text='python', command=callback).pack()
root.mainloop()
改变CheckButton的显示文本
from tkinter import *
def callback():
v.set('change checkbutton')
root = Tk()
v = StringVar()
v.set('check button')
Checkbutton(root, text='python', command=callback, textvariable=v).pack()
root.mainloop()
将CheckButton的值与变量绑定
from tkinter import *
def callback():
print(v.get())
v.set(v.get())
root = Tk()
v = IntVar()
v.set('check button')
Checkbutton(root, text='python', command=callback, variable=v, textvariable=v).pack()
root.mainloop()
设置CheckButton的状态值
from tkinter import *
def callback():
print(v.get())
root = Tk()
v = StringVar()
v.set('check button')
Checkbutton(root, text='python', command=callback, variable=v, onvalue='python', offvalue='tkinter').pack()
root.mainloop()
Tkinter RadioButton
单选按钮
RadioButton极简例子
from tkinter import *
root = Tk()
Radiobutton(root, text='python').pack()
Radiobutton(root, text='tkinter').pack()
Radiobutton(root, text='widget').pack()
root.mainloop()
为RadioButton指定组
from tkinter import *
root = Tk()
v = IntVar()
v.set(1)
for i in range(3):
Radiobutton(root, variable=v, text='python', value=i).pack()
root.mainloop()
创建不同的组
from tkinter import *
root = Tk()
vLang = IntVar()
vOS = IntVar()
vLang.set(1)
vOS.set(2)
for v in [vLang,vOS]:
for i in range(3):
Radiobutton(root, variable=v, value=i, text='python' + str(i)).pack()
root.mainloop()
使用相同的value
from tkinter import *
root = Tk()
v = IntVar()
v.set(1)
for i in range(3):
Radiobutton(root, variable=v, value=1, text='python' + str(i)).pack()
for i in range(3):
Radiobutton(root, variable=v, value=i, text='python' + str(2 + i)).pack()
root.mainloop()
RadioButton绑定事件处理函数
from tkinter import *
def r1():
print('call r1')
def r2():
print('call r2' )
def r3():
print('call r3')
def r4():
print('call r4' )
i = 0
root = Tk()
v = IntVar()
v.set(0)
for r in [r1, r2, r3, r4]:
Radiobutton(root, variable=v, text='radio button', value=i, command=r).pack()
Radiobutton(root, variable=v, text='radio button', value=i, command=r).pack()
i += 1
root.mainloop()
RaidoButton外观效果
from tkinter import *
root = Tk()
v = IntVar()
v.set(1)
for i in range(3):
Radiobutton(root, variable=v, indicatoron=0, text='python & tkinter', value=i).pack()
root.mainloop()