Tkinter (06) 框架部件 Frame
框架部件的创建及其选项
框架不像一般的部件, 基本上就是一个容器, 可以放进其他的部件, 每一个容器内的部件布置都是自成一个格局.
import tkinter as tk
parent = tk.Tk()
w = tk.Frame(parent, option, ...)
选项 | 说明 |
---|---|
bg or background | 背景颜色 |
bd or borderwidth | 外框寛度, 内定为 0 |
cursor | 在部件上方时,鼠标的图样 |
height | 垂直高度, 一般无效, 除非设置了 grid_propagate(0) |
highlightbackground | 非聚焦时的聚焦颜色 |
highlightcolor | 聚焦时的聚焦颜色 |
highlightthickness | 聚焦厚度,内定 1, 0 则无 |
padx | 内部水平点素间隔, 一般框架都是紧贴内部件 |
pady | 内部垂直点素间隔, 一般框架都是紧贴内部件 |
relief | 外框花样,内定为 FLAT |
takefocus | TAB 键在部件是否会循环焦点 0/1 |
width | 水平寛度, 一般无效, 除非设置了 grid_propagate(0) |
范例视窗及代码
from functools import partial
import tkinter as tk
def callback(bttn):
status.configure(text=f'{bttn} clicked !')
def button(text, size, color):
bttn = Button(right_frame, text=text, **size, **color, command=partial(callback, text))
return bttn
black = {'bg':'black', 'fg':'white'}
green = {'bg':'green', 'fg':'white'}
blue = {'bg':'blue', 'fg':'white'}
root = tk.Tk()
root.wm_title("Frame Demo")
root.config(background = "#004000")
left_frame = tk.Frame(root, width=200, height = 600, bg="blue")
left_frame.grid(row=0, column=0, padx=10, pady=2)
left_head = tk.Label(left_frame, text="Instructions:", width=10)
left_head.grid(row=0, column=0, padx=10, pady=2)
left_instructions = Label(left_frame, text="1\n2\n2\n3\n4\n5\n6\n7\n8\n9\n", width=10)
left_instructions.grid(row=1, column=0, padx=10, pady=2)
right_frame = tk.Frame(root, width=200, height = 600, bg="yellow")
right_frame.grid(row=0, column=1, padx=10, pady=2)
button0 = button('Button0', {'width':20, 'height':1}, black)
button1 = button('Button1', {'width':20, 'height':1}, green)
button2 = button('Button2', {'width':20, 'height':1}, blue)
button0.grid(row=0, column=0)
button1.grid(row=0, column=1)
button2.grid(row=0, column=2)
bottom_frame = tk.Frame(right_frame, bg='red')
bottom_frame.grid(row=1, column=0, columnspan=3, padx=10, pady=2)
status = tk.Label(bottom_frame, width = 30, height = 10, takefocus=0)
status.grid(row=2, column=0, padx=10, pady=2)
root.mainloop() #start monitoring and updating the GUI
本作品采用《CC 协议》,转载必须注明作者和本文链接