You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a tkinter app thats uses several OptionMenu widgets. These widgets call a function to update the list of options each time the widget is clicked. The resulting dropdown list shows the latest updated items. The code below reproduces this behavior by showing 10 random integers each time the widget is clicked.
I am porting the entire application to customtkinter and in this case the get_options function is called after the list is displayed, thus showing stale values.
The print statements in the code shows the changed value, and the updates to the options.
File tk_app.py shows the tkinter dropdown application:
import tkinter as tk
from dynamic_dropdown import TkDropDown, get_options
win = tk.Tk()
var = tk.StringVar(value='None')
def callback(value):
print(value)
dd = TkDropDown(win, var, updater=get_options, command=callback)
win.mainloop()
File ctk_app.py shows the customtkinter version:
import customtkinter as ctk
from dynamic_dropdown import CTkDropDown, get_options
win = ctk.CTk()
var = tk.StringVar(value='None')
def callback(value):
print(value)
dd = CTkDropDown(win, var, updater=get_options, command=callback)
win.mainloop()
File dynamic_dropdown.py implements this behavior. TkDropDown shows the correct behavior. CTkDropDown ports the former to customtkinter and shows the incorrect behavior.
from tkinter import ttk
import customtkinter as ctk
import random
def get_options():
# Example dynamic options.
return [str(random.randint(0, 100)) for _ in range(10)]
class TkDropDown:
def __init__(self, parent, variable: tk.StringVar, updater: callable, command: callable):
self.parent = parent
self.variable = variable # Variable that holds the selected value
self.updater_f = updater # Function that returns the list of options
self.command_f = command # Callback function when value is changed
self.opt = ttk.OptionMenu(self.parent, self.variable, self.variable.get(), command=self.callback)
self.opt.pack(side="top")
self.opt.bind("<Button-1>", lambda event: self.update()) # Update the options when clicked
self.update()
def callback(self, *args):
self.command_f(self.variable.get())
def update(self):
new_options = self.updater_f()
print(new_options)
# Remove the existing menu items
self.menu = self.opt['menu']
self.menu.delete(0, 'end')
# Insert the new menu items
for item in new_options:
self.menu.add_radiobutton(label=item, variable=self.variable)
class CTkDropDown:
def __init__(self, parent, variable: tk.StringVar, updater: callable, command: callable):
self.parent = parent
self.variable = variable
self.updater_f = updater
self.command_f = command
self.opt = ctk.CTkOptionMenu(
master=self.parent,
variable=self.variable,
values=[self.variable.get()], # Initial value
command=self.callback
)
self.opt.pack(side="top")
self.opt.bind("<Button-1>", lambda event: self.update())
self.update()
def callback(self, choice):
self.command_f(choice)
def update(self):
new_options = self.updater_f()
print(new_options)
# Configure with new options
self.opt.configure(values=new_options)
Is there a better way to do this using customtkinter? Please help.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have a tkinter app thats uses several
OptionMenu
widgets. These widgets call a function to update the list of options each time the widget is clicked. The resulting dropdown list shows the latest updated items. The code below reproduces this behavior by showing 10 random integers each time the widget is clicked.I am porting the entire application to customtkinter and in this case the
get_options
function is called after the list is displayed, thus showing stale values.The
print
statements in the code shows the changed value, and the updates to the options.File
tk_app.py
shows the tkinter dropdown application:File
ctk_app.py
shows the customtkinter version:File
dynamic_dropdown.py
implements this behavior.TkDropDown
shows the correct behavior.CTkDropDown
ports the former to customtkinter and shows the incorrect behavior.Is there a better way to do this using customtkinter? Please help.
Beta Was this translation helpful? Give feedback.
All reactions