【问题标题】:Hide tkinter window in system tray [duplicate]在系统托盘中隐藏tkinter窗口[重复]
【发布时间】:2019-06-21 06:53:27
【问题描述】:

我正在制作一个程序来提醒我朋友的生日,这样我就不会忘记祝福他们。为此,我制作了两个两个 tkinter 窗口:

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

我对第二个 tkinter 窗口没有任何问题,但我想隐藏系统托盘中的第一个 tkinter 窗口,这样它就不会在启动时打开,但它确实有能力在我像某些程序一样单击程序图标时打开例如:f.lux、Internet Downloader Manager(IDM)、Windows Antivirus 等。

为此我做了:

   root.deiconify()

   root.iconify()

这几乎被问题解决了(但只是几乎)。它隐藏了我的 tkinter 窗口,但在任务栏中显示了我不想要的图标。

我希望隐藏 tkinter 窗口及其图标(从任务栏),但我想在系统托盘中查看 tkinter 窗口图标,以便我可以随时打开程序。

我希望我的程序通过隐藏窗口显示在这里

我的代码

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

我可以在系统托盘中隐藏我的 tkinter 窗口吗?

【问题讨论】:

  • 另见pystray
  • 这显然不是链接问题的完全重复,因为这专门针对 tkinter。目前的答案似乎足够了,没有办法。标记为重复以将人们引向类似问题似乎是不合适的。

标签: python tkinter window


【解决方案1】:

正如该线程中所述,“Tk,Tkinter 包装的库,不提供“最小化到任务栏”的方法。”

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

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

【讨论】:

  • 除了做同样的事情还有其他选择吗?其他软件如何做同样的事情?
  • @markkeven 可能是.withdraw() root 的方法?如此处所述 - stackoverflow.com/questions/1406145/…
  • 它和 root.deiconify() root.iconify() 做了同样的事情。你能告诉我其他软件如何在系统托盘中最小化吗?
  • 我不明白的问题,你想知道低级Windows界面的实现细节或支持最小化到系统托盘的Tkinter替代品吗?
  • 其实是的。我想知道如何将程序最小化到系统托盘而不是最小化到任务栏。如果可以使用 tkinter 来完成,那就太好了(正如你告诉我的那样不可能)。你能推荐我其他可以解决我问题的包吗?
猜你喜欢
  • 2016-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多