Tuesday, 15 July 2014

python - Trouble creating 2D defaulted radiobutton -



python - Trouble creating 2D defaulted radiobutton -

i'm trying create 2d grid of radiobuttons default values. grid may have many 256 rows 3 columns. have researched much , tried many options , best code have now. new python , tkinter additional suggestions solving dilema (and code writing) appreciated. maintain in mind need read each row of radio buttons determine 1 checked.

import tkinter tk tkinter import * class gui(frame): def __init__(self, parent): frame.__init__(self, parent) self.parent = parent self.pack() self.initui() def initui(self): # 5hz radio button should selected default self.freqs=['1hz','5hz','10hz'] self.numfreqs = len(self.freqs) #val , var set seek found defaults #every 3 vals = 1 var therfore middle val should = var # val 0,1,2 = var 1 , val 3,4,5 = var 4 , val 6,7,8 = var 7 # should allow 5hx radiobuttonto 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 plenty values 256 rows of 3 radiobuttons #for var = [1,4,7,10... val = [0,1,2, 3,4,5, 6,7,8... vars match vals on 5hz radiobutton #can't since creates the'many selected bug' #see http://stackoverflow.com/questions/5071559/tkinter-radio-button-initialization-bug #for in range(len(self.var)): #self.var[i] = intvar() 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 grouping of radios slection/clear of grouping #if value = variable 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()

not exclusively sure problem looking help on code, made couple quick changes should create bit more functional:

import tkinter tk tkinter import * class gui(frame): def __init__(self, parent): frame.__init__(self, parent) self.parent = parent self.pack() self.initui() def initui(self): # 5hz radio button should selected default self.freqs=['1hz','5hz','10hz'] self.numfreqs = len(self.freqs) #val , var set seek found defaults #every 3 vals = 1 var therfore middle val should = var # val 0,1,2 = var 1 , val 3,4,5 = var 4 , val 6,7,8 = var 7 # should allow 5hz radiobutton default selected self.val = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14] #you need hold on reference intvars, not # entire widgets. declaring each "var" individually 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 allow read values 3 3 i,lbl in enumerate(self.textlbls): #enumerate nice way avoid range(len(x)) tempvals = [next(valueiter) x in range(3)] #get next 3 values #make variable new checkbox (and append our list) self.checkbuttonvars.append(intvar(value=0)) #make variable new radiobutton grouping (and append our list) self.radiobuttonsvars.append(intvar(value=tempvals[1])) #make checkboxes, radiobuttons , label 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 button retrieve values, demo. self.printbutton = button(self,text='return values',command=self.printvals) self.printbutton.grid(row=i+1,column=8) def getradiovals(self,*args): #returns values in radio buttons list homecoming [x.get() x in self.radiobuttonsvars] def getcheckvals(self,*args): #returns values in checkboxes list homecoming [x.get() 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()

if give improve description of specific problems having, might possible address them.

also, note: trying display 256 radiobuttons vertically cause problems. far know, there no way create scrolled frame in standard tkinter, window end much taller screen.

python radio-button tkinter

No comments:

Post a Comment