【问题标题】:Windows without decorations没有装饰的窗户
【发布时间】:2018-01-26 02:36:45
【问题描述】:

在 python 3.6.3 上使用此测试代码

tkinter.Tcl().eval('info patchlevel') 返回'8.6.6' 我的操作系统是 Mint 18.3 Cinnamon 3.6.7,但我希望我的代码是跨平台的。 我使用控制台运行脚本:

from tkinter import *
from tkinter import ttk

def valid(*args):
    print("Dans valid", nom.get())
    root.quit()

root = Tk()

rootW = 300
rootH = 200
x = (root.winfo_screenwidth() - rootW)//2
y = (root.winfo_screenheight() - rootH)//2
root.geometry(f'{rootW}x{rootH}+{x}+{y}')

mainFrame = ttk.Frame(root,  padding="5")
mainFrame.grid(column=0, row=0, sticky=(N, W, E, S))
mainFrame.columnconfigure(0, weight=1)
mainFrame.rowconfigure(0, weight=1)

nom = StringVar()
nomEntry = ttk.Entry(mainFrame,  width=20,  textvariable=nom)
nomEntry.grid(column=2, row=2, sticky=(E))
ttk.Label(mainFrame, text="Inscription").grid(column=2, row=1, sticky=W)
ttk.Label(mainFrame, text="Votre nom").grid(column=1,  row=2)
ttk.Button(mainFrame, text="Validation", command=valid).grid(column=2, row=3, sticky=W)

for child in mainFrame.winfo_children():
    child.grid_configure(padx=5, pady=5)

nomEntry.focus()
#root.update_idletasks()
root.overrideredirect(1)

root.mainloop()
root.destroy()

我想使用一个 tkinter 窗口,它使用 overrideredirect() 方法从该窗口中删除所有窗口管理器装饰。

在这种情况下,我没有更多的装饰,但我不再能够访问Entry 字段,而"Validation" 按钮可操作并退出窗口。

如果我评论 root.overrideredirect(1) 行,一切正常,带有装饰。

我尝试添加行root.update_idletasks(),但这并没有改变我的问题:没关系,但有装饰。

如何获得没有装饰的操作窗口?

【问题讨论】:

  • “在这种情况下,我没有更多的装饰,但我不再能够访问输入字段。” 你的意思是你不能使用输入字段,nomEntry?
  • 虽然上面的代码很简单,但您可以进一步简化它,尝试使用minimal reproducible example 更好地缩小您的代码范围以改进您的问题。
  • “我如何获得一个没有装饰的可操作窗口?” 您提供的代码中的窗口究竟如何可操作?
  • 你在 OSX 上吗?
  • 您的意思是您无法使用输入字段nomEntry? 是的

标签: python tkinter


【解决方案1】:

当我在 3.6.4、tk8.6.6、Win 10 上运行(使用 IDLE)时,我看到一个未装饰的窗口,其中包含两个标签、一个带焦点的输入框和一个按钮。当我输入 "this is a test" 并单击按钮时,我看到外壳中打印了“Dans valid this is a test”。我没有发现任何问题。

NMT tk 8.5 文档声称需要.update_idletasks,但似乎不适用于在 Windows 上使用 tk 8.6 的代码。它还说“此方法可能不适用于某些 Unix 和 MacOS 平台。”这适用于你吗?如果您在 MacOS 上,您可能正在运行 8.5,并且可能是可以更新的有缺陷的旧 8.5 版本。

official tcl/tk 8.6 文档提供了更多技术细节,并且还提到行为取决于系统(尽管我不了解细节):“一些,但不是全部,平台会在其他时间注意到。”

【讨论】:

  • tkinter.Tcl().eval('info patchlevel') give '8.6.6' and python 3.6.3 and my os is Mint 18.3 Cinnamon 3.6.7
【解决方案2】:

我想我找到了一个解决方案,无论如何在 linux 上都有效(在 windows 和 OsX 上测试)

root = Tk()
root.wm_attributes('-type', 'splash')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多