Hide tkinter window in system tray [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2020-12-31 15:01:44

问题


I am making a program that reminds me birthday of my friends so that I don't forget to wish them. For that I made two two tkinter windows:

1. First one is for entering name and birth date   
2. Second one is for reminding their birthday

I have no problem with second tkinter window but I wanted to hide the first tkinter window in system tray so that it don't open in startup but it do have ability to open whenever I click to the program icon like some programs do eg: f.lux, Internet Downloader Manager(IDM), Windows Antivirus, etc.

For that I have did:

   root.deiconify()

   root.iconify()

which nearly solved by problem (but only nearly). It hid my tkinter window but showed icon in taskbar which I don't want.

I want both tkinter window and its icon hidden (from the taskbar) but I want to view tkinter window icon in system tray so that I can open program anytime I want to.

I want my program to be displayed here by hiding window

My CODE

from tkinter import *


def when_clicked(event):
    """function that gets called whenever entry is clicked"""
    if entry_area_two.get() == 'DD-MM':
        entry_area_two.delete(0, "end")  # delete all the text in the entry
        entry_area_two.insert(0, '')  # Insert blank for user input
        entry_area_two.config(fg='black')


def when_not_clicked(event):
    if entry_area_two.get() == '' or entry_area_two.get() == ' ':
        entry_area_two.insert(0, 'DD-MM')
        entry_area_two.config(fg='grey')


def when_clicked_another(event):
    """function that gets called whenever entry is clicked"""
    if entry_area_one.get() == 'Name':
        entry_area_one.delete(0, "end")  # delete all the text in the entry
        entry_area_one.insert(0, '')  # Insert blank for user input
        entry_area_one.config(fg='black')


def when_not_clicked_another(event):
    if entry_area_one.get() == '' or entry_area_one.get() == ' ':
        entry_area_one.insert(0, 'Name')
        entry_area_one.config(fg='grey')


def get():
    name = entry_area_one.get()
    date = entry_area_two.get()

    print(name, date)


def main():
    global entry_area_one
    global entry_area_two

    root = Tk()
    root.resizable(0, 0)

    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()

    tkinter_width, tkinter_height = 300, 280
    pos_x, pos_y = screen_width - 800, screen_height // 5

    root.geometry('{}x{}+{}+{}'.format(tkinter_width, tkinter_height, pos_x, pos_y))

   text_one = Label(root, text='Name')
   text_two = Label(root, text='Date of Birth')

   entry_area_one = Entry(root, bd=2, width=40)
   entry_area_two = Entry(root, bd=2, width=40)

   add_button = Button(root, text='ADD', height=2, width=34, command=get)
   close_button = Button(root, text='CLOSE', height=2, width=34, command=root.destroy)

   text_one.pack()
   text_two.place(x=18, y=80)

   entry_area_one.insert(0, 'Name')
   entry_area_one.bind('<FocusIn>', when_clicked_another)
   entry_area_one.bind('<FocusOut>', when_not_clicked_another)
   entry_area_one.config(fg='grey')
   entry_area_one.pack()

   entry_area_two.insert(0, 'DD-MM')
   entry_area_two.bind('<FocusIn>', when_clicked)
   entry_area_two.bind('<FocusOut>', when_not_clicked)
   entry_area_two.config(fg='grey')
   entry_area_two.place(x=25, y=125)

   add_button.place(x=24, y=170)
   close_button.place(x=24, y=220)

   text_one.config(font=("Courier", 25), fg='Black', bg='grey')
   text_two.config(font=("Courier", 25), fg='Black', bg='grey')

   root.configure(background='grey')
   root.deiconify()   # This didn't worked
   root.iconify()     # This didn't worked
   root.mainloop()

Can I hide my tkinter window in system tray?


回答1:


As mentioned in this thread, "Tk, the library that Tkinter wraps, does not offer a way to "minimize to taskbar"."

Question: https://mail.python.org/pipermail/python-list/2005-May/295953.html

Answer: https://mail.python.org/pipermail/python-list/2005-May/342496.html



来源:https://stackoverflow.com/questions/54399137/hide-tkinter-window-in-system-tray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!