【问题标题】:Tkinter - Displaying a fullscreen app without a title bar and with an on screen keyboard (Linux)Tkinter - 显示没有标题栏和屏幕键盘的全屏应用程序(Linux)
【发布时间】:2021-07-27 04:53:44
【问题描述】:

我正在尝试在树莓派上创建全屏 tkinter 应用程序。该应用程序需要有一个屏幕键盘(它在触摸屏上使用)。我也希望没有标题栏(我不希望人们能够关闭应用程序,而且它看起来更干净)。除了使用 zoomed 属性的标题栏之外,我已经能够完成所有这些工作,但它不能与我发现的任何其他删除标题栏的方法结合使用。我正在使用的键盘是带有 raspbian 的 raspberry pi 3B+ 的 florence。以下是我尝试过的案例。

覆盖重定向和全屏属性不允许屏幕键盘工作(它在应用程序后面打开)。

splash 属性非常接近工作,问题是我的 Entry 小部件不起作用(当我单击它们时,光标不会弹出,我实际上可以返回 python 并且光标弹出但输入在 python 中。我认为这是因为该应用程序落后于 Python)。这与 root.geometry 结合使用以全屏显示应用程序。

我找到了两个类似的堆栈溢出问题Updating entry widget using text from onscreen keyboard in tkinterHow do I remove the title bar with tkinter on linux LXDE without overrideredirect or attributes?。第一个问题不需要全屏(这是我当前代码所在的位置),第二个问题不需要键盘。

    root = tk.Tk()
    root.attributes('-zoomed', True)

    #width = root.winfo_screenwidth()
    #height = root.winfo_screenheight()
    #root.geometry("%dx%d+0+0" % (width, height))
    #root.wm_attributes('fullscreen', True)
    #root.overrideredirect(True)
    #root.attributes('-type', 'splash')
    root.mainloop()

【问题讨论】:

  • root.attributes("-type", "splash") 有什么问题?之后尝试使用root.attributes("-topmost", True)。小心并始终添加关闭窗口的方法,这样您就不必重新启动计算机。
  • 我确实有办法关闭它,只是没有包含它,esc 绑定到一个函数来关闭它。至于 root.attributes("-type", "splash"),它不适用于 linux 上的 Entry 小部件,它不允许您输入它们。我也刚刚尝试了 root.attributes("-topmost", True),它给我留下了与 root.attributes('-zoomed', True) 相同的屏幕,带有一个标题栏。
  • 您是否在root.attributes("-type", "splash") 之后添加了root.attributes("-topmost", True)?强制窗口始终位于顶部。它适用于我的 Ubuntu 20.10
  • 这将键盘留在 tkinter 应用程序后面,类似于全屏属性。就像我需要一种方法来告诉它在一切之上(除了键盘)。
  • 键盘是 tkinter 应用程序的一部分,还是与您的应用程序一起运行的单独应用程序?

标签: python tkinter raspberry-pi keyboard fullscreen


【解决方案1】:

有一种解决方法可以使用 splash 属性:您可以将条目上的单击绑定到 entry.focus_force(),然后您可以输入条目。这是一个小例子:

import tkinter as tk

root = tk.Tk()
root.attributes("-fullscreen", True)
root.attributes("-type", "splash")

root.bind_class("Entry", "<1>", lambda ev: ev.widget.focus_force())

entry = tk.Entry(root)
entry.pack()
tk.Button(root, text="Close", command=root.destroy).pack()
root.mainloop()

注意:我使用了一个类绑定到tk.Entry

root.bind_class("Entry", "<1>", lambda ev: ev.widget.focus_force())

使其适用于程序中的所有tk.Entry。但是,如果一个人想使用不同类别的小部件,例如ttk.Entry,类绑定要修改为

root.bind_class(<class name>, "<1>", lambda ev: ev.widget.focus_force())

其中&lt;class name&gt; 将是ttk.Entry 的“TEntry”,ttk.Combobox 的“TCombobox”或tk.Text 的“Text”。

【讨论】:

  • 这很有魅力,非常感谢!作为旁注,我发现这不适用于 tkinter.ttk 条目,这对我来说不是问题,但对于以后看这个的人来说很重要!谢谢!!
  • @CameronGreenwalt 这是因为我绑定了"Entry" 类,为了让它与ttk.Entry 一起工作,你需要绑定到"TEntry" 类。如果您愿意,也可以直接绑定到特定的小部件:entry.bind("&lt;1&gt;", lambda ev: entry.focus_force())
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-24
  • 2018-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-01
相关资源
最近更新 更多