![](/img/trans.png)
[英]How to display GUI message indicating which checkbutton(s) and radiobutton have been selected in tkinter in Python?
[英]How can you find which checkbutton is selected using Tkinter and a function?
在這里,我試圖創建一個包含蘋果、石榴和香蕉這三種水果的列表,並要求用戶 select 是他們最喜歡的水果。 因此,我正在定義ZC1C425268E68385D1AB5074C17A94F14F14Z PRINT_CHOICE,每次選擇任何復選框時都稱為,並定義了在選擇復選框時將相應水果附加到數組的數組果實。 我在 function 中定義了數組,而不是全局聲明,因為不應將值添加兩次。 但是,我無法選擇在 label 框中打印,該框應該顯示用戶選擇的水果列表。 你能幫我找出我在哪里犯了錯誤,我怎樣才能顯示選擇的水果?
import tkinter as tk
from tkinter import *
parent = tk.Tk()
parent.title("My favorite fruits")
l= tk.Label(parent, background="yellow", text="empty", width="30")
l.pack()
w = tk.Label(parent , text ="Select your favorite fruits!", bg="pink", fg = "white")
txt = "you love "
def print_choice():
fruits=list()
global txt
flag=0
while flag==0:
if checkvar1.get()==1 and checkvar2.get==0 and checkvar3.get()==0:
fruits.append("Apple")
flag=1
break
elif checkvar2.get()==1 and checkvar1.get()==0 and checkvar3.get()==0:
fruits.append("Pomegranate")
flag=1
break
elif checkvar3.get()==1 and checkvar1.get()==0 and checkvar2.get()==0:
fruits.append("Banana")
flag=1
break
elif checkvar3.get()==1 and checkvar1.get()==1 and checkvar2.get()==0:
fruits.append("Banana")
fruits.append("Apple")
flag=1
break
elif checkvar3.get()==1 and checkvar1.get()==0 and checkvar2.get()==1:
fruits.append("Banana")
fruits.append("Pomegranate")
flag=1
break
elif checkvar3.get()==0 and checkvar1.get()==1 and checkvar2.get()==1:
fruits.append("Apple")
fruits.append("Pomegranate")
flag=1
break
elif checkvar3.get()==1 and checkvar1.get()==1 and checkvar2.get()==1:
fruits.append("Banana")
fruits.append("Apple")
fruits.append("Pomegranate")
flag=1
break
else :
fruits.append(" ")
for fruit in fruits:
txt += fruit+" "
l.config(text=txt)
checkvar1= tk.IntVar()
checkvar2 = tk.IntVar()
checkvar3 = tk.IntVar()
c1 = tk.Checkbutton(parent, text ="Apple", variable = checkvar1, onvalue=1, offvalue=0,height = 5, width =20, bg="pink", activebackground="yellow", activeforeground="orange", command=print_choice)
c1.pack()
c2 = tk.Checkbutton(parent, text ="Pomegranate", variable = checkvar2, onvalue=1, offvalue=0,height = 5, width =20, bg="pink", command=print_choice)
c2.pack()
c3 = tk.Checkbutton(parent, text ="Banana", variable = checkvar3, onvalue=1, offvalue=0,height = 5, width =20, bg="pink", command=print_choice)
c3.pack()
parent.mainloop()
這就是我作為 output 得到的:
每當我嘗試 select 復選框時,Python 停止響應。 我有可能在某處遇到無限循環嗎?
因此,這是您可能希望如何執行此操作的示例:
from tkinter import Tk, Checkbutton, IntVar
root = Tk()
selected = []
class Choice:
def __init__(self, text):
self.text = text
self.state = IntVar()
self.checkbutton = Checkbutton(root, text=self.text, command=self.check,
variable=self.state, onvalue=1, offvalue=0)
self.checkbutton.pack()
def check(self):
state = self.state.get()
if state == 1:
selected.append(self.text)
if state == 0:
selected.remove(self.text)
print(selected)
c1 = Choice('Apple')
c2 = Choice('Orange')
c3 = Choice('Pear')
root.mainloop()
在這種情況下,您可以根據需要添加任意數量的復選框,並且您在硬編碼代碼中唯一要做的就是像這樣啟動 Choice class var_name = Choice('checkbutton name')
其中 var_name 是任何變量名您想要,並且檢查按鈕名稱是您想要的任何名稱(只是為了澄清)。
您不需要很多 if-else 語句就可以知道用戶喜歡哪種水果。
我將向您展示實現同一目標的另外兩種方法。
第一種方法是創建一個字典,其中水果名稱作為鍵,控制變量作為值。 現在遍歷檢查控制變量的值是否為 1 的項目。如果為 1,則 append 到水果列表。
第一種方式(使用字典):
import tkinter as tk
parent = tk.Tk()
parent.title("My favorite fruits")
l= tk.Label(parent, background="yellow", text="empty", width="30")
l.pack()
w = tk.Label(parent , text ="Select your favorite fruits!", bg="pink", fg = "white")
txt = "you love "
def print_choice():
global fruit_lst
for fruit, checked in fruits_dict.items():
if checked.get() and fruit not in fruit_lst:
print(fruit)
fruit_lst.append(fruit)
if fruit in fruit_lst and not checked.get():
fruit_lst.remove(fruit)
l.config(text=txt+' ,'.join(fruit_lst))
checkvar1= tk.IntVar()
checkvar2 = tk.IntVar()
checkvar3 = tk.IntVar()
fruits_dict = {"Apple": checkvar1, "Pomegranate": checkvar2, "Banana": checkvar3}
fruit_lst = []
c1 = tk.Checkbutton(parent, text ="Apple", variable = checkvar1, onvalue=1, offvalue=0,height = 5, width =20, bg="pink", activebackground="yellow", activeforeground="orange", command=print_choice)
c1.pack()
c2 = tk.Checkbutton(parent, text ="Pomegranate", variable = checkvar2, onvalue=1, offvalue=0,height = 5, width =20, bg="pink", command=print_choice)
c2.pack()
c3 = tk.Checkbutton(parent, text ="Banana", variable = checkvar3, onvalue=1, offvalue=0,height = 5, width =20, bg="pink", command=print_choice)
c3.pack()
parent.mainloop()
您可以使用以下代碼避免字典和循環:
import tkinter as tk
parent = tk.Tk()
parent.title("My favorite fruits")
l= tk.Label(parent, background="yellow", text="empty", width="30")
l.pack()
w = tk.Label(parent , text ="Select your favorite fruits!", bg="pink", fg = "white")
txt = "you love "
def current(x, var):
global fruit_lst
if var.get():
fruit_lst.append(x['text'])
else:
try:
fruit_lst.remove(x['text'])
except ValueError:
pass
print(fruit_lst)
checkvar1= tk.IntVar()
checkvar2 = tk.IntVar()
checkvar3 = tk.IntVar()
fruit_lst = []
c1 = tk.Checkbutton(parent, text ="Apple", variable = checkvar1, onvalue=1, offvalue=0)
c1.config(command = lambda x=c1, var=checkvar1: current(x, var))
c1.pack()
c2 = tk.Checkbutton(parent, text ="Pomegranate", variable = checkvar2, onvalue=1, offvalue=0)
c2.config(command = lambda x=c2, var=checkvar2: current(x, var))
c2.pack()
c3 = tk.Checkbutton(parent, text ="Banana", variable = checkvar3, onvalue=1, offvalue=0)
c3.config(command = lambda x=c3, var=checkvar3: current(x, var))
c3.pack()
parent.mainloop()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.