I was hoping someone could help me with something. I would like to make an Tkinter app that asks for a number then uses that number to draw the correct number of Labels and Entrys.
Here's basic brainstorm of what I'm trying to do (I know this is wrong)
from Tkinter import *
root = Tk()
numlines = IntVar()
Label(root, text='Number Of Lines').grid(row=0, column=0) #this is to always stay
Entry(root, textvariable=numlines).grid(row=0, column=1) #this is to stay always stay
Button(root, text='Apply Number', command=apply).grid(row=1, column=1)
def apply():
# this part to draw as a block based in numline(if munlines 5 then draw these two widget 5 times on 5 rows)
Label(root, text='Line 1').grid(row=2, column=0)# this part to draw as a block based in numline(if munlines 5 then draw these two widget 5 times)
Entry(root, textvariable=numlines).grid(row=2, column=1)
root.mainloop()
Realistically all Tkinter applications should be put inside of a class. In addition it is also general a bad idea for use import *
from any package as you could have override issues with unknown values that you imported. As such, the following example is inside of a class and should give you an idea of how this would look. I believe this is what you're looking for:
import Tkinter as Tk
class App(Tk.Frame):
def __init__(self, master, *args, **kwargs):
Tk.Frame.__init__(self, *args, **kwargs)
self.existing_lines = 2
self.entry_lines = Tk.IntVar()
Tk.Label(self, text="Number Of Lines").grid(row=0, column=0)
Tk.Entry(self, textvariable=self.entry_lines).grid(row=0, column=1)
Tk.Button(self, text="Apply Number", command=self.add_rows).grid(row=1, column=1)
def add_rows(self):
for row in xrange(self.existing_lines, self.existing_lines+self.entry_lines.get()):
Tk.Label(self, text="Line %i" % (row-1)).grid(row=row, column=0)
Tk.Entry(self, textvariable=self.entry_lines).grid(row=row, column=1)
self.existing_lines+= 1
if __name__ == "__main__":
root = Tk.Tk()
App(root).pack()
root.mainloop()