【问题标题】:python tkinter restore window without title barpython tkinter恢复没有标题栏的窗口
【发布时间】:2019-03-13 19:39:00
【问题描述】:

我有一个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。我完全去掉标题栏的原因是为了避免来自操作系统字体和窗口设置的差异。它目前在两个操作系统上都表现出相同的行为。

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

【问题讨论】:

  • 你在用这个图标做什么?它在这里根本不起作用。您是否想保留它并在自己的酒吧中使用它?
  • 该图标似乎在 Linux 中有效,但在 Windows 中无效。我更关心它在第一次启动时在任务栏上显示一个图标,以及覆盖重定向。当它被最小化和恢复时,我不想显示标题栏。
  • 在 Windows 上,我可以在画布上创建一个包含图标的标签。这应该对你有用。这里有理由在框架上使用画布吗?
  • 是的,因为我要使用画布类来绘制一些东西
  • 您仍然可以使用画布绘制一些您不需要将其用于栏的东西。除非你想在吧台上画点什么?如果您确实想在 BAR 上绘图,您仍然可以使用画布执行此操作,只需将画布对象放在条形框架中即可。

标签: python tkinter icons taskbar titlebar


【解决方案1】:

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

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

我还将您的画布更改为框架,因为这样可以更轻松地管理按钮等内容。

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

在这篇文章中看到这个答案:Python 3 tkinter iconbitmap error in ubuntu

更新:

我已经添加了iconbitmaproot.tk.call('wm', 'iconphoto', root._w, icon),但是我不确定您是否能够更改任务栏图标,直到您至少在 Windows 中编译代码。您可以使用 py2exe 或冻结。我以前使用过 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()

【讨论】:

  • 感谢您的回复,非常感谢!我正在尝试使其适用于 Windows 和 linux。我完全去掉标题栏的原因是为了避免来自操作系统字体和窗口设置的差异。在迁移到 Windows 之前,我专注于让它在 ubuntu 上正常工作。我继续在 PC 上测试了该程序。您建议的更改在两个操作系统上表现出与以前相同的行为。我已经编辑了我的主要帖子以反映这一点。
  • @Jamin 在保持覆盖重定向工作时,我的代码在 Windows 上运行良好。我会看看我能对 Icon 和 linux 选项做些什么。
  • @Jamin 我已经更新了 Windows 方面的代码。我仍在审查 linux。
  • @Jamin 我明白我误解了您的需求。我确信我可以编辑我的答案以将图标提供给最小化的任务栏窗口。请记住,在将代码编译为可执行文件之前,您可能无法向 Windows 任务栏提供任务栏图标。从 python 解释器或 IDE 运行代码可能不允许。
  • @Jamin 您不能在 linux 中使用 .ico。尝试使用.xbm 文件。这是我添加的链接答案的问题的一部分。
【解决方案2】:

有两种方式

如果您想在新的/自定义的Taskbar 中恢复它,只需这样做:-

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()

如果你想在 Windows 中恢复 Taskbar:-

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-20
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 2021-11-03
    • 2015-08-27
    相关资源
    最近更新 更多