當我們改變一個組件後,其他組件一起變化怎麼做呢?下面是一個例子
from tkinter import Tk, Checkbutton, Label from tkinter import StringVar, IntVar root = Tk() text = StringVar() text.set('old') status = IntVar() def change(): if status.get() == 1: # if clicked text.set('new') else: text.set('old') cb = Checkbutton(root, variable=status, command=change) lb = Label(root, textvariable=text) cb.pack() lb.pack() root.mainloop()
注意這些代碼是python3.6下,如果是python2.7 應該用Tkinter模塊