1. 程式人生 > >Python中tkinter中控件的使用(2.多選框)

Python中tkinter中控件的使用(2.多選框)

ron \n oop += ole ext 選擇 返回 綁定

#CheckButton多選框
import tkinter

win = tkinter.Tk()
win.title("CheckButton多選框")
win.geometry("800x600+600+100")

def updata():
message=""
if hobby1.get()==True:#如果check1被選擇,那麽hobby1被返回True
message+="money\n"
if hobby2.get()==True:
message+="power\n"
if hobby3.get()==True:
message+="fine foods"

text.delete(0.0,tkinter.END)#清空文本中所有內容,從0開始,到最後
text.insert(tkinter.INSERT,message)#在文本框中顯示文本
hobby1=tkinter.BooleanVar()#綁定選擇時的動作
check1=tkinter.Checkbutton(win,text="money",
variable=hobby1,command=updata)
check1.pack()
hobby2=tkinter.BooleanVar()#
check2=tkinter.Checkbutton(win,text="power",variable=hobby2

,command=updata)
check2.pack()
hobby3=tkinter.BooleanVar()
check3=tkinter.Checkbutton(win,text="fine foods",variable=hobby3,
command=updata)
check3.pack()


text=tkinter.Text(win,width=50,height=5)#顯示文本框
text.pack()

win.mainloop()

Python中tkinter中控件的使用(2.多選框)