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()
您可以創建一個框架和畫布的滾動框架。網上有幾個例子。 – 2013-02-13 02:08:19