Tkinter Label
Lable的极简例子
from tkinter import *
root = Tk()
label = Label(root, text='timing')
label.pack() # 显示Lable必需
root.mainloop() # 显示主界面必需
Lable使用内置位图
from tkinter import*
root = Tk()
label = Label(root, text='timing')
label.pack()
for bitmap in ['error', 'hourglass', 'info', 'questhead', 'question', 'warning', 'gray12', 'gray25', 'gray50', 'gray75']:
label = Label(root, bitmap=bitmap)
label.pack()
root.mainloop()
Label的前景色和后景色
from tkinter import *
root = Tk()
label = Label(root, text='timing', fg='red', bg='gray')
label.pack()
root.mainloop()
Label的高度和宽度
from tkinter import *
root = Tk()
for color in ['red', 'blue', 'yellow']:
label = Label(root, text=color, bg=color)
label.pack()
for color in ['red', 'blue', 'yellow']:
label = Label(root, text=color, bg=color, width=10, height=3)
label.pack()
root.mainloop()
Label使用图像和文本
compound指定文本与图像是如何在文本上显示的,缺省为None,当指定图像时,文本将被覆盖,只显示图像。
left:图像居左
right:图像居右
top:图像居上
bottom:图像居下
center:文字覆盖在图像上
from tkinter import *
root = Tk()
for location in ['left', 'right', 'top', 'bottom', 'center']:
label = Label(root, text=location, bitmap='error', compound=location)
label.pack()
root.mainloop()
Label文本的多行显示
from tkinter import *
root = Tk()
for justify in ['left', 'right']:
for anchor in ['e', 'w', 'n', 's', 'ne', 'se', 'sw', 'se', 'center']:
label = Label(root, text='test for the label justify and anchor',
bg='yellow',
width=40,
height=3,
wraplength=80, # 每行最大的字符数
justify=justify, # 文本对齐方式
anchor=anchor) # 指定文本在Label中的显示位置
label.pack()
root.mainloop()
Tkinter Button
Button极简例子
from tkinter import *
def hello():
print('hello button')
root = Tk()
Button(root, text='click button', command=hello).pack()
root.mainloop()
Button的外观
from tkinter import *
def hello():
print('hello button')
root = Tk()
for flat in [FLAT, GROOVE, RAISED, RIDGE, SOLID, SUNKEN]:
Button(root, text='%s button' % str(flat).lower(), relief=flat, command=hello).pack()
root.mainloop()
Button的图像显示
可使用位图或gif图
from tkinter import *
def hello():
print('hello button')
root = Tk()
Button(root, bitmap='error', command=hello).pack()
root.mainloop()
Button显示文本和图像
与Label类似
from tkinter import *
def hello():
print('hello button')
root = Tk()
for location in ['left', 'right', 'top', 'bottom', 'center']:
Button(root, text=location, bitmap='error', compound=location, command=hello).pack()
root.mainloop()
Button的焦点
from tkinter import *
def cb1():
print('button1 clicked')
def cb2(event):
print('button2 clicked')
def cb3():
print('button3 clicked')
root = Tk()
b1 = Button(root, text='Button1', command=cb1)
b2 = Button(root, text='Button2')
b2.bind("<Return>", cb2)
b3 = Button(root, text='Button3', command=cb3)
b1.pack()
b2.pack()
b3.pack()
b2.focus_set() # 焦点在button2上,按回车直接调用cb2
root.mainloop()
Button的宽度、高度、文本位置、前景色、背景色与Label方法相同
Button的边框
from tkinter import *
def hello():
print('hello button')
root = Tk()
for b in [0, 2, 4, 6, 7, 9]:
Button(root, text=str(b), bd=b, command=hello).pack()
root.mainloop()
Button的外观效果与Lable相同
Button的状态
from tkinter import *
def hello():
print('hello button')
root = Tk()
for state in ['normal', 'active', 'disabled']:
Button(root, text=state,
state=state,
command=hello).pack()
root.mainloop()
Button与变量
from tkinter import *
def change():
v.set('changed')
root = Tk()
v = StringVar()
button = Button(root, textvariable=v, command=change)
v.set('click here to change') # 设置初始显示文本
button.pack()
root.mainloop()
二十弱冠,三十而立,四十不惑,五十而知天命。自勉