单击任务栏图标时,如何overrideredirect在其他打开的窗口顶部显示自定义 Tkinter 窗口(true)?我下面的代码可以工作(最大化尚未实现),除非此 Tkinter 窗口与其他非 Tkinter 窗口重叠。例如,当此 Tkinter 窗口位于(其他程序的)两个窗口下方并且调用时(通过单击任务栏图标),需要单击该图标2 次,此窗口才会出现在这 2 个窗口的顶部。我想在单击任务栏图标时将窗口自动置于顶部。
我根据这个答案编写了我的代码,并为任务栏图标缝合了这个解决方法。我尝试搜索任何处理任务栏图标单击事件的 Tkinter 代码,但一无所获。
import tkinter as tk
from tkinter import ttk
def get_pos(event):
global xwin
global ywin
xwin = event.x
ywin = event.y
def move_window(event):
root.geometry(f'+{event.x_root - xwin}+{event.y_root - ywin}')
def quit():
root.destroy()
#window contents
root = tk.Tk()
container = tk.Toplevel(root)
root.overrideredirect(True)
#default window dimension
root.geometry('350x150+200+200')
root.minsize(350, 150)
container.attributes("-alpha",0.0)
back_ground = "#2c2c2c"
#minimize btn binding
def onRootIconify(event): root.withdraw()
container.bind("<Unmap>", onRootIconify)
root.bind("<Unmap>", onRootIconify)
def onRootDeiconify(event): root.deiconify()
container.bind("<Map>", onRootDeiconify)
root.bind("<Map>", onRootDeiconify)
#title bar
title_bar = tk.Frame(root, bg=back_ground, bd=1,
highlightcolor=back_ground,
highlightthickness=0)
#minimize btn
minimize_btn = tk.Button(title_bar, text='', bg=back_ground, padx=5, pady=2,
bd=0, font="bold", fg='white', width=2,
activebackground="red",
activeforeground="white",
highlightthickness=0,
command=lambda: container.wm_state('iconic'))
#maximize btn
maximize_btn = tk.Button(title_bar, text='', bg=back_ground, padx=5, pady=2,
bd=0, font="bold", fg='white', width=2,
activebackground="red",
activeforeground="white",
highlightthickness=0,
command=None)
#close btn
close_button = tk.Button(title_bar, text='', bg=back_ground, padx=5, pady=2,
bd=0, font="bold", fg='white', width=2,
activebackground="red",
activeforeground="white",
highlightthickness=0,
command= quit)
#window title
title_window = "Untitled window"
title_name = tk.Label(title_bar, text=title_window, font="Arial 12", bg=back_ground, fg="white")
#main area of the window
window = tk.Frame(root, bg="white", highlightthickness=1, highlightbackground=back_ground)
txt = tk.Label(window, bg='white', text="Prototype window").pack(anchor="center")
# pack the widgets
title_bar.pack(fill='x', side=tk.TOP)
title_name.pack(side='left', padx=5)
close_button.pack(side='right')
maximize_btn.pack(side=tk.RIGHT)
minimize_btn.pack(side=tk.RIGHT)
window.pack(fill='both', expand=True, side=tk.TOP)
# bind title bar motion to the move window function
title_bar.bind("<B1-Motion>", move_window)
title_bar.bind("<Button-1>", get_pos)
#workaround to enable window dragging on window title text
title_name.bind("<B1-Motion>", move_window)
title_name.bind("<Button-1>", get_pos)
minimize_btn.bind('<Enter>', lambda x: minimize_btn.configure(bg='#777777'))
minimize_btn.bind('<Leave>', lambda x: minimize_btn.configure(bg=back_ground))
maximize_btn.bind('<Enter>', lambda x: maximize_btn.configure(bg='#777777'))
maximize_btn.bind('<Leave>', lambda x: maximize_btn.configure(bg=back_ground))
close_button.bind('<Enter>', lambda x: close_button.configure(bg='red'))
close_button.bind('<Leave>',lambda x: close_button.configure(bg=back_ground))
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
自定义窗口:
经过查找Coder的相关答案和推荐,终于解决了我的问题。在我的解决方案中,我使用了这个答案中的技术。我完成的代码不使用任何不可见的 Tkinter 窗口并大量使用ctypes.windll(因此我的代码仅限于Windows )。最小化、最大化和关闭窗口的逻辑也完成了。
我设法解决了以下问题:
\n#https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowpos\n#https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow\n\nimport tkinter as tk\nfrom tkinter import ttk\nfrom ctypes import windll\n\n\ndef set_appwindow():\n global hasstyle\n GWL_EXSTYLE=-20\n WS_EX_APPWINDOW=0x00040000\n WS_EX_TOOLWINDOW=0x00000080\n if not hasstyle:\n hwnd = windll.user32.GetParent(root.winfo_id())\n style = windll.user32.GetWindowLongW(hwnd, GWL_EXSTYLE)\n style = style & ~WS_EX_TOOLWINDOW\n style = style | WS_EX_APPWINDOW\n res = windll.user32.SetWindowLongW(hwnd, GWL_EXSTYLE, style)\n root.withdraw()\n root.after(10, lambda:root.wm_deiconify())\n hasstyle=True\n\ndef get_pos(event):\n global xwin\n global ywin\n xwin = event.x\n ywin = event.y\n\ndef move_window(event):\n global previousPosition\n root.geometry(f\'+{event.x_root - xwin}+{event.y_root - ywin}\')\n previousPosition = [root.winfo_x(), root.winfo_y()]\n\ndef move_window_bindings(*args, status=True):\n if status == True:\n title_bar.bind("<B1-Motion>", move_window)\n title_bar.bind("<Button-1>", get_pos)\n title_name.bind("<B1-Motion>", move_window)\n title_name.bind("<Button-1>", get_pos)\n else:\n title_bar.unbind("<B1-Motion>")\n title_bar.unbind("<Button-1>")\n title_name.unbind("<B1-Motion>")\n title_name.unbind("<Button-1>")\n\ndef quit():\n root.destroy()\n\n#reference: https://programtalk.com/python-examples/ctypes.windll.user32.ShowWindow/\ndef minimize(hide=False):\n hwnd = windll.user32.GetParent(root.winfo_id())\n windll.user32.ShowWindow(hwnd, 0 if hide else 6)\n\ndef maximizeToggle():\n global maximized\n global previousPosition\n if maximized == False:\n #maximize current window\n maximize_btn.config(text="\xe2\x9d\x90")\n hwnd = windll.user32.GetParent(root.winfo_id())\n SWP_SHOWWINDOW = 0x40\n windll.user32.SetWindowPos(hwnd, 0, 0, 0, int(root.winfo_screenwidth()), int(root.winfo_screenheight()-48),SWP_SHOWWINDOW)\n maximized = True\n move_window_bindings(status=False)\n else:\n #restore down window\n maximize_btn.config(text="")\n hwnd = windll.user32.GetParent(root.winfo_id())\n SWP_SHOWWINDOW = 0x40\n windll.user32.SetWindowPos(hwnd, 0, previousPosition[0], previousPosition[1], int(root.minsize()[0]), int(root.minsize()[1]),SWP_SHOWWINDOW)\n maximized = False\n move_window_bindings(status=True)\n\n\n#---------------------------------\nroot = tk.Tk()\nroot.overrideredirect(True)\n\n#window details\nmaximized = False\nback_ground = "#2c2c2c"\ndimension = (300, 300)\n#------------------------------\n\nif len(dimension) == 0:\n #default window dimension\n x = (root.winfo_screenwidth()/2)-(350/2)\n y = (root.winfo_screenheight()/2)-(250)\n root.geometry(f\'350x150+{int(x)}+{int(y)}\')\n root.minsize(350, 150)\n dimension = (350, 150)\n previousPosition = [int(x), int(y)]\n\nelse:\n x = (root.winfo_screenwidth()/2)-(dimension[0]/2)\n y = (root.winfo_screenheight()/2)-250\n root.geometry(f\'{dimension[0]}x{dimension[1]}+{int(x)}+{int(y)}\')\n root.minsize(dimension[0], dimension[1])\n previousPosition = [int(x), int(y)]\n\n\n\n\n#title bar\ntitle_bar = tk.Frame(root, bg=back_ground, bd=1,\n highlightcolor=back_ground, \n highlightthickness=0)\n\n#window title\ntitle_window = "Untitled window"\ntitle_name = tk.Label(title_bar, text=title_window, \n font="Arial 12", bg=back_ground, fg="white")\n\n#minimize btn\nminimize_btn = tk.Button(title_bar, text=\'\', bg=back_ground, padx=5, pady=2, \n bd=0, font="bold", fg=\'white\', width=2,\n activebackground="red",\n activeforeground="white", \n highlightthickness=0, \n command=minimize)\n\n#maximize btn\nmaximize_btn = tk.Button(title_bar, text=\'\', bg=back_ground, padx=5, pady=2, \n bd=0, font="bold", fg=\'white\', width=2,\n activebackground="red",\n activeforeground="white", \n highlightthickness=0, \n command=maximizeToggle)\n\n#close btn\nclose_button = tk.Button(title_bar, text=\'\', bg=back_ground, padx=5, pady=2, \n bd=0, font="bold", fg=\'white\', width=2,\n activebackground="red",\n activeforeground="white", \n highlightthickness=0, \n command= quit)\n\n#hover effect\nminimize_btn.bind(\'<Enter>\', lambda x: minimize_btn.configure(bg=\'#777777\'))\nminimize_btn.bind(\'<Leave>\', lambda x: minimize_btn.configure(bg=back_ground))\nmaximize_btn.bind(\'<Enter>\', lambda x: maximize_btn.configure(bg=\'#777777\'))\nmaximize_btn.bind(\'<Leave>\', lambda x: maximize_btn.configure(bg=back_ground))\nclose_button.bind(\'<Enter>\', lambda x: close_button.configure(bg=\'red\'))\nclose_button.bind(\'<Leave>\',lambda x: close_button.configure(bg=back_ground))\n\n\n#main area of the window\nwindow = tk.Frame(root, bg="white", highlightthickness=1, highlightbackground=back_ground)\n\ntxt = tk.Label(window, bg=\'white\', text="Prototype window").pack(anchor="center")\n\n# pack the widgets\ntitle_bar.pack(fill=\'x\', side=tk.TOP)\ntitle_name.pack(side=\'left\', padx=5)\nclose_button.pack(side=\'right\')\nmaximize_btn.pack(side=tk.RIGHT)\nminimize_btn.pack(side=tk.RIGHT)\nwindow.pack(fill=\'both\', expand=True, side=tk.TOP)\nmove_window_bindings(status=True)\n\n\n#ctype\nhasstyle = False\nroot.update_idletasks()\nroot.withdraw()\nset_appwindow()\n\nroot.mainloop()\nRun Code Online (Sandbox Code Playgroud)\n演示:
\n\n如果有人可以将我的代码转换为有类语法,请发表评论,因为我很难做到这一点,呵呵。代码优化非常受欢迎。
\n