class - Use loop to create variables python 3 -


intro: using tkinter module create 2 windows, each separate classes. want take integer entered on first window, , use create number of labels , entries in second window.

i.e.

import tkinter  class firstwindow:     def __init__(self):         self.firstmaster = tkinter.tk()         self.topframe = tkinter.frame(self.numbermaster)         ...         self.numentry = tkinter.entry(self.topframe, width=10)         ...         self.averagebutton = tkinter.button(self.bottomframe, text='next', command=self.nextstep)         ...         ...         tkinter.mainloop()      def nextstep(self):         self.numberitems = int(self.numentry.get())         self.average = averagergui(self.numbertests)  class averagergui:     def __init__(self, numtests):         self.secondmaster = tkinter.tk()         self.topframe = tkinter.frame(self.secondmaster)          number in range(1, numtests):             self.frame'number' = tkinter.frame(self.secondmaster) 

i know not work; included illustrate want do: create number of frames dependent on numtests parameter.

i thought of using list, problem because not know how convert strings names of variables:

varlist = [] number in range(1, numbertests):     label = str(number)     var = 'frame' + label     varlist.append(var) 

any thoughts?

if use list, there no need generate names:

self.frames = [] number in range(numtests):     self.frames.append(tkinter.frame(self.secondmaster)) 

you can simplify using list comprehension:

self.frames = [tkinter.frame(self.secondmaster) _ in in range(numtests)] 

now can access each frame index:

self.frames[0] self.frames[1] 

or loop through them:

for frame in self.frames:     # frame 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -