import tkinter as tk # 1 imports
from tkinter import ttk
from tkinter import scrolledtext as st
win = tk.Tk() # 2 Create instance
win.title("Python GUI") # 3 Add a title
aLabel = ttk.Label(win, text="输入文本:")
aLabel.grid(column=0, row=0) # 5
ttk.Label(win, text="choose a number").grid(column=1, row=0)
number = tk.StringVar()
numberChosen = ttk.Combobox(win, width=12, textvariable=number, state="readonly")
numberChosen.grid(column=1, row=1)
numberChosen["values"] = (1, 2, 3, 4, 5, 6, 12)
numberChosen.current(3)
def clickMe():
action.configure(text="hello " + name.get() + "-" + number.get())
# aLabel.configure(foreground="red")
action = ttk.Button(win, text="点我", command=clickMe)
action.grid(column=3, row=1)
name = tk.StringVar()
nameEntered = ttk.Entry(win, width=12, textvariable=name)
nameEntered.grid(column=0, row=1)
nameEntered.focus() # Place cursor into name Entry
chVarDis = tk.IntVar() # 2
check1 = tk.Checkbutton(win, text="Disabled", variable=chVarDis, state='disabled') # 3
check1.select() # 4
check1.grid(column=0, row=4, sticky=tk.W) # 5
chVarUn = tk.IntVar() # 6
check2 = tk.Checkbutton(win, text="UnChecked", variable=chVarUn)
check2.deselect() # 8
check2.grid(column=1, row=4, sticky=tk.W) # 9
chVarEn = tk.IntVar() # 10
check3 = tk.Checkbutton(win, text="Enabled", variable=chVarEn)
check3.select() # 12
check3.grid(column=2, row=4, sticky=tk.W) # 13
COLOR1 = "DarkSalmon" #2
COLOR2 = "honeydew" #3
COLOR3 = "AliceBlue" #4
radVar = tk.IntVar()
def radCall(): #6
radSel=radVar.get()
if radSel == 1: win.configure(background=COLOR1)
elif radSel == 2: win.configure(background=COLOR2)
elif radSel == 3: win.configure(background=COLOR3)
rad1 = tk.Radiobutton(win, text=COLOR1, variable=radVar, value=1, command=radCall) # 9
rad1.grid(column=0, row=5, sticky=tk.W) # 10
rad2 = tk.Radiobutton(win, text=COLOR2, variable=radVar, value=2, command=radCall) # 11
rad2.grid(column=1, row=5, sticky=tk.W) # 12
rad3 = tk.Radiobutton(win, text=COLOR3, variable=radVar, value=3, command=radCall) # 13
rad3.grid(column=2, row=5, sticky=tk.W) # 14
scrollW = 30
scrollH = 3
scroll = st.ScrolledText(win,width = scrollW, height=scrollH,wrap= tk.WORD)
scroll.grid(column=0, columnspan=3)
scroll.focus()
win.mainloop() # 5 Start GUI