trace_add() for menu labels works

This commit is contained in:
Désiré Werner Menrath 2025-05-07 10:47:49 +02:00
parent 9a4d8b3506
commit 42870e2942
2 changed files with 48 additions and 108 deletions

View File

@ -1,45 +0,0 @@
#!/usr/bin/python3
import tkinter as tk
class MainWindow(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.title("Trace-Test-Window")
self.geometry("400x300")
self.text_label = tk.StringVar()
self.text_forground = tk.StringVar(value="red")
self.text_label.set("This is the main window")
self.label = tk.Label(self, textvariable=self.text_label)
self.label.grid(row=0, column=0, padx=10, pady=10)
self.label.grid_remove()
self.button_text = tk.StringVar()
self.button_text.set("Drück für andere Text anzeige")
self.button = tk.Button(self, textvariable=self.button_text, command=self.toggle_lable)
self.button.grid(row=1, column=0, padx=10, pady=10)
self.text_label.trace_add("write", self.update_label)
self.text_forground.trace_add("write", self.update_label)
def update_label(self, *args):
self.label.configure(foreground=self.text_forground.get())
if self.text_label.get():
self.label.grid()
else:
self.label.grid_remove()
def toggle_lable(self):
if 'main window' in self.text_label.get():
self.text_label.set("gewechseltes label")
self.button_text.set("Drück für main window")
else:
self.text_label.set("This is the main window")
self.button_text.set("Drück für andere Text anzeige")
if __name__ == "__main__":
window = MainWindow()
window.mainloop()

109
wirepy.py
View File

@ -107,6 +107,7 @@ class FrameWidgets(ttk.Frame):
self.update_tooltip = tk.StringVar() # StringVar-Variable for update tooltip please not remove! self.update_tooltip = tk.StringVar() # StringVar-Variable for update tooltip please not remove!
self.update_foreground = tk.StringVar(value="red") self.update_foreground = tk.StringVar(value="red")
# Frame for Menu # Frame for Menu
self.menu_frame = ttk.Frame(self) self.menu_frame = ttk.Frame(self)
self.menu_frame.configure(relief="flat") self.menu_frame.configure(relief="flat")
@ -130,7 +131,11 @@ class FrameWidgets(ttk.Frame):
self.settings.add_checkbutton(label=_("Disable Updates"), self.settings.add_checkbutton(label=_("Disable Updates"),
command=lambda: self.update_setting(self.set_update.get()), variable=self.set_update) command=lambda: self.update_setting(self.set_update.get()), variable=self.set_update)
self.updates_lb = ttk.Label(self.menu_frame) self.updates_lb = ttk.Label(self.menu_frame, textvariable=self.update_label)
self.updates_lb.grid(column=4, columnspan=3, row=0, padx=10)
self.updates_lb.grid_remove()
self.update_label.trace_add("write", self.update_label_display)
self.update_foreground.trace_add("write", self.update_label_display)
res = GiteaUpdate.api_down(AppConfig.UPDATE_URL, AppConfig.VERSION, ConfigManager.get("updates")) res = GiteaUpdate.api_down(AppConfig.UPDATE_URL, AppConfig.VERSION, ConfigManager.get("updates"))
self.update_ui_for_update(res) self.update_ui_for_update(res)
@ -295,86 +300,67 @@ class FrameWidgets(ttk.Frame):
self.on_off() self.on_off()
# Method that is called when the variable changes
def update_label_display(self, *args):
# Set the foreground color
self.updates_lb.configure(foreground=self.update_foreground.get())
# Show or hide the label based on whether it contains text
if self.update_label.get():
# Make sure the label is in the correct position every time it's shown
self.updates_lb.grid(column=4, columnspan=3, row=0, padx=10)
else:
self.updates_lb.grid_remove()
# Update the labels based on the result # Update the labels based on the result
def update_ui_for_update(self, res): def update_ui_for_update(self, res):
"""Update UI elements based on update check result""" """Update UI elements based on update check result"""
# First, remove the update button if it exists to avoid conflicts
if hasattr(self, 'update_btn'):
self.update_btn.grid_forget()
delattr(self, 'update_btn')
if res == "False": if res == "False":
self.set_update.set(value=1) self.set_update.set(value=1)
self.update_label.set(_("Update search off")) self.update_label.set(_("Update search off"))
self.update_tooltip.set(_("Updates you have disabled")) self.update_tooltip.set(_("Updates you have disabled"))
self.update_foreground.set("red") # Clear the foreground color as requested
self.update_foreground.set("")
# Remove update button if it exists
if hasattr(self, 'update_btn'):
self.update_btn.grid_forget()
# Display the label
self.updates_lb.configure(
textvariable=self.update_label,
foreground=self.update_foreground.get()
)
self.updates_lb.grid(column=4, columnspan=3, row=0, padx=10)
Tooltip(self.updates_lb, self.update_tooltip.get(), self.tooltip_state)
elif res == "No Internet Connection!": elif res == "No Internet Connection!":
self.update_label.set(_("No Server Connection!")) self.update_label.set(_("No Server Connection!"))
self.update_foreground.set("red") self.update_foreground.set("red")
# Remove update button if it exists
if hasattr(self, 'update_btn'):
self.update_btn.grid_forget()
# Display the label
self.updates_lb.configure(
textvariable=self.update_label,
foreground=self.update_foreground.get()
)
self.updates_lb.grid(column=4, columnspan=3, row=0, padx=10)
elif res == "No Updates": elif res == "No Updates":
self.update_label.set(_("No Updates")) self.update_label.set(_("No Updates"))
self.update_tooltip.set(_("Congratulations! Wire-Py is up to date")) self.update_tooltip.set(_("Congratulations! Wire-Py is up to date"))
self.update_foreground.set("black") self.update_foreground.set("")
# Remove update button if it exists
if hasattr(self, 'update_btn'):
self.update_btn.grid_forget()
# Display the label
self.updates_lb.configure(
textvariable=self.update_label,
foreground=self.update_foreground.get()
)
self.updates_lb.grid(column=4, columnspan=3, row=0, padx=10)
Tooltip(self.updates_lb, self.update_tooltip.get(), self.tooltip_state)
else: else:
self.set_update.set(value=0) self.set_update.set(value=0)
update_text = f"Update {res} {_('available!')}" update_text = f"Update {res} {_('available!')}"
# Remove the label if displayed # Clear the label text since we'll show the button instead
self.updates_lb.grid_forget() self.update_label.set("")
# Create or update the update button # Create the update button
if not hasattr(self, 'update_btn'): self.update_btn = ttk.Menubutton(self.menu_frame, text=update_text)
# Create the update button if it doesn't exist yet self.update_btn.grid(column=4, columnspan=3, row=0, padx=0)
self.update_btn = ttk.Menubutton(self.menu_frame, text=update_text) Tooltip(self.update_btn, _("Click to download new version"), self.tooltip_state)
self.update_btn.grid(column=4, columnspan=3, row=0, padx=0)
Tooltip(self.update_btn, _("Click to download new version"), self.tooltip_state)
self.download = tk.Menu(self, relief="flat") self.download = tk.Menu(self, relief="flat")
self.update_btn.configure(menu=self.download, style="Toolbutton") self.update_btn.configure(menu=self.download, style="Toolbutton")
self.download.add_command( self.download.add_command(
label=_("Download"), label=_("Download"),
command=lambda: GiteaUpdate.download( command=lambda: GiteaUpdate.download(
f"{AppConfig.DOWNLOAD_URL}/{res}.zip", f"{AppConfig.DOWNLOAD_URL}/{res}.zip",
res, res,
AppConfig.IMAGE_PATHS["icon_info"], AppConfig.IMAGE_PATHS["icon_info"],
AppConfig.IMAGE_PATHS["icon_vpn"], AppConfig.IMAGE_PATHS["icon_vpn"],
AppConfig.IMAGE_PATHS["icon_error"], AppConfig.IMAGE_PATHS["icon_error"],
AppConfig.IMAGE_PATHS["icon_msg"] AppConfig.IMAGE_PATHS["icon_msg"]
)
) )
)
@staticmethod @staticmethod
def about() -> None: def about() -> None:
@ -399,7 +385,7 @@ class FrameWidgets(ttk.Frame):
# Disable updates # Disable updates
ConfigManager.set("updates", "off") ConfigManager.set("updates", "off")
# When updates are disabled, we know the result should be "False" # When updates are disabled, we know the result should be "False"
self.update_ui_for_update_status("False") self.update_ui_for_update("False")
else: else:
# Enable updates # Enable updates
ConfigManager.set("updates", "on") ConfigManager.set("updates", "on")
@ -407,7 +393,6 @@ class FrameWidgets(ttk.Frame):
try: try:
# Force a fresh check by passing "on" as the update setting # Force a fresh check by passing "on" as the update setting
res = GiteaUpdate.api_down(AppConfig.UPDATE_URL, AppConfig.VERSION, "on") res = GiteaUpdate.api_down(AppConfig.UPDATE_URL, AppConfig.VERSION, "on")
print(f"API returned: {res}") # Debug output
# Make sure UI is updated regardless of previous state # Make sure UI is updated regardless of previous state
if hasattr(self, 'update_btn'): if hasattr(self, 'update_btn'):
@ -416,11 +401,11 @@ class FrameWidgets(ttk.Frame):
self.updates_lb.grid_forget() self.updates_lb.grid_forget()
# Now update the UI with the fresh result # Now update the UI with the fresh result
self.update_ui_for_update_status(res) self.update_ui_for_update(res)
except Exception as e: except Exception as e:
print(f"Error checking for updates: {e}") print(f"Error checking for updates: {e}")
# Fallback to a default message if there's an error # Fallback to a default message if there's an error
self.update_ui_for_update_status("No Internet Connection!") self.update_ui_for_update("No Internet Connection!")
def tooltip_update_label(self) -> None: def tooltip_update_label(self) -> None:
"""Updates the tooltip menu label based on the current tooltip status""" """Updates the tooltip menu label based on the current tooltip status"""