TKinter

GUI编程包,生成图形用户界面

  • frame:作为其它窗口的容器
  • label:显示图片文字等
  • entry
  • grid:不会随着窗口大小改变
  • destroy:删掉窗口
  • pack:可以随着窗口改变大小
  • radiobutton/checkbox:单选按钮与复选框
  • message:展示文字
  • place:布局

event:事件绑定机制

绑定一个句柄,处理函数可以被绑定给各种

#空窗口
from tkinter import *

top = Tk()
top.mainloop()

#Frame 和 Lable 的结合
import tkinter as tk

top = tk.Tk()

tk.Label(top,text = "on the window").pack()

frm = tk.Frame(top)
frm.pack()

frm1 = tk.Frame(frm)
frm2 = tk.Frame(frm)

frm1.pack(side = "left")
frm2.pack(side = "right")

tk.Label(frm1,text = "on frm1-1").pack()
tk.Label(frm1,text = "on frm1-2").pack()
tk.Label(frm2,text = "on frm2-1").pack()

top.mainloop()
#Entry 控件:接受字符串输入
from tkinter import *

def show_entry_fields():
    print("First Name: %s\nLast Name: %s"%(e1.get(),e2.get()))

master = Tk()
Label(master,text = "First Name").grid(row = 0)
Label(master,text = "Last Name").grid(row = 1)

e1 = Entry(master)
e2 = Entry(master)

e1.grid(row = 0,column = 1)
e2.grid(row = 1,column = 1)

Button(master,text = "Quit",command = master.quit).grid(row = 3,column = 0,sticky = W,pady = 4)
Button(master,text = "Show",command = show_entry_fields).grid(row = 3,column = 1,sticky = W,pady = 4)

mainloop()

First Name: a
Last Name: v
#  Button控件,用于展示不同样式的按钮,与用户交互
import tkinter as tk

counter = 0
def counter_label(label):
    counter = 0
    def count():
        global counter
        counter += 1
        label.config(text = str(counter))
        label.after(100,count)
    count()

root = tk.Tk()
root.title("Counting Seconds")
label = tk.Label(root,fg = "dark green")
label.pack()
counter_label(label)
button = tk.Button(root,text = "Stop",width = 25,command = root.destroy)
button.pack()
root.mainloop()

# radiobutton & checkbox:单选或复选框,返回1或0
from tkinter import *
master = Tk()

def var_states():
    print("male:%d\nfemale:%d"%(var1.get(),var2.get()))

Label(master,text = "Your Sex:").grid(row = 0,sticky = W)
var1 = IntVar()
Checkbutton(master,text = 'male',variable = var1).grid(row = 1,sticky = W)
var2 = IntVar()
Checkbutton(master,text = 'female',variable = var2).grid(row = 2,sticky = W)
Button(master,text = "Quit",command = master.quit).grid(row = 3,sticky = W,pady = 4)
Button(master,text = "Show",command = var_states).grid(row = 4,sticky = W,pady = 4)

mainloop()
# message 用于展示文字短消息
from tkinter import *
master = Tk()
whatever_you_do = "jjfabsbsfandlnvoisbet"
msg = Message(master,text = whatever_you_do)
msg.config(bg = "lightgreen",font = ("times",14,"italic"))
msg.pack()
mainloop()

界面布局

  • Gird布局:把整个窗口划分成格子
  • Pack布局:流式布局,将控件水平或垂直地逐个放在一起
  • Place布局:指定控件的绝对位置或相对于其他控件的位置
# Place布局的应用
import tkinter as tk
import random

root = tk.Tk()
# width x height + x_offset + y_offset
root.geometry("170x200+30+30")

language = ["Python","Perl","C++","Java","Tcl/Tk"]
labels = range(5)
for i in range(5):
    ct = [random.randrange(256)for x in range(3)]
    brightness = int(round(0.299*ct[0]+0.587*ct[1]+0.114*ct[2]))
    ct_hex = "%02x%02x%02x"%tuple(ct)
    bg_colour = '#'+''.join(ct_hex)
    l = tk.Label(root,
                 text = language[i],
                 fg = "White" if brightness < 120 else 'Black',
                 bg = bg_colour)
    l.place(x = 20,y = 30+i*30,width = 120,height = 25)

root.mainloop()

Event绑定机制

  • 一个tkinter应用生命周期的大部分时间都处在消息循环(event loop)中,等待事件发生(点按钮,鼠标移动等)
  • Tkinter 提供了用以处理相关事件的机制,处理函数可以被绑定给各个可控的相关事件 widget.bind(event,handler)
  • 如果相关事件发生,handler函数会被触发,事件对象event 会传递给handler函数
#获取鼠标坐标的event

from tkinter import *

def motion(event):
    print("Mouse position: (%s,%s)"%(event.x,event.y))
    return

master = Tk()
whatever_you_do = "hhhhhhhhhhhhhh"
msg = Message(master,text = whatever_you_do)
msg.config(bg = 'lightgreen',font = ('times',24,'italic'))
msg.bind('<Motion>',motion)
msg.pack()
mainloop()