【问题标题】:Toggle tkinter window visibility切换 tkinter 窗口可见性
【发布时间】:2021-01-30 14:26:58
【问题描述】:

当我单击 p 时,我正在尝试切换 tkinter 窗口的可见性。

toggle = True

if keyboard.is_pressed('p'):
    toggle = not toggle

if toggle:
    app.wm_attributes("-alpha", 0)
else:
    app.wm_attributes("-alpha", 1)

【问题讨论】:

标签: python user-interface tkinter


【解决方案1】:

不确定keyboard 是什么,尽管我使用的是keyboard 库。无论如何,您可以为此使用tkinter 本身。这是一个设置您的示例:

from tkinter import *

app = Tk()
toggle = True # Initially true

def check(e):
    global toggle 
    if toggle: # If true
        app.attributes('-alpha',0) # Then hide
        toggle = False # Set it to False
    else: # If not true
        toggle = True 
        app.attributes('-alpha',1) # Bring it back

app.bind('<p>',check) # Bind to the 'p' key.

app.mainloop()

还要注意wm_attributes()attributes() 是相同的。

【讨论】:

  • 感谢您的回答。虽然我有问题。当我单击 p 时窗口消失,但当我再次单击它时它不会出现。
  • @Pera16 非常抱歉,我在复制粘贴时出错,现在重试。
猜你喜欢
  • 1970-01-01
  • 2012-11-08
  • 1970-01-01
  • 2016-08-13
  • 1970-01-01
  • 2020-01-03
  • 1970-01-01
  • 1970-01-01
  • 2015-12-26
相关资源
最近更新 更多