如何将图标添加回 guizero/tkinter python 3 中的任务栏?

问题描述

我正在尝试为已移除边框的自定义应用程序窗口添加图标。我在 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)

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,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,highlightbackground='red',font=("Verdana",31))
Minmize_button.text_color="purple"


#Content of the window
Canvas_Box = Box(app,height='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()

解决方法

虽然不完美,但已经很接近了。当您最小化窗口时,它会重新添加图标。这是一个完整的自定义窗口,支持拖动。 我集成并更新了一些其他人制作的代码以与 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://stackoverflow.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,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://stackoverflow.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://stackoverflow.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()

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...