[Tkinter] Python Newbie - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] Python Newbie (/thread-26737.html) |
Python Newbie - MartyH - May-11-2020 Hi guys, I hope you can helpppppp..... I have a program which is supposed to get the new values of 9 variables. It initializes [init()] by arranging the numbers similar to a spreadsheet, and putting varaibles in the "textvariable" as var00, var01, var02 etc Then it give values to each by attempting to use the .get method of the Control variables. But my program does not work. Thanks Marty import tkinter as tk from tkinter import * root = tk.Tk() def give_values(): # row 0 var00 = StringVar().get() var01 = StringVar().get() var02 = StringVar().get() # row 1 var10 = StringVar().get() var11 = StringVar().get() var12 = StringVar().get() # row 2 var20 = StringVar().get() var21 = StringVar().get() var22 = StringVar().get() v = (var00, var01, var02, var10, var11, var12, var20, var21, var22) print(v) def init(): for i in range(3): for j in range(3): frame = tk.Frame( master=root, relief=tk.RAISED, width=2, borderwidth=1 ) frame.grid(row=i, column=j) entry = tk.Entry(master=frame, text=f"Row {i}\nColumn {j}", textvariable="var" + str(i) + str(j)) entry.pack() give_values() init() give_values() # b1 = Button(root, text="Done", width=10).grid(row=0, column=10, rowspan=10) # command=done) b2 = Button(root, text="Enter", width=10, command=give_values).grid(row=1, column=10, rowspan=10) # command=enter()) root.mainloop() RE: Python Newbie - MartyH - May-12-2020 Thanks for changing the location of my post to the correct one! I am ready to throw in the towel -- i've tried everything that I know -- I'm using python 3.7.3 Is there anything else I can add? Thanks marty RE: Python Newbie - deanhystad - May-12-2020 A couple of problems here. You are creating new StringVars every time you call give_values so there is no way these can be bound to an Entry. Even if you only created the StringVar's once, their attribute names (varXX) do not exist outside the function give_values, so you would have no way to get these variables and bind them to the entries. Finally, you cannot set textvariable="var00" and expect the Entry constructor to figure out which namespace to use to search for attribute "var00" and get a StringVar. import tkinter as tk from tkinter import * root = tk.Tk() variables = [] def give_values(): return [var.get() for var in variables] # Makes a list calling get for each StringVar in variables def init(): for i in range(3): for j in range(3): frame = tk.Frame( master=root, relief=tk.RAISED, width=2, borderwidth=1 ) frame.grid(row=i, column=j) var = StringVar() variables.append(var) entry = tk.Entry(master=frame, text=f"Row {i}\nColumn {j}", textvariable=var) entry.pack() init() # b1 = Button(root, text="Done", width=10).grid(row=0, column=10, rowspan=10) # command=done) b2 = Button(root, text="Enter", width=10, command=give_values).grid(row=1, column=10, rowspan=10) # command=enter()) root.mainloop() RE: Python Newbie - MartyH - May-13-2020 that's great and i am part of the way there -- I really appreciate that! I still have 2 questions: 1) I really didn't understand StringVar, and I am not sure that really do now!! Perhaps you can expand on it! 2) Another question --> so now I've got a list of <tkinter.StringVar object at 0x00F68290>, ... etc how does this translate into the value for the variable? I have tried all sorts of machinations of [variables] and can't seem to get any satifaction. Thanks, Marty RE: Python Newbie - buran - May-13-2020 this is old, bit outdated tutorial on tkinter, but still one of the best: https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/index.html this is the part about control variables: https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/control-variables.html RE: Python Newbie - deanhystad - May-13-2020 A short example using most of your provided code: import tkinter as tk from tkinter import * root = tk.Tk() variables = [] def increment(): for i in range(len(variables)-1): variables[i].set(variables[i+1].get()) variables[-1].set(variables[-1].get() + 1) def init(): for i in range(3): for j in range(3): frame = tk.Frame(master=root, relief=tk.RAISED) frame.grid(row=i, column=j) var = IntVar() variables.append(var) entry = tk.Entry(master=frame, width=3, textvariable=var) entry.pack() b2 = Button(root, text="Increment", command=increment) \ .grid(row=3, column=0, columnspan=3) init() root.mainloop() RE: Python Newbie - MartyH - May-13-2020 Hey that really helps!! I've just discovered how line 19 related to the "Increment" method (by the line directly above it) and I have to look at carefully to discover some other facts... Thanks, Marty |