没有标题栏的python tkinter还原窗口 [英] python tkinter restore window without title bar

查看:167
本文介绍了没有标题栏的python tkinter还原窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 tkinter 窗口,从中删除了标题栏,并向其中添加了一个自定义关闭和最小化按钮.程序首次加载时,它不会在任务栏上显示图标.当我单击定制的最小化按钮时,它将在任务栏上创建一个图标.但是,当我单击以还原窗口时,无法再次摆脱标题栏.

I have a tkinter window that I removed the title bar from and added a custom close and minimize button to. When the program first loads it doesn't display an icon to the taskbar. When I click the custom made minimize button it then creates an icon on the taskbar; however, when I click to restore the window I am unable to get rid of the titlebar again.

我希望图标始终显示在任务栏上,并且当程序最小化然后还原时,我希望标题栏仍然从 .overrideredirect(1)中消失.不幸的是,在最小化前后不重置任务栏图标的情况下,我很难重置标志.

I want the icon to always show on the taskbar, and when the program is minimized and then restored I would like the title bar to still be gone from .overrideredirect(1). Unfortunately I'm having trouble resetting the flag before and after minimizing without making the taskbar icon disappear.

请让我知道我在做什么错.谢谢!

Please let me know what I am doing wrong. Thanks!

#!/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.我完全删除标题栏的原因是为了避免OS字体和窗口设置带来的差异.当前在两个操作系统上都表现出相同的行为.

I am trying to make it work for both windows and linux. The reason I'm taking out the title bar altogether is to avoid the differences that come from the OS font and window settings. It's currently exhibiting this same behavior on both Operating Systems.

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

To reiterate, I want the taskbar icon to show up on program launch and I want the program's window to maintain it's titlebar-less state when restored from being minimized.

推荐答案

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

It depends on what operating system you are using. If you are using Windows the below solution should work for you.

我添加了一个将重新应用 overriderdirect 的功能.该函数由我们在根目录上使用的绑定调用.

I have added a function that will reapply the overriderdirect. This function is being called by a bind we used on root.

我也将您的画布更改为框架,因为这样可以更轻松地管理按钮之类的东西.

I have also changed your canvas to a frame as this make it easier to manage things like buttons.

对于Linux,您可能需要使用其他文件类型.在Windows上,您使用.ico;在Linux上,您可能需要使用.xbm.

For linux you may need to use a different file type. On window you use .ico and on linux you may need to use .xbm.

在此信息上查看有关此问题的答案: ubuntu中的Python 3 tkinter iconbitmap错误

See this answer about it on this post: Python 3 tkinter iconbitmap error in ubuntu

更新:

我添加了 iconbitmap root.tk.call('wm','iconphoto',root._w,图标),但是我不确定是否除非您至少在Windows中编译代码,否则您将能够更改任务栏图标.您可以使用py2exe或冻结.我以前使用过冻结功能,并且有一个用于它的客户桌面和任务栏图标.

I have added the iconbitmap and root.tk.call('wm', 'iconphoto', root._w, icon) however I am not sure if you will be able to make your taskbar icon change until you compile the code at least in windows. You can use py2exe or freeze. I have used freeze before and I have a customer desktop and taskbar icon I use for it.

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

这篇关于没有标题栏的python tkinter还原窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆