如何在 guizero/tkinter python 3 中将图标添加回任务栏?
Posted
技术标签:
【中文标题】如何在 guizero/tkinter python 3 中将图标添加回任务栏?【英文标题】:How do I add back the icon to the task bar in guizero/tkinter python 3? 【发布时间】:2021-04-13 14:23:17 【问题描述】:我正在尝试为删除了边框的自定义应用程序窗口重新添加图标。我在 Guizero/Tkinter 中制作了这个。我需要能够重新添加图标,以便它可以显示在任务栏中。该图标需要显示为活动的,以便您可以隐藏/最小化到任务栏中并从那里显示/重新打开窗口。我试过“root.iconify()”。
from guizero import *
app=App(title='Test',bg='#121212',width=750,height=550)
root = app.tk
#Icon does not display when overrideredirect is True
app.tk.iconbitmap("icon.ico")
#Remove window border
root.overrideredirect(True)
#https://***.com/questions/14910858/how-to-specify-where-a-tkinter-window-opens
#base on answer given byRachel Gallen
def center_window(width=750, height=500):
# get screen width and height
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
# calculate position x and y coordinates
x = (screen_width/2) - (width/2)
y = (screen_height/2) - (height/2)
root.geometry('%dx%d+%d+%d' % (width, height, x, y))
#menu bar
Top_box = Box(app,height=35,width='fill', align="top")
Top_box.bg='white'
#window title
left_box = Box(Top_box,height=35,width='fill', align="left")
left_box.bg='#121212'
text = Text(left_box, text=" Test Project",align="left")
text.text_color='gray'
text.tk.padx=15
text.tk.config(font='Helvetica 15 bold')
#end of window title
#Exit/quit button
Quit_button = PushButton(Top_box, text="X", command=quit ,align='right')
Quit_button.bd=0
Quit_button.text_size=22
Quit_button.text_color='purple'
Quit_button.bg='gray'
Quit_button.tk.config(activebackground='black',highlightthickness=10,bd=0,highlightbackground='red')
#Minmize/hide button
Minmize_button = PushButton(Top_box, text="-", command=app.hide ,align='right')
Minmize_button.tk.config(activebackground='green',highlightthickness=2,bd=0,highlightbackground='red',font=("Verdana", 31))
Minmize_button.text_color="purple"
#Content of the window
Canvas_box = Box(app,height='fill',width='fill', align="top")
Canvas_box.bg='green'
Text=Text(Canvas_box,text='Test',align='top',size=26)
Text.text_color='white'
center_window(750, 500)
app.display()
【问题讨论】:
【参考方案1】:虽然不完美,但非常接近。当您最小化窗口时,它会重新添加图标。这是一个完整的自定义窗口,支持拖动。 我集成并更新了一些其他人编写的代码以与 Guizero 1.2.0 一起使用。
from guizero import *
app = App(bg='white',title='Test',width=645,height=275)
#Removes window border and title bar
app.tk.overrideredirect(True)
#app.icon="Shorcut_Icon1.ico"
#minimize system to re-add icon
#https://***.com/questions/52714026/python-tkinter-restore-window-without-title-bar
#base on Answer given by Mike - SMT
def close():
app.destroy()
def minimizeWindow():
app.hide()
app.tk.overrideredirect(False)
app.tk.iconify()
def check_map(event): # apply override on deiconify.
if str(event) == "<Map event>":
app.tk.overrideredirect(1)
print ('Deiconified', event)
else:
print ('Iconified', event)
winControl_container=Box(app,width=app.width,height=35,align='top')
Bottom_line=Box(winControl_container,width=app.width,height=1,align='bottom')
Bottom_line.bg='blue'
closeButton= PushButton(winControl_container, text='X', command=close,align='right')
minButton= PushButton(winControl_container, text='—', command=minimizeWindow,align='right')
#Start of window movement system
#https://***.com/questions/4055267/tkinter-mouse-drag-a-window-without-borders-eg-overridedirect1
#base on answer given by David58
lastClickX = 0
lastClickY = 0
def SaveLastClickPos(event):
global lastClickX, lastClickY
lastClickX = event.x
lastClickY = event.y
def Dragging(event):
x, y = event.x - lastClickX + app.tk.winfo_x(), event.y - lastClickY + app.tk.winfo_y()
app.tk.geometry("+%s+%s" % (x , y))
#Adds window movement/dragging events. Just comment this out if you don't want the window to be able to move.
app.when_left_button_pressed=SaveLastClickPos
app.when_mouse_dragged=Dragging
#End of movement/drag system
app.tk.bind('<Map>', check_map) # added bindings to pass windows status to function
app.tk.bind('<Unmap>', check_map)
#Center window system
#https://***.com/questions/14910858/how-to-specify-where-a-tkinter-window-opens
#base on answer given byRachel Gallen
def center_window(width, height):
# get screen width and height
screen_width = app.tk.winfo_screenwidth()
screen_height = app.tk.winfo_screenheight()
# calculate position x and y coordinates
x = (screen_width/2) - (width/2)
y = (screen_height/2) - (height/2)
app.tk.geometry('%dx%d+%d+%d' % (width, height, x, y))
center_window(app.width,app.height)
app.display()
【讨论】:
【参考方案2】:# This works fine do not adjust code
# works with version 1.1.0
# This is no longer supposed to work with the latest version of guizero.
#
# I have tried with version 1.3.0 which is the
# latest version on 05/01/22
# and it still appears to work without any changes needed
# in comments below.
# You will need to use the command below instead.
# WindowsName.tk.iconbitmap("Icon.ico")
# Imports ---------------
from tkinter import *
from guizero import App
root = Tk()
app = App(title="App window")
root.iconbitmap(app, 'green-finger.ico')
# Root window is shown when the program is run,
# so it must be destroyed it to stop this happening.
root.destroy()
app.display()
【讨论】:
以上是关于如何在 guizero/tkinter python 3 中将图标添加回任务栏?的主要内容,如果未能解决你的问题,请参考以下文章
当代超吸金的行业“Python工程师”,如何快速从Pytho入门到初级Python工程师?