我正在给我餐馆的员工做菜单测试。计划是让菜单项在“此处循环项”之间循环,此时它们选择正确的复选按钮(成分),然后单击“提交并继续按钮”。当他们点击submit按钮时,我首先需要读取复选按钮的on和off值,以确定他们选择了哪些项,然后将这些项与我在列表字典中定义的正确答案进行比较,然后清除所有的复选按钮,无论答案是错的还是对的,程序将继续运行,最终我将拥有一个结果屏幕,但现在我被困在如何读取复选按钮的打开和关闭值。我现在只是想打印一些精选的蔬菜,却想不出来。
我认为这与它们采用不同的方法以及它们是在一个循环中添加的事实有关?我不太确定,但我知道我的代码试图读取错误的东西,任何帮助将是贪婪的感激!
很抱歉问了这么长的问题,我只是觉得给你提供尽可能多的信息来理解我想做什么是有益的。。

from tkinter import *

class GUI(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.initUI()

    def initUI(self):
        self.grid()
        self.parent.title("Wahoos Menu Test")
        self.create_buttons()

    global count
    count = -1

    def create_buttons(self):
        for r in range(20):
            for c in range(14):
                Label(self, text='',
                    borderwidth=0).grid(row=r,column=c)
        B = Button(self, text ="Begin Exam", relief=RIDGE, fg="black", command= self.on_button_press).grid(row=19, column=7)
        L = Label(self, text="What comes in the following", fg="blue").grid(row=6, column=0)
        self.veg = ['Lettuce', 'Cabbage', 'Cheese', 'Ahee Rice', 'Brown Rice', 'Banzai Veg', 'Red Cabbage', 'Black Beans', 'Cajun White Beans']
        self.vegboxes = []
        self.opt = []
        c = 1
        for ve in self.veg:
            c +=1
            self.v = IntVar()
            self.vegboxes.append(self.v)
            vo = Checkbutton(self, text=ve, variable=self.v, onvalue=1, offvalue=0).grid(row=c, column=11, sticky=W)

    def on_button_press(self):
        global count
        count = count + 1
        menuItems = {'nft': ['cabbage', 'cheese', 'corn', 'nf', 'salsa'],
        'nckt': ['lettuce', 'cheese', 'corn', 'nck', 'salsa']}
        menu = ['blackened fish taco', 'wahoos chicken salad']
        if count == len(menu):
            C = Button(self, text ="           Your Done!          ", relief=RIDGE, fg="black").grid(row=19, column=7)
        else:
            m = Label(self, text=menu[count], fg="black").grid(row=7, column=0)
            C = Button(self, text ="Submit and Continue", relief=RIDGE, fg="black", command= self.read_checks).grid(row=19, column=7)
    def read_checks(self):
        for v in self.veg:
            if self.v == 1:
                print(self.veg[v])


def main():
    root = Tk()
    app = GUI(root)
    root.mainloop()

if __name__ == '__main__':
    main()

最佳答案

你可以创建一个字典,每个键都是Checkbutton的标签,
值为状态"Control Variable".
然后使用控制变量的get()方法检查状态,如下例所示。

import tkinter as tk

class GUI(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)

        self.buttonDic = {
        'Brown Rice':0,
        'Banzai Veg':0,
        'Red Cabbage':0,
        'Black Beans':0
        }

        for key in self.buttonDic:
            self.buttonDic[key] = tk.IntVar()
            aCheckButton = tk.Checkbutton(self, text=key,
                                            variable=self.buttonDic[key])
            aCheckButton.grid(sticky='w')

        submitButton = tk.Button(self, text="Submit",
                                        command=self.query_checkbuttons)
        submitButton.grid()

    def query_checkbuttons(self):
        for key, value in self.buttonDic.items():
            state = value.get()
            if state != 0:
                print(key)
                self.buttonDic[key].set(0)

gui = GUI()
gui.mainloop()

这种方法允许您使用一个字典创建和分析checkbutton。
注意for key, value in self.buttonDic.items():中items()的用法
需要防止:
ValueError: too many values to unpack
有关Checkbutton小部件及其变量的更多信息,请参见:here
我将包括我的第一次尝试
Checkbutton小部件的onvalueoffvalue
以防有人更了解控制变量。
import tkinter as tk

class GUI(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)

        self.bRiceV = tk.StringVar()
        bRice = tk.Checkbutton(self, text="Brown Rice",variable=self.bRiceV,
                                    onvalue="Brown Rice", offvalue="Off")
        bRice.pack()

        self.bVegV = tk.StringVar()
        bVeg = tk.Checkbutton(self, text="Banzai Veg",variable=self.bVegV,
                                    onvalue="Banzai Veg", offvalue="Off")
        bVeg.pack()

        self.varList = [self.bRiceV, self.bVegV]

        submitButton = tk.Button(self, text="Submit",
                                command=self.query_checkbuttons)
        submitButton.pack()

    def query_checkbuttons(self):
        for var in self.varList:
            value = var.get()
            if value != 'Off':
                print(value)
                var.set('Off')

gui = GUI()
gui.mainloop()

10-07 21:59