【问题标题】:Why isn't the `.deiconify` function working? [closed]为什么 .deiconify 函数不起作用? [关闭]
【发布时间】:2020-08-20 14:32:56
【问题描述】:

我正在制作一个名为 NutShellOs 的操作系统模拟。

当我单击任务栏图标时,它应该会反弹,就像普通操作系统一样。但是,当我单击它时,它第一次起作用,但之后它就反弹了。

这是我的代码:

def clicked_on_text_editor(event = "<Button-1>"):
                Text1.withdraw()
                #Port.coords(text_editor_logo_on_port, 55, Desktop.winfo_screenheight() + (0.1 * (0.1 * Desktop.winfo_screenheight())), 105, Desktop.winfo_screenheight())

                def clicked_again(event = "<Button-1>"):
                    #Port.coords(text_editor_logo_on_port, 55, 0, 105, (0.1 * Desktop.winfo_screenheight()))
                    Text1.deiconify()
                Port.bind("<Button-1>", clicked_again)
            Port.bind("<Button-1>", clicked_on_text_editor)

顺便说一句,这不是完整的代码。

【问题讨论】:

  • edit 您的问题包含完整的minimal reproducible example。另外,让我们知道您为调试此所做的工作。您确定正在调用clicked_again 吗?你确定Text1 就是你认为的那样吗?
  • 我添加了一个最小的可重现示例,并且我已经调试了一点。@BryanOakley
  • “我已经调试了一点”并没有告诉我们任何事情。你做了什么?此外,您的最小可重现示例并不完整。我们不能运行它。我们不知道Text1 是什么,或者Port 是什么,并且缩进被破坏了。
  • @BryanOakley 然后检查“完整代码”链接。在那里我给出了整个代码来运行。
  • @BryanOakley 另外,我添加了 print 语句来进行一些调试,但这并不是很有帮助。

标签: python tkinter operating-system


【解决方案1】:

示例代码没有提供太多方向,google驱动链接不起作用。

所以基本上这只是一个猜测,我猜你想隐藏/显示Toplevel

由于某种原因似乎有效:

import tkinter as tk

def clicked_on_text_editor(event = "<Button-1>"):
    Text1.withdraw()

    def clicked_again(event = "<Button-1>"):
        Text1.deiconify()
        Port.bind("<Button-1>", clicked_on_text_editor)

    Port.bind("<Button-1>", clicked_again)

root = tk.Tk()

Text1 = tk.Toplevel()

Port = tk.Label(root, text = "Total Guess")
Port.pack(padx = 50, pady = 20)

Port.bind("<Button-1>", clicked_on_text_editor)

root.mainloop()

我不会那样做,我可能会这样做:

import tkinter as tk

def clicked_on_text_editor(e):
    if e.widget.toggle:
        Text1.withdraw()
    else:
        Text1.deiconify()
    e.widget.toggle = not e.widget.toggle # Changes True to False, False to True.

root = tk.Tk()

Text1 = tk.Toplevel()

Port = tk.Label(root, text = "Total Guess")
Port.toggle = True # Assign a toggle attribute, can be anything not currently an attribute.
Port.pack(padx = 50, pady = 20)

Port.bind("<Button-1>", clicked_on_text_editor)

root.mainloop()

【讨论】:

    猜你喜欢
    • 2020-09-05
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    • 2018-02-15
    相关资源
    最近更新 更多