Bind a reset button to a function that turns it from DISABLED to NORMAL when multiple Combo Boxes are clicked #504
-
Hi, Im trying to programme a reset buttons such that, if three Combo Boxes are filled with values, different from their initial 'Select' option, then the reset button changes from DISABLED to NORMAL. In my current code, I've to enable to reset button when return is hit...but I've not managed to get that to work either. So would preferably like it to enable when the three Combo Boxes are filled Please see my code attached. If theres any more information or I haven't made a lot of sense, please let me know! `import tkinter.messagebox customtkinter.set_appearance_mode("System") # Modes: "System" (standard), "Dark", "Light" class HomePage(customtkinter.CTk):
if name == "main": |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
@amanashraf Here is your modified code with the solution you requested: import customtkinter
import tkinter
customtkinter.set_appearance_mode("System") # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("blue") # Themes: "blue" (standard), "green", "dark-blue"
class HomePage(customtkinter.CTk):
WIDTH = 1000
HEIGHT = 750
def __init__(self):
super().__init__()
self.title("Movie/Series Selector")
self.geometry(f"{HomePage.WIDTH}x{HomePage.HEIGHT}")
# -------- create frame --------
# configure grid layout
self.grid_columnconfigure(1, weight=1)
self.grid_rowconfigure(0, weight=1)
self.frame_left = customtkinter.CTkFrame(master=self)
self.frame_left.grid(row=0, column=0, sticky="nswe", padx=(10,5), pady=20)
self.frame_right = customtkinter.CTkFrame(master=self)
self.frame_right.grid(row=0, column=1, sticky="nswe", padx=(5,10), pady=20)
#Left Frame
#Time
self.label_1 = customtkinter.CTkLabel(master=self.frame_left,
text="How much time do you have?",
text_font=("Roboto Medium", -16)) # font name and size in px
self.label_1.grid(row=1, column=0, pady=(50,10), padx=30, sticky="we")
self.combobox_1 = customtkinter.CTkComboBox(master=self.frame_left,
width = 120,
values=["Select",
"<30mins",
"30mins - 1hr",
"1hr - 2hrs 30mins",
"+2hrs 30mins"], command=self.disable_enable) ### Command function for the combobox 1
self.combobox_1.grid(row=2, column=0, pady=10, padx=30, sticky="we")
#Streaming Service
self.label_2 = customtkinter.CTkLabel(master=self.frame_left,
text="What streaming service do you have?",
text_font=("Roboto Medium", -16)) # font name and size in px
self.label_2.grid(row=4, column=0, padx=30, pady=(35,10))
self.combobox_2 = customtkinter.CTkComboBox(master=self.frame_left,
values=["Select",
"Netflix",
"Prime",
"Disney+",
"Apple TV",
"Hulu",
"HBO"], command=self.disable_enable) ### Same command function for the combobox 2
self.combobox_2.grid(row=5, column=0, pady=10, padx=30, sticky="we")
#Movie or Series
self.label_3 = customtkinter.CTkLabel(master=self.frame_left,
text="Do you want to watch a Movie or Series?",
text_font=("Roboto Medium", -16)) # font name and size in px
self.label_3.grid(row=10, column=0, pady=(35,10), padx=30)
self.combobox_3 = customtkinter.CTkComboBox(master=self.frame_left,
values=["Select", "Movie", "Series"], command=self.disable_enable) ### Again same command function for the combobox 3
self.combobox_3.grid(row=12, column=0, pady=10, padx=30, sticky="we")
#Submit
self.searchButton = customtkinter.CTkButton(master=self.frame_left,
width= 140,
height= 50,
text="Search",
text_font = ("Roboto Medium",20),
command=self.search)
self.searchButton.grid(row=20, column=0, pady=(55,10), padx=30)
self.searchButton.bind("<Return>",self.search)
self.searchButton.bind("<Return>",self.switchButton, add='+')
#Right Frame
#Results Box
self.textbox = customtkinter.CTkTextbox(master=self.frame_right,
width= 550,
height= 550,
text_font = ("Roboto Medium",20))
self.textbox.grid(row=0, column=0, columnspan=2, padx=20, pady=(40, 0), sticky="nsew")
#Reset button
self.resetButton = customtkinter.CTkButton(master=self.frame_right,
width= 140,
height= 50,
text="Reset",
text_font = ("Roboto Medium",20),
state=tkinter.DISABLED,
command= self.resetClick)
self.resetButton.grid(row=20, column=0, padx=20, pady=(35,10))
#Next Result
self.nextResultButton = customtkinter.CTkButton(master=self.frame_right,
width= 140,
height= 50,
text="Find Another",
text_font = ("Roboto Medium",20),
state=tkinter.DISABLED,
command= self.searchAgain)
self.nextResultButton.grid(row=20, column=1, padx=20, pady=(35,10))
#self.nextResultButton["state"] = DISABLED
### Command function for the comboboxes ###
def disable_enable(self, value):
if self.combobox_1.get()!="Select" and self.combobox_2.get()!="Select" and self.combobox_3.get()!="Select":
self.resetButton.configure(state=tkinter.NORMAL) ### Enable the reset button if the three combobox have values other than select
else:
self.resetButton.configure(state=tkinter.DISABLED) ### Disable the reset button if any of the three combobox have default Select value
def search(self):
time = self.combobox_1.get()
service = self.combobox_2.get()
SorM = self.combobox_3.get()
print(time, service, SorM)
def resetClick(self):
self.combobox_1.set("Select")
self.combobox_2.set("Select")
self.combobox_3.set("Select")
self.disable_enable(None) ### Make the button disabled again after resetting
def searchAgain(self):
pass #will create this later
def switchButton(self):
self.resetButton['state']=tkinter.NORMAL
self.nextResultButton['state']=tkinter.NORMAL
def on_closing(self, event=0):
self.destroy()
def start(self):
self.mainloop()
if __name__ == "__main__":
app = HomePage()
app.start() My changes with comments are given with I would recommend you to use CTkOptionMenu instead of CtkComboBox as there is no entry option in it. Here is the demo:Example_demo.mp4 |
Beta Was this translation helpful? Give feedback.
-
And please mark this QnA as |
Beta Was this translation helpful? Give feedback.
@amanashraf Here is your modified code with the solution you requested: