2013-02-11 78 views
0

我正嘗試使用默認值創建單選按鈕的2D網格。網格可以有多達256行但總是3列。我研究了很多,並嘗試了很多選擇,這是我現在最好的代碼。很顯然,我是Python和Tkinter的新手,所以對解決我的困境(以及編寫代碼)的任何其他建議都非常感激。另外請記住,我將需要回讀每行單選按鈕以確定哪一個被選中。創建2D默認單選按鈕時出現問題

import Tkinter as tk 
from Tkinter import * 

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

    def initUI(self): 
     # the 5Hz radio button should be selected as default 
     self.freqs=['1Hz','5Hz','10Hz'] 
     self.numFreqs = len(self.freqs) 
     #val and var are set up to try to establish the defaults 
     #every 3 vals = 1 var therfore the middle val should = var 
     # val 0,1,2 = var 1 , val 3,4,5 = var 4 , val 6,7,8 = var 7 
     # this should allow the 5hx radiobuttonto be default selected 
     self.val = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14] 
     self.var=[1,4,7,10,13] 
     self.var[0] = IntVar() 
     self.var[1] = IntVar() 
     self.var[2] = IntVar() 
     self.var[3] = IntVar() 
     self.var[4] = IntVar() 
     self.cButtons = [] 
     self.rButtons1 = [] 
     self.rButtons2 = [] 
     self.rButtons3 = [] 

     self.textLbls = ['Choice 1', 'Choice 2', 'Choice 3','Choice 4','Choice 5'] 

     #build enough values for 256 rows of 3 radiobuttons 
     #For var = [1,4,7,10... val = [0,1,2, 3,4,5, 6,7,8... so vars match vals on the 5hz radiobutton 
     #Can't do this since it creates the'many already selected bug' 
     #See http://stackoverflow.com/questions/5071559/tkinter-radio-button-initialization-bug 
     #for i in range(len(self.var)): 
      #self.var[i] = IntVar() 

     for i in range(len(self.textLbls)): 
      temp = self.val[(self.numFreqs*i)+1] 
      temp1 = self.var[i] 
      self.cButtons.append(Checkbutton(self, text=self.textLbls[i], onvalue=1, offvalue=0)) 
      self.cButtons[i].grid(row=i, column=2, sticky = W) 
      Label(self, text='   Frequency:').grid(row=i, column=6) 
      #variable connects group of radios for slection/clear of group 
      #if value = variable this is default 
      self.rButtons1.append(Radiobutton(self, text=self.freqs[0], variable=self.var[i], value=self.val[(self.numFreqs*i)+0])) 
      self.rButtons1[i].grid(row=i, column=7, padx=5) 
      self.rButtons2.append(Radiobutton(self, text=self.freqs[1], variable=self.var[i], value=self.val[(self.numFreqs*i)+1])) 
      self.rButtons2[i].grid(row=i, column=8, padx=5) 
      self.rButtons3.append(Radiobutton(self, text=self.freqs[2], variable=self.var[i], value=self.val[(self.numFreqs*i)+2])) 
      self.rButtons3[i].grid(row=i, column=9, padx=5) 

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

if __name__ == '__main__': 
    main() 

回答

1

不完全知道你在找什麼問題,用於與該代碼的幫助,但我做了幾個快速的變化,應該使多一點功能:

import tkinter as tk 
from tkinter import * 

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

    def initUI(self): 
     # the 5Hz radio button should be selected as default 
     self.freqs=['1Hz','5Hz','10Hz'] 
     self.numFreqs = len(self.freqs) 
     #val and var are set up to try to establish the defaults 
     #every 3 vals = 1 var therfore the middle val should = var 
     # val 0,1,2 = var 1 , val 3,4,5 = var 4 , val 6,7,8 = var 7 
     # this should allow the 5hz radiobutton to be default selected 
     self.val = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14] 
     #You only really need to hold on to a reference to the IntVars, not the 
     # entire widgets. Declaring each "var" individually is also unneccesary 
     self.checkButtonVars = [] 
     self.radioButtonsVars = [] 

     self.textLbls = ['Choice 1', 'Choice 2', 'Choice 3','Choice 4','Choice 5'] 
     #(your comment block) 

     valueIter = iter(self.val) #This will let us read the values 3 by 3 
     for i,lbl in enumerate(self.textLbls):  #enumerate is a nice way to avoid range(len(x)) 
      tempVals = [next(valueIter) for x in range(3)] #get the next 3 values 
      #make a variable for the new CheckBox (and append it to our list) 
      self.checkButtonVars.append(IntVar(value=0)) 
      #make a variable for the new RadioButton group (and append it to our list) 
      self.radioButtonsVars.append(IntVar(value=tempVals[1])) 

      #Make the checkboxes, radiobuttons and label for the line. 
      Checkbutton(self, text=lbl, variable=self.checkButtonVars[i]).grid(row=i, column=2, sticky = W) 
      Label(self, text='   Frequency:').grid(row=i, column=6) 
      Radiobutton(self, text=self.freqs[0], variable=self.radioButtonsVars[i], value=tempVals[0]).grid(row=i, column=7, padx=5) 
      Radiobutton(self, text=self.freqs[1], variable=self.radioButtonsVars[i], value=tempVals[1]).grid(row=i, column=8, padx=5) 
      Radiobutton(self, text=self.freqs[2], variable=self.radioButtonsVars[i], value=tempVals[2]).grid(row=i, column=9, padx=5) 

     #I added a button to retrieve the values, this is just as a demo. 
     self.printButton = Button(self,text='Return values',command=self.printVals) 
     self.printButton.grid(row=i+1,column=8) 


    def getRadioVals(self,*args): 
     #returns the values in the radio buttons as a list 
     return [x.get() for x in self.radioButtonsVars] 

    def getCheckVals(self,*args): 
     #returns the values in the checkboxes as a list 
     return [x.get() for x in self.checkButtonVars] 

    def printVals(self,*args): 
     print('Radio Buttons: %s' %(self.getRadioVals())) 
     print('Check Buttons: %s' %(self.getCheckVals())) 


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

if __name__ == '__main__': 
    main() 

如果你可以給一個更好地描述你所遇到的具體問題,有可能解決它們。

另外,就像一個註釋:試圖垂直顯示256個單選按鈕可能會導致一些問題。據我所知,沒有辦法在標準tkinter中創建滾動框架,所以你的窗口將比屏幕更高。

+1

您可以創建一個框架和畫布的滾動框架。網上有幾個例子。 – 2013-02-13 02:08:19