Elliot Labs Elliot Labs - 3 months ago 26
Python Question

Tkinter Dynamic Widget editing

Here is my code:

class render_window:
def __init__(self, height, width, window_title):
self.root_window = Tk()
w = width
h = height
ws = self.root_window.winfo_screenwidth() # width of the screen
hs = self.root_window.winfo_screenheight() # height of the screen
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
self.root_window.title(window_title)
self.root_window.minsize(width, height)
self.root_window.geometry('%dx%d+%d+%d' % (w, h, x, y))

def new_button(self, button_text, button_command="", grid_row=0, grid_column=0, grid_sticky="NESW", grid_columnspan=1, grid_rowspan=1):
self.button = ttk.Button(self.root_window, text=button_text, command=button_command)
self.button.grid(row=grid_row, column=grid_column, sticky=grid_sticky, columnspan=grid_columnspan, rowspan=grid_rowspan)
self.responsive_grid(grid_row, grid_column)

def new_label(self, label_text, text_alignment="center", grid_row=0, grid_column=0, grid_sticky="NESW", grid_columnspan=1, grid_rowspan=1):
self.label = ttk.Label(self.root_window, text=label_text, anchor=text_alignment)
self.label.grid(row=grid_row, column=grid_column, sticky=grid_sticky, columnspan=grid_columnspan, rowspan=grid_rowspan)
self.responsive_grid(grid_row, grid_column)

def new_progress_bar(self, pg_length=250, pg_mode="determinate", grid_row=0, grid_column=0, grid_sticky="NESW", grid_columnspan=1, grid_rowspan=1):
self.progress_bar = ttk.Progressbar(self.root_window, length=pg_length, mode=pg_mode)
self.progress_bar.grid(row=grid_row, column=grid_column, sticky=grid_sticky, columnspan=grid_columnspan, rowspan=grid_rowspan)
self.responsive_grid(grid_row, grid_column)

def responsive_grid(self, row_responsive=0, column_responsive=0, row_weight_num=1, column_weight_num=1):
self.root_window.grid_columnconfigure(column_responsive, weight=column_weight_num)
self.root_window.grid_rowconfigure(row_responsive, weight=row_weight_num)

options_window = render_window(200, 250, "Options Window")

options_window.new_progress_bar()
options_window.progress_bar.start()
options_window.new_progress_bar(grid_column=1)
options_window.progress_bar.start()
options_window.new_label("Options Window\And other buttons...", grid_row=1, grid_columnspan=2)

options_window.root_window.mainloop()


I have created a system that allows the creation of an interface relatively easily using tkinter. I am having an issue with the modifying of already existing elements, I cannot seem to modify them if I create multiple instances, I can only edit the last one created. when I say modify/edit, I am talking about .config().

So whenever I do:
options_window.progress_bar.config(args_here), it only does that for the last bar created. Is there a way to specify which bar I can execute the code on?

Thanks!

Answer

If I understand you correctly... could you not just asign each progressbar to a variable? Ie.

pb1 = options_window.progress_bar
pb1.start()
pb1.conig('etc, etc')

Sorry if i have misunderstood your problem!

PS - Cool idea!