I have a GUI written in Customtkinter, I want to make it possible to activate a script by pressing F6 in addition to clicking a button in the GUI itself. The way I wrote doesn’t work, the console opens, but the GUI itself doesn’t.
import customtkinter
from PIL import Image
import ctypes
from pynput import keyboard
#icon on taskbar
myappid = 'mycompany.myproduct.subproduct.version' # arbitrary string
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
#main script
class App(customtkinter.CTk):
def __init__(self):
super().__init__()
def display_resolution_menu_callback(choiced_resolution):
print("optionmenu dropdown clicked:", choiced_resolution, stamina_farm.get())
def start_stop_button_callback():
if self.start_stop_button.cget("text") == "Start (F6)":
self.start_stop_button.configure(text="Stop (F6)", fg_color="#C20000", hover_color="#8B0000")
print("started")
else:
self.start_stop_button.configure(text="Start (F6)", fg_color="green", hover_color="#006A00")
print("stopped!")
def on_release(key):
if key == keyboard.Key.f6:
start_stop_button_callback()
with keyboard.Listener(on_release=on_release) as listener:
listener.join()
#head
self.title("Gym League Bot")
self.geometry("400x500")
self.resizable(False, False)
self.iconbitmap("C:/Users/Filimonick/Desktop/BotCreatingTools/gym.ico")
#logo
self.logo = customtkinter.CTkImage(dark_image=Image.open("C:/Users/Filimonick/Desktop/BotCreatingTools/logo.png"), size=(365, 120))
self.logo_label = customtkinter.CTkLabel(master=self, text="", image=self.logo)
self.logo_label.place(x=18, y=20)
#display resolution menu
self.display_resolution_menu_frame = customtkinter.CTkFrame(master=self, width=180, height=60)
self.display_resolution_menu_label = customtkinter.CTkLabel(master=self.display_resolution_menu_frame, text="Display resolution", fg_color="#4A0A6F", corner_radius=6, width=180, font=("Outfit Bold", 20))
self.display_resolution_menu_frame.place(x=15, y=150)
self.display_resolution_menu_label.place(x=0, y=0)
self.display_resolution_menu = customtkinter.CTkOptionMenu(master=self.display_resolution_menu_frame, values=["1920x1080", "2560x1080"],
command=display_resolution_menu_callback, fg_color="#4A0A6F", corner_radius=6, width=180, font=("Outfit Bold", 20), button_color="#430865")
self.display_resolution_menu.place(x=0, y=30)
#stamina farm switcher
stamina_farm = customtkinter.BooleanVar(value=False)
self.stamina_farm_switch = customtkinter.CTkSwitch(master=self, text="Stamina Farm", progress_color="#4A0A6F", font=("Outfit Bold", 20), variable=stamina_farm)
self.stamina_farm_switch.place(x=210, y=150)
#start stop button
self.start_stop_button = customtkinter.CTkButton(master=self, text="Start (F6)", width=180, fg_color="green", hover_color="#006A00", command=start_stop_button_callback)
self.start_stop_button.place(x=210, y=180)
app = App()
app.mainloop()
I looked for solutions on stackoverflow, but I couldn’t apply them, they were crashing all the code
New contributor
Filimonick is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.