from tkinter import *
root = Tk()
root.title("tkinter 的元件 - by 傻眼貓咪 - FishC")
labelFrame = [
LabelFrame(root, text = "Button", padx = 40, pady = 40), LabelFrame(root, text = "Canvas", padx = 40, pady = 40), LabelFrame(root, text = "Checkbutton"),
LabelFrame(root, text = "Entry", padx = 40, pady = 40), LabelFrame(root, bg = "grey", text = "Frame", padx = 40, pady = 40), LabelFrame(root, text = "Label", padx = 40, pady = 40),
LabelFrame(root, text = "Listbox", padx = 40, pady = 40), LabelFrame(root, text = "Menubutton", padx = 40, pady = 40), LabelFrame(root, text = "Message", padx = 40, pady = 40),
LabelFrame(root, text = "Radiobutton", padx = 40, pady = 40), LabelFrame(root, text = "Scale", padx = 40, pady = 40),
LabelFrame(root, text = "Scrollbar", padx = 40, pady = 40), LabelFrame(root, text = "Spinbox", padx = 40, pady = 40),
LabelFrame(root, text = "Text", padx = 40, pady = 40), LabelFrame(root, text = "PanedWindow", padx = 40, pady = 40, width = 40, height = 40, bg = "#FFFFFF")
]
for n, frame in enumerate(labelFrame):
r, c = divmod(n, 5)
frame.grid(row = r, column = c)
buttons = [Button(labelFrame[0], text = str(n + 1), padx = 15, pady = 15) for n in range(9)]
for n, button in enumerate(buttons):
r, c = divmod(n, 3)
buttons[n].grid(row = r, column = c)
canvas = Canvas(labelFrame[1], bg = "pink", width = 20, height = 20).pack()
checkButtons = [Checkbutton(labelFrame[2], text = chr(n + 65)) for n in range(5)]
for n, button in enumerate(checkButtons):
checkButtons[n].grid(row = n)
entry = Entry(labelFrame[3], borderwidth = 5).pack()
frame = Frame(labelFrame[4], bg = "pink", width = 25, height = 25).pack()
labels = [Label(labelFrame[5], text = f"this is Label {n + 1}") for n in range(5)]
for n, button in enumerate(labels):
labels[n].grid(row = n)
listbox = Listbox(labelFrame[6])
arr = ["one", "two", "three", "four", "five"]
for n, word in enumerate(arr):
listbox.insert(n, word)
listbox.pack()
menubutton = Menubutton(labelFrame[7], text = "this is menu", relief = RAISED)
menubutton.menu = Menu(menubutton)
menubutton["menu"] = menubutton.menu
var = [IntVar() for _ in range(5)]
for n, word in enumerate(arr):
menubutton.menu.add_checkbutton(label = word, variable = var[n])
menubutton.pack()
message = Message(labelFrame[8], text = "this is a message").pack()
v1 = StringVar(labelFrame[9], "R1")
v2 = StringVar(labelFrame[9], "R2")
valuesA = {f"RadioButton {n}" : str(n) for n in range(1, 6)}
valuesB = {f"RadioButton {chr(n + 65)}" : str(n + 1) for n in range(3)}
for n, (txt, val) in enumerate(valuesA.items()):
Radiobutton(labelFrame[9], text = txt, variable = v1, value = val, indicator = 0).grid(row = n, column = 0)
for n, (txt, val) in enumerate(valuesB.items()):
Radiobutton(labelFrame[9], text = txt, variable = v2, value = val).grid(row = n, column = 1)
scales = [Scale(labelFrame[10], orient = HORIZONTAL), Scale(labelFrame[10])]
for n, scale in enumerate(scales):
scale.grid(row = n)
scrollbar = Scrollbar(labelFrame[11])
scrollbar.pack(side = RIGHT, fill = Y)
arr = Listbox(labelFrame[11], yscrollcommand = scrollbar.set)
for n in range(100):
arr.insert(END, f"This is line {n}")
arr.pack(side = LEFT, fill = BOTH)
scrollbar.config(command = arr.yview)
spinbox = Spinbox(labelFrame[12], from_= 0, to = 100).pack()
text = Text(labelFrame[13], width = 10, height = 7)
text.insert(INSERT, "A B C D E F G H I J K L M ")
text.insert(END, "N O P Q R S T U V W X Y Z")
text.pack()
text.tag_add("posA", "1.0", "1.4")
text.tag_add("posB", "1.8", "1.13")
text.tag_config("posA", background = "yellow", foreground = "blue")
text.tag_config("posB", background = "black", foreground = "green")
panelwindow = PanedWindow(labelFrame[14], bg = "#000000", width = 20, height = 20).pack()
root.mainloop()