Checkbutton: 多選按鈕
一、基本使用
程式效果,列印選中的菜名,
代碼如下:(總感覺我獲取菜名的方法有點累贅)
# coding:utf8 from tkinter import * class App: def __init__(self, master): frame = Frame(master).pack() v1 = IntVar() v2 = IntVar() v3 = IntVar() v4 = IntVar() # variable如果選中了,值為1,如果未選中值為0. Checkbutton(frame, text="青菜", variable=v1).pack() Checkbutton(frame, text="白菜", variable=v2).pack() Checkbutton(frame, text="菠菜", variable=v3).pack() Checkbutton(frame, text="黃瓜", variable=v4).pack() # 觸發事件 def show(): list1 = [v1.get(), v2.get(), v3.get(), v4.get()] list2 = ["青菜", "白菜", "菠菜", "黃瓜"] print("您選中的菜是:\n", end="") for k in range(len(list1)): if list1[k]: print(list2[k], end="\n") return True Button(frame, text="點擊查看選中的菜", command=show).pack() root = Tk() win = App(root) root.mainloop()
代碼2:和單選按鈕一樣,這個也可以迭代生成,自行研究吧,
讀書和健身總有一個在路上
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/149792.html
標籤:Python
