2012-04-10 197 views
2

我正在爲我的餐廳的員工進行菜單測試。該計劃是菜單項來遍歷「環路項目在這裏」此時,他們選擇了正確的checkbuttons(成分),然後點擊「提交併繼續按鈕」。當他們點擊提交按鈕時,我首先需要讀取檢查按鈕的開關值,以確定他們選擇了哪些項目,然後將這些值與我在列表字典中定義的正確答案進行比較,然後清除所有檢查按鈕以及是否答案是錯誤的或正確的程序將繼續,我最終會有一個結果屏幕,但現在我堅持如何讀取開關值的檢查按鈕。我只是試圖打印選定的蔬菜,現在無法弄清楚。Python 3.2 tkinter通過for循環添加讀取checkbutton值

我認爲這與它們在不同方法中的事實以及它們被添加到循環中的事實有關?我不確定,但我知道我的代碼正在嘗試閱讀錯誤的東西,任何幫助都會被感謝!

對不起,冗長的問題,我只是認爲這將是有益的給你儘可能多的信息儘可能地瞭解我試圖做...

enter image description here

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() 
+0

附註:類的好處之一是,你可以在'__init__'中插入類似self.count的東西,從而避免使用全局關鍵字。 – 2012-04-10 12:25:17

回答

4

您可以創建一個字典,並且每個鍵都是Checkbutton的標籤
並且其值爲狀態"Control Variable".
然後,您將使用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() 

該方法允許您使用一個字典創建並分析Checkbuttons。
注意在for key, value in self.buttonDic.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() 
+0

啊,非常感謝你!令人沮喪的是,我早些時候曾經有過類似的東西,我試圖使用它,但無法得到它的確定。對此,我真的非常感激。現在開始研究如何存儲它們,以便我可以將它們與我的列表字典進行比較...... – crenfro 2012-04-10 13:08:25