代码之家  ›  专栏  ›  技术社区  ›  Jamin

不带标题栏的python tkinter还原窗口

  •  1
  • Jamin  · 技术社区  · 6 年前

    我有一个 tkinter

    我希望图标总是显示在任务栏上,当程序最小化,然后恢复,我希望标题栏仍然是从 .overrideredirect(1) . 不幸的是,在最小化之前和之后,我都无法重置标志,而不会使任务栏图标消失。

    请让我知道我做错了什么。谢谢!

    #!/usr/bin/python3
    from tkinter import *
    import tkinter as tk
    import datetime
    import time
    import math
    
    root = tk.Tk() 
    
    root.overrideredirect(1)
    
    def close():
        root.destroy()
    def minimizeWindow():
        root.withdraw()
        root.overrideredirect(False)
        root.iconify()
    
    root.resizable(False, False)
    canvas = Canvas(root, width = 400, height = 400)
    canvas.pack()
    exit = Button(root, text='x', command = close)
    exitWindow = canvas.create_window(10,10, window=exit)
    minimize = Button(root, text='-', command = minimizeWindow)
    minimizeWindow = canvas.create_window(30,10,window=minimize)
    icon = PhotoImage(file='py.gif')
    root.tk.call('wm', 'iconphoto', root._w, icon)
    root.mainloop() # starts the mainloop
    

    我正在努力使它同时适用于windows和linux。我把标题栏全部去掉的原因是为了避免操作系统字体和窗口设置的差异。它目前在两个操作系统上都表现出相同的行为。

    重申一下,我希望任务栏图标显示在程序启动时,我希望程序的窗口保持它的标题栏状态时,从最小化恢复。

    1 回复  |  直到 6 年前
        1
  •  2
  •   Mike - SMT    6 年前

    这取决于您使用的操作系统。如果您使用的是Windows,下面的解决方案应该适合您。

    我添加了一个函数,可以重新应用 overriderdirect . 我们在根上使用的绑定正在调用此函数。

    对于linux,您可能需要使用不同的文件类型。在windows上使用.ico,在linux上可能需要使用.xbm。

    请在下面的帖子中看到这个答案: Python 3 tkinter iconbitmap error in ubuntu

    我添加了 iconbitmap root.tk.call('wm', 'iconphoto', root._w, icon) 不过,我不知道你是否能够使你的任务栏图标改变,直到你编译的代码至少在windows中。你可以使用py2ex或者freeze。我以前用过冻结,我有一个客户桌面和任务栏图标,我用它。

    import tkinter as tk
    
    root = tk.Tk() 
    root.geometry("400x400")
    root.overrideredirect(1)
    root.resizable(False, False)
    root.columnconfigure(0, weight=1)
    root.iconbitmap(default='./Colors/small_red.ico')
    
    
    def close():
        root.destroy()
    
    def minimizeWindow():
        root.withdraw()
        root.overrideredirect(False)
        root.iconify()
    
    def check_map(event): # apply override on deiconify.
        if str(event) == "<Map event>":
            root.overrideredirect(1)
            print ('Deiconified', event)
        else:
            print ('Iconified', event)
    
    bar_frame = tk.Frame(root)
    bar_frame.grid(row=0, column=0, sticky="ew")
    bar_frame.columnconfigure(0, weight=1)
    icon = tk.PhotoImage(file='./Colors/small_red.gif')
    # This appears to have the same results so not sure what the difference is from iconbitmap.
    # root.tk.call('wm', 'iconphoto', root._w, icon) 
    
    tk.Button(bar_frame, text='x', command=close).grid(row=0, column=1)
    tk.Button(bar_frame, text='-', command=minimizeWindow).grid(row=0, column=2)
    
    root.bind('<Map>', check_map) # added bindings to pass windows status to function
    root.bind('<Unmap>', check_map)
    
    root.mainloop()
    
        2
  •  0
  •   AsmitTheCoder    4 年前

    有两种方法

    如果要在新的/自定义的 Taskbar 就这么做吧this:-

    from tkinter import *
    import tkinter as tk
    import datetime
    import time
    import math
    
    root = tk.Tk() 
    
    def close():
        root.destroy()
    def minimizeWindow():
        root.update_idletasks()
        root.overrideredirect(False)
        root.state('iconic')
        root.attributes('-topmost', True)
        root.overrideredirect(True)
        root.geometry("215x330")
    root.wm_overrideredirect(True)
    root.attributes('-topmost', False)
    root.resizable(False, False)
    canvas = Canvas(root, width = 400, height = 400)
    canvas.pack()
    exit = Button(root, text='x', command = close)
    exitWindow = canvas.create_window(10,10, window=exit)
    minimize = Button(root, text='-', command = minimizeWindow)
    minimizeWindow = canvas.create_window(30,10,window=minimize)
    icon = PhotoImage(file='py.gif')
    root.tk.call('wm', 'iconphoto', root._w, icon)
    root.mainloop()
    

    任务栏 :-

    from tkinter import *
    import tkinter as tk
    import datetime
    import time
    import math
    
    root = tk.Tk() 
    
    root.overrideredirect(1)
    def check(event):
        if str(event) == "<Map event>":
            window.overrideredirect(1)
        else:
            None
    def close():
        root.destroy()
    def minimizeWindow():
        root.withdraw()
        root.overrideredirect(False)
        root.iconify()
    root.overrideredirect(True)
    
    root.resizable(False, False)
    canvas = Canvas(root, width = 400, height = 400)
    canvas.pack()
    exit = Button(root, text='x', command = close)
    exitWindow = canvas.create_window(10,10, window=exit)
    minimize = Button(root, text='-', command = minimizeWindow)
    minimizeWindow = canvas.create_window(30,10,window=minimize)
    icon = PhotoImage(file='py.gif')
    root.tk.call('wm', 'iconphoto', root._w, icon)
    root.bind('<Map>', check_map) 
    root.bind('<Unmap>', check_map)
    root.mainloop() # starts the mainloop