I have a tkinter subwindow and i am trying to call a function and use a variable to add to the number of rows of elements. When execute the button, it says that the variable is referenced before assignment. Not understanding why.
The variable is ‘plant_rows’
try to excuse my horrible programming as much as possible as im still learning.
def open_plant_window():
plant_rows = 1
def add_another_plant(window):
plant_rows = plant_rows + 1
rows_and_cols = window.grid_size()
print(rows_and_cols)
new_name = 'p' + str(plant_rows) + '_name'
print(new_name)
Entry(window).grid(row=plant_rows, column=0)
plant_window = Toplevel()
plant_window.title('Plant Selection')
plant_window.geometry('500x500')
plant_window_title = Label(plant_window, text='Add Plants').grid(row=0)
add_row = Button(plant_window, text='Add Another Plant', command=lambda: add_another_plant(plant_window)).grid(row=4)
1 Like
i think i figured it out. I needed to use IntVar’s so that i could get and set values without rerendering the window.
At least thats what i think at the moment
1 Like
I’ve not tried your code but, if plant_rows is the variable you have two options.
make the variable global or 2. pass it as an argument to the inner functions.
Here is an example of creating multiple toplevel windows
import tkinter as tk
class AnotherWindow:
number = 1
def __init__(self):
window = tk.Toplevel(None)
window.minsize(400,200)
label = tk.Label(window, text=f'Window {AnotherWindow.number}')
label['font'] = ('None', 20, 'bold')
label.pack(fill='both')
AnotherWindow.number += 1
class Window:
def __init__(self, parent):
label = tk.Label(parent, text='Main Window')
label.pack(fill='both')
button = tk.Button(parent, text='Open Window')
button['command'] = AnotherWindow
button.pack(side='bottom')
if __name__ == '__main__':
root = tk.Tk()
root.geometry('400x200+300+300')
root['pady'] = 10
Window(root)
root.mainloop()
1 Like
system
Closed
June 2, 2024, 8:54am
4
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.