mirror of
https://github.com/TomSchimansky/CustomTkinter.git
synced 2023-08-10 21:13:13 +03:00
small fixes for CTkComboBox
This commit is contained in:
parent
0aa9dfc70f
commit
550653c6c3
@ -354,7 +354,7 @@ class DrawEngine:
|
||||
return requires_recoloring
|
||||
|
||||
def draw_rounded_rect_with_border_vertical_split(self, width: Union[float, int], height: Union[float, int], corner_radius: Union[float, int],
|
||||
border_width: Union[float, int], left_section_width: int) -> bool:
|
||||
border_width: Union[float, int], left_section_width: Union[float, int]) -> bool:
|
||||
""" Draws a rounded rectangle with a corner_radius and border_width on the canvas which is split at left_section_width.
|
||||
The border elements have the tags 'border_parts_left', 'border_parts_lright',
|
||||
the main foreground elements have an 'inner_parts_left' and inner_parts_right' tag,
|
||||
@ -362,6 +362,7 @@ class DrawEngine:
|
||||
|
||||
returns bool if recoloring is necessary """
|
||||
|
||||
left_section_width = round(left_section_width)
|
||||
width = math.floor(width / 2) * 2 # round (floor) _current_width and _current_height and restrict them to even values only
|
||||
height = math.floor(height / 2) * 2
|
||||
corner_radius = round(corner_radius)
|
||||
|
@ -61,8 +61,6 @@ class CTkComboBox(CTkBaseClass):
|
||||
# callback and hover functionality
|
||||
self.function = command
|
||||
self.variable = variable
|
||||
self.variable_callback_blocked = False
|
||||
self.variable_callback_name = None
|
||||
self.state = state
|
||||
self.hover = hover
|
||||
self.click_animation_running = False
|
||||
@ -91,12 +89,13 @@ class CTkComboBox(CTkBaseClass):
|
||||
self.draw_engine = DrawEngine(self.canvas)
|
||||
|
||||
self.entry = tkinter.Entry(master=self,
|
||||
width=0,
|
||||
state=self.state,
|
||||
width=1,
|
||||
bd=0,
|
||||
highlightthickness=0,
|
||||
font=self.apply_font_scaling(self.text_font))
|
||||
left_section_width = self._current_width - self._current_height
|
||||
self.entry.grid(row=0, column=0, rowspan=1, columnspan=1, sticky="w",
|
||||
self.entry.grid(row=0, column=0, rowspan=1, columnspan=1, sticky="ew",
|
||||
padx=(self.apply_widget_scaling(max(self.corner_radius, 3)),
|
||||
self.apply_widget_scaling(max(self._current_width - left_section_width + 3, 3))))
|
||||
|
||||
@ -112,16 +111,15 @@ class CTkComboBox(CTkBaseClass):
|
||||
self.bind('<Configure>', self.update_dimensions_event)
|
||||
|
||||
if self.variable is not None:
|
||||
self.variable_callback_name = self.variable.trace_add("write", self.variable_callback)
|
||||
self.set(self.variable.get(), from_variable_callback=True)
|
||||
self.entry.configure(textvariable=self.variable)
|
||||
|
||||
# if self.variable is not None:
|
||||
# self.variable_callback_name = self.variable.trace_add("write", self.variable_callback)
|
||||
# self.set(self.variable.get(), from_variable_callback=True)
|
||||
|
||||
def set_scaling(self, *args, **kwargs):
|
||||
super().set_scaling(*args, **kwargs)
|
||||
|
||||
if self.text_label is not None:
|
||||
self.text_label.destroy()
|
||||
self.text_label = None
|
||||
|
||||
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width),
|
||||
height=self.apply_widget_scaling(self._desired_height))
|
||||
self.draw()
|
||||
@ -190,7 +188,7 @@ class CTkComboBox(CTkBaseClass):
|
||||
|
||||
if "state" in kwargs:
|
||||
self.state = kwargs["state"]
|
||||
self.set_cursor()
|
||||
self.entry.configure(state=self.state)
|
||||
require_redraw = True
|
||||
del kwargs["state"]
|
||||
|
||||
@ -227,17 +225,8 @@ class CTkComboBox(CTkBaseClass):
|
||||
del kwargs["command"]
|
||||
|
||||
if "variable" in kwargs:
|
||||
if self.variable is not None: # remove old callback
|
||||
self.variable.trace_remove("write", self.variable_callback_name)
|
||||
|
||||
self.variable = kwargs["variable"]
|
||||
|
||||
if self.variable is not None and self.variable != "":
|
||||
self.variable_callback_name = self.variable.trace_add("write", self.variable_callback)
|
||||
self.set(self.variable.get(), from_variable_callback=True)
|
||||
else:
|
||||
self.variable = None
|
||||
|
||||
self.entry.configure(textvariable=self.variable)
|
||||
del kwargs["variable"]
|
||||
|
||||
if "width" in kwargs:
|
||||
@ -248,13 +237,17 @@ class CTkComboBox(CTkBaseClass):
|
||||
self.set_dimensions(height=kwargs["height"])
|
||||
del kwargs["height"]
|
||||
|
||||
if "values" in kwargs:
|
||||
self.values = kwargs["values"]
|
||||
del kwargs["values"]
|
||||
|
||||
super().configure(*args, **kwargs)
|
||||
|
||||
if require_redraw:
|
||||
self.draw()
|
||||
|
||||
def on_enter(self, event=0):
|
||||
if self.hover is True and self.state == tkinter.NORMAL:
|
||||
if self.hover is True and self.state == tkinter.NORMAL and len(self.values) > 0:
|
||||
if sys.platform == "darwin" and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
|
||||
self.canvas.configure(cursor="pointinghand")
|
||||
elif sys.platform.startswith("win") and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
|
||||
@ -289,30 +282,21 @@ class CTkComboBox(CTkBaseClass):
|
||||
if self.click_animation_running:
|
||||
self.on_enter()
|
||||
|
||||
def variable_callback(self, var_name, index, mode):
|
||||
if not self.variable_callback_blocked:
|
||||
self.set(self.variable.get(), from_variable_callback=True)
|
||||
|
||||
def set(self, value: str, from_variable_callback: bool = False):
|
||||
self.current_value = value
|
||||
|
||||
self.entry.delete(0, tkinter.END)
|
||||
self.entry.insert(0, self.current_value)
|
||||
|
||||
if self.variable is not None and not from_variable_callback:
|
||||
self.variable_callback_blocked = True
|
||||
self.variable.set(self.current_value)
|
||||
self.variable_callback_blocked = False
|
||||
|
||||
if not from_variable_callback:
|
||||
if self.function is not None:
|
||||
self.function(self.current_value)
|
||||
|
||||
def get(self) -> str:
|
||||
return self.current_value
|
||||
return self.entry.get()
|
||||
|
||||
def clicked(self, event=0):
|
||||
if self.state is not tkinter.DISABLED:
|
||||
if self.state is not tkinter.DISABLED and len(self.values) > 0:
|
||||
self.open_dropdown_menu()
|
||||
|
||||
# click animation: change color with .on_leave() and back to normal after 100ms with click_animation()
|
||||
|
@ -93,7 +93,6 @@ class CTkOptionMenu(CTkBaseClass):
|
||||
self.canvas.bind("<Button-1>", self.clicked)
|
||||
self.bind('<Configure>', self.update_dimensions_event)
|
||||
|
||||
self.set_cursor()
|
||||
self.draw() # initial draw
|
||||
|
||||
if self.variable is not None:
|
||||
@ -169,23 +168,21 @@ class CTkOptionMenu(CTkBaseClass):
|
||||
self.text_label.configure(bg=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
|
||||
def open_dropdown_menu(self):
|
||||
if len(self.values) > 0:
|
||||
self.dropdown_menu = DropdownMenu(x_position=self.winfo_rootx(),
|
||||
y_position=self.winfo_rooty() + self.apply_widget_scaling(self._current_height + 4),
|
||||
width=self._current_width,
|
||||
values=self.values,
|
||||
command=self.set,
|
||||
fg_color=self.dropdown_color,
|
||||
button_hover_color=self.dropdown_hover_color,
|
||||
button_color=self.dropdown_color,
|
||||
text_color=self.dropdown_text_color)
|
||||
self.dropdown_menu = DropdownMenu(x_position=self.winfo_rootx(),
|
||||
y_position=self.winfo_rooty() + self.apply_widget_scaling(self._current_height + 4),
|
||||
width=self._current_width,
|
||||
values=self.values,
|
||||
command=self.set,
|
||||
fg_color=self.dropdown_color,
|
||||
button_hover_color=self.dropdown_hover_color,
|
||||
button_color=self.dropdown_color,
|
||||
text_color=self.dropdown_text_color)
|
||||
|
||||
def configure(self, *args, **kwargs):
|
||||
require_redraw = False # some attribute changes require a call of self.draw() at the end
|
||||
|
||||
if "state" in kwargs:
|
||||
self.state = kwargs["state"]
|
||||
self.set_cursor()
|
||||
require_redraw = True
|
||||
del kwargs["state"]
|
||||
|
||||
@ -246,29 +243,19 @@ class CTkOptionMenu(CTkBaseClass):
|
||||
if "values" in kwargs:
|
||||
self.values = kwargs["values"]
|
||||
del kwargs["values"]
|
||||
self.set_cursor()
|
||||
|
||||
super().configure(*args, **kwargs)
|
||||
|
||||
if require_redraw:
|
||||
self.draw()
|
||||
|
||||
def set_cursor(self):
|
||||
if Settings.cursor_manipulation_enabled:
|
||||
if self.state == tkinter.DISABLED:
|
||||
if sys.platform == "darwin" and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
|
||||
self.configure(cursor="arrow")
|
||||
elif sys.platform.startswith("win") and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
|
||||
self.configure(cursor="arrow")
|
||||
|
||||
elif self.state == tkinter.NORMAL:
|
||||
if sys.platform == "darwin" and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
|
||||
self.configure(cursor="pointinghand")
|
||||
elif sys.platform.startswith("win") and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
|
||||
self.configure(cursor="hand2")
|
||||
|
||||
def on_enter(self, event=0):
|
||||
if self.hover is True and self.state == tkinter.NORMAL:
|
||||
if self.hover is True and self.state == tkinter.NORMAL and len(self.values) > 0:
|
||||
if sys.platform == "darwin" and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
|
||||
self.configure(cursor="pointinghand")
|
||||
elif sys.platform.startswith("win") and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
|
||||
self.configure(cursor="hand2")
|
||||
|
||||
# set color of inner button parts to hover color
|
||||
self.canvas.itemconfig("inner_parts_right",
|
||||
outline=ThemeManager.single_color(self.button_hover_color, self._appearance_mode),
|
||||
@ -278,6 +265,11 @@ class CTkOptionMenu(CTkBaseClass):
|
||||
self.click_animation_running = False
|
||||
|
||||
if self.hover is True:
|
||||
if sys.platform == "darwin" and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
|
||||
self.configure(cursor="arrow")
|
||||
elif sys.platform.startswith("win") and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
|
||||
self.configure(cursor="arrow")
|
||||
|
||||
# set color of inner button parts
|
||||
self.canvas.itemconfig("inner_parts_right",
|
||||
outline=ThemeManager.single_color(self.button_color, self._appearance_mode),
|
||||
@ -312,7 +304,7 @@ class CTkOptionMenu(CTkBaseClass):
|
||||
return self.current_value
|
||||
|
||||
def clicked(self, event=0):
|
||||
if self.state is not tkinter.DISABLED:
|
||||
if self.state is not tkinter.DISABLED and len(self.values) > 0:
|
||||
self.open_dropdown_menu()
|
||||
|
||||
# click animation: change color with .on_leave() and back to normal after 100ms with click_animation()
|
||||
|
@ -97,6 +97,7 @@ class DropdownMenu(tkinter.Toplevel):
|
||||
hover_color=self.button_hover_color,
|
||||
corner_radius=self.button_corner_radius,
|
||||
command=lambda i=index: self.button_callback(i))
|
||||
button.text_label.configure(anchor="w")
|
||||
button.text_label.grid(row=0, column=0, rowspan=2, columnspan=2, sticky="w")
|
||||
button.grid(row=index, column=0,
|
||||
padx=x_spacing,
|
||||
|
@ -4,56 +4,60 @@ import customtkinter
|
||||
customtkinter.set_appearance_mode("dark") # Modes: "System" (standard), "Dark", "Light"
|
||||
customtkinter.set_default_color_theme("blue") # Themes: "blue" (standard), "green", "dark-blue"
|
||||
|
||||
app = customtkinter.CTk() # create CTk window like you do with the Tk window
|
||||
app.geometry("400x540")
|
||||
app = customtkinter.CTk()
|
||||
app.geometry("400x580")
|
||||
app.title("CustomTkinter simple_example.py")
|
||||
|
||||
|
||||
def button_function():
|
||||
print("Button click")
|
||||
def button_callback():
|
||||
print("Button click", combobox_1.get())
|
||||
|
||||
|
||||
def slider_function(value):
|
||||
def slider_callback(value):
|
||||
progressbar_1.set(value)
|
||||
|
||||
|
||||
y_padding = 13
|
||||
|
||||
frame_1 = customtkinter.CTkFrame(master=app)
|
||||
frame_1.pack(pady=20, padx=60, fill="both", expand=True)
|
||||
|
||||
label_1 = customtkinter.CTkLabel(master=frame_1, justify=tkinter.LEFT)
|
||||
label_1.pack(pady=y_padding, padx=10)
|
||||
label_1.pack(pady=12, padx=10)
|
||||
|
||||
progressbar_1 = customtkinter.CTkProgressBar(master=frame_1)
|
||||
progressbar_1.pack(pady=y_padding, padx=10)
|
||||
progressbar_1.pack(pady=12, padx=10)
|
||||
|
||||
button_1 = customtkinter.CTkButton(master=frame_1, command=button_function)
|
||||
button_1.pack(pady=y_padding, padx=10)
|
||||
button_1 = customtkinter.CTkButton(master=frame_1, command=button_callback)
|
||||
button_1.pack(pady=12, padx=10)
|
||||
|
||||
slider_1 = customtkinter.CTkSlider(master=frame_1, command=slider_function, from_=0, to=1)
|
||||
slider_1.pack(pady=y_padding, padx=10)
|
||||
slider_1 = customtkinter.CTkSlider(master=frame_1, command=slider_callback, from_=0, to=1)
|
||||
slider_1.pack(pady=12, padx=10)
|
||||
slider_1.set(0.5)
|
||||
|
||||
entry_1 = customtkinter.CTkEntry(master=frame_1, placeholder_text="CTkEntry")
|
||||
entry_1.pack(pady=y_padding, padx=10)
|
||||
entry_1.pack(pady=12, padx=10)
|
||||
|
||||
optionmenu_1 = customtkinter.CTkOptionMenu(frame_1, values=["Option 1", "Option 2", "Option 42"])
|
||||
optionmenu_1.pack(pady=10, padx=10)
|
||||
s = customtkinter.StringVar(value="test")
|
||||
|
||||
optionmenu_1 = customtkinter.CTkOptionMenu(frame_1, values=["Option 1", "Option 2", "Option 42"], variable=s)
|
||||
optionmenu_1.pack(pady=12, padx=10)
|
||||
optionmenu_1.set("CTkOptionMenu")
|
||||
|
||||
combobox_1 = customtkinter.CTkComboBox(frame_1, values=["Option 1", "Option 2", "Option 42"], variable=s)
|
||||
combobox_1.pack(pady=12, padx=10)
|
||||
optionmenu_1.set("CTkComboBox")
|
||||
|
||||
checkbox_1 = customtkinter.CTkCheckBox(master=frame_1)
|
||||
checkbox_1.pack(pady=y_padding, padx=10)
|
||||
checkbox_1.pack(pady=12, padx=10)
|
||||
|
||||
radiobutton_var = tkinter.IntVar(value=1)
|
||||
|
||||
radiobutton_1 = customtkinter.CTkRadioButton(master=frame_1, variable=radiobutton_var, value=1)
|
||||
radiobutton_1.pack(pady=y_padding, padx=10)
|
||||
radiobutton_1.pack(pady=12, padx=10)
|
||||
|
||||
radiobutton_2 = customtkinter.CTkRadioButton(master=frame_1, variable=radiobutton_var, value=2)
|
||||
radiobutton_2.pack(pady=y_padding, padx=10)
|
||||
radiobutton_2.pack(pady=12, padx=10)
|
||||
|
||||
switch_1 = customtkinter.CTkSwitch(master=frame_1)
|
||||
switch_1.pack(pady=y_padding, padx=10)
|
||||
switch_1.pack(pady=12, padx=10)
|
||||
|
||||
app.mainloop()
|
||||
|
@ -1,4 +1,5 @@
|
||||
import tkinter
|
||||
import tkinter.ttk as ttk
|
||||
import customtkinter
|
||||
|
||||
app = customtkinter.CTk()
|
||||
@ -22,4 +23,10 @@ optionmenu_tk.pack(pady=10, padx=10)
|
||||
optionmenu_1 = customtkinter.CTkOptionMenu(app, variable=variable, values=countries, command=select_callback)
|
||||
optionmenu_1.pack(pady=10, padx=10)
|
||||
|
||||
combobox_tk = ttk.Combobox(app, values=countries)
|
||||
combobox_tk.pack(pady=10, padx=10)
|
||||
|
||||
combobox_1 = customtkinter.CTkComboBox(app, variable=variable, values=countries, command=select_callback)
|
||||
combobox_1.pack(pady=10, padx=10)
|
||||
|
||||
app.mainloop()
|
@ -73,8 +73,8 @@ switch_1.pack(pady=20, padx=10)
|
||||
optionmenu_var = tkinter.StringVar(value="test")
|
||||
optionmenu_1 = customtkinter.CTkOptionMenu(master=app, variable=optionmenu_var, values=["Option 1", "Option 2", "Option 3"])
|
||||
optionmenu_1.pack(pady=20, padx=10)
|
||||
optionmenu_2 = customtkinter.CTkOptionMenu(master=app, values=["Option 1", "Option 2", "Option 3"])
|
||||
optionmenu_2.pack(pady=20, padx=10)
|
||||
optionmenu_2.configure(variable=optionmenu_var)
|
||||
combobox_1 = customtkinter.CTkComboBox(master=app, values=["Option 1", "Option 2", "Option 3"])
|
||||
combobox_1.pack(pady=20, padx=10)
|
||||
combobox_1.configure(variable=optionmenu_var)
|
||||
|
||||
app.mainloop()
|
||||
|
@ -48,5 +48,10 @@ optionmenu_1.pack(pady=10, padx=10)
|
||||
button_6 = customtkinter.CTkButton(master=app, text="Disable/Enable optionmenu_1", command=lambda: change_state(optionmenu_1))
|
||||
button_6.pack(padx=20, pady=(10, 20))
|
||||
|
||||
combobox_1 = customtkinter.CTkComboBox(app, values=["test 1", "test 2"])
|
||||
combobox_1.pack(pady=10, padx=10)
|
||||
button_7 = customtkinter.CTkButton(master=app, text="Disable/Enable combobox_1", command=lambda: change_state(combobox_1))
|
||||
button_7.pack(padx=20, pady=(10, 20))
|
||||
|
||||
|
||||
app.mainloop()
|
||||
|
Loading…
Reference in New Issue
Block a user