如何使用 Tkinter 和函数找到选中了哪个复选按钮?
How can you find which checkbutton is selected using Tkinter and a function?
我在这里尝试创建三种水果的列表,苹果、石榴和香蕉,并要求用户 select 他们最喜欢的水果。所以我定义了一个函数 print_choice ,每次任何复选框被 selected 时调用,并且定义了一个数组 fruits 当复选框被 selected 时将相应的水果附加到数组.
我在函数内部定义了数组而不是全局声明,因为不应将值相加两次。但是,我无法选择打印在标签框中,该标签框应该显示用户 select 编辑的水果列表。你能帮我找出我在哪里犯了错误吗?我该如何展示水果 selected ?
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()
这是我得到的输出:
每当我尝试 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()
在这种情况下,您几乎可以添加任意数量的复选框,而您在硬编码代码中唯一要做的就是像这样 var_name = Choice('checkbutton name')
启动选择 class,其中var_name 是您想要的任何变量名称,复选框名称是您想要的任何名称(只是为了澄清)。
您不需要很多 if-else 语句就可以知道用户喜欢哪种水果。
我将向您展示实现相同目的的另外两种方法。
第一种方法是创建一个以水果名称为键、控制变量为值的字典。
现在遍历项目检查控制变量的值是否为 1.If 它是 1 然后附加到水果列表。
第一种方式(使用字典):
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()
我在这里尝试创建三种水果的列表,苹果、石榴和香蕉,并要求用户 select 他们最喜欢的水果。所以我定义了一个函数 print_choice ,每次任何复选框被 selected 时调用,并且定义了一个数组 fruits 当复选框被 selected 时将相应的水果附加到数组. 我在函数内部定义了数组而不是全局声明,因为不应将值相加两次。但是,我无法选择打印在标签框中,该标签框应该显示用户 select 编辑的水果列表。你能帮我找出我在哪里犯了错误吗?我该如何展示水果 selected ?
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()
这是我得到的输出:
每当我尝试 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()
在这种情况下,您几乎可以添加任意数量的复选框,而您在硬编码代码中唯一要做的就是像这样 var_name = Choice('checkbutton name')
启动选择 class,其中var_name 是您想要的任何变量名称,复选框名称是您想要的任何名称(只是为了澄清)。
您不需要很多 if-else 语句就可以知道用户喜欢哪种水果。
我将向您展示实现相同目的的另外两种方法。
第一种方法是创建一个以水果名称为键、控制变量为值的字典。 现在遍历项目检查控制变量的值是否为 1.If 它是 1 然后附加到水果列表。
第一种方式(使用字典):
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()