Reputation: 21
I am creating a program that automatically generates a graph of real estate in cities and displays it through the Tkinter window just to expand my understanding of Python.
In this program, I added a feature that allows users to choose many options when they decide which option should they choose to generate the graph.
For the GUI, I decided to use only tkinter and some features of the Custom Tkinter (CTk).
Here is my code for options of cities created by tkinter combobox:
def combobox_callback(self):
if self.cities == "NY":
self.messagebox.showinfo(message="NY selected")
main(self)
elif self.cities== "CA":
self.messagebox.showinfo(message="CA selected")
main(self)
cities=["NY", "CA" # etc ...]
def result(self):
self.messagebox.showinfo("Clicked", str.get())
str = StringVar(self)
self.combobox1 = ctk.CTkComboBox(master=self, values=cities, command=result)
self.combobox1.place(x=280,y=50)
def main(self):
# This function works fine so I didn't add any code here
# It is about generating graphs when the correct city input is plugged
My question: I want to make every option of the cities by using combobox, and when the combobox clicked, it should automatically call the main function so that the proper graph should be generated. How should I do that?
Upvotes: 1
Views: 981
Reputation: 6820
Updated - since <<ComboboxSelected>>
isn't supported, I'd suggest using a variable trace bound to your combobox's variable
attribute instead, as it seems to support that at least
self.cb_var = StringVar()
self.combobox1 = ctk.CTkComboBox(
master=self,
values=cities,
command=result,
variable=self.cb_var, # add this to your combobox
)
self.cb_var.trace_add( # add a trace to watch cb_var
'write', # callback will be triggered whenever cb_var is written
self.result # callback function goes here!
)
def result(self, *args): # add *args to accomodate the vals passed by the trace
self.messagebox.showinfo("Clicked", self.cb_var.get())
FYI: I would avoid using str
as the name for any variables as that's a reserved word in Python used by the str()
function. Hence why I've called my variable cb_var
here instead.
Upvotes: 2