目前我有一个gui tkinter窗口可用于某些Radiobuttons。这些单选按钮通过列表显示在窗口中(i表示范围),每个按钮只能检查一次,1到多次。基本上单选按钮工作正常。
我也希望获得相同的结果,除了使用复选按钮。所以我可以一次选择多个选项,这意味着要么有1对多,要么有多对多。
单选按钮的值为value = i,但我不知道如何使用checkbuttons执行此操作。我知道他们需要一种不同的价值函数。
我的代码如下:
解释
breads = ["White", "Wheat"] #"Multigrain" - Can be added to list to see GUI respond to new ingredients
cheeses = ["Swiss", "Cheddar"] #Edam - Can be added...
salads = ["Lettuce", "Tomato"] #Capsicum, Cucumber - Can be added...
meats = ["Bacon", "Salami"] # Bacon - Can be added...
sauces = ["Mayo"] #Honey Mustard - Can be added...
标签和按钮:
Label(self,
text = "Bread Type ($2):",
).grid(row = 10, column = 0, sticky = W)
self.breads_btns = []
for i in range(len(breads)):
rb = Radiobutton(self, variable = self.breads_var, value = i, anchor = W, text = breads[i], command=self.show_sentence)
self.breads_btns.append(rb)
rb.grid(row =(i + 11), column = 0, sticky = W)
Label(self,
text = "Cheeses ($1):",
).grid(row = 22, column = 0, sticky = W)
self.cheeses_btns = []
for i in range(len(cheeses)):
rb = Radiobutton(self, variable = self.cheeses_var, value = i, anchor = W, text = cheeses[i], command=self.show_sentence)
self.cheeses_btns.append(rb)
rb.grid(row =(i + 23), column = 0, sticky = W)
Label(self,
text = "Salads ($2):",
).grid(row = 33, column = 0, sticky = W)
self.salads_btns = []
for i in range(len(salads)):
cb = Checkbutton(self, variable = self.salads_var, anchor = W, text = salads[i], command=self.show_sentence)
self.salads_btns.append(cb)
cb.grid(row =(i + 34), column = 0, sticky = W)
Label(self,
text = "Meats ($4):",
).grid(row = 44, column = 0, sticky = W)
self.meats_btns = []
for i in range(len(meats)):
cb = Checkbutton(self, variable = self.meats_var, anchor = W, text = meats[i], command=self.show_sentence)
self.meats_btns.append(cb)
cb.grid(row =(i + 45), column = 0, sticky = W)
Label(self,
text = "Sauces ($1):",
).grid(row = 55, column = 0, sticky = W)
self.sauces_btns = []
for i in range(len(sauces)):
rb = Radiobutton(self, variable = self.sauces_var, value = i, anchor = W, text = sauces[i], command=self.show_sentence)
self.sauces_btns.append(rb)
rb.grid(row =(i + 56), column = 0, sticky = W)
输出: Image of Window
总体而言,任何人都可以帮助我完成检查按钮值所需的功能。此外,我需要它们能够在选中时显示在文本框中(我计划实施的未来代码)。