具有Overrideredirect和全屏的Tkinter条目小部件 [英] Tkinter Entry Widget with Overrideredirect and Fullscreen

查看:230
本文介绍了具有Overrideredirect和全屏的Tkinter条目小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这不是重复的文章,但是我还没有在整个Google和论坛中看到我的问题的答案.

I hope this is not a double post but I have not seen the answer to my question specifically throughout Google and the forums.

基本上由于某种原因,当我创建一个Entry小部件时;当我通过self.attributes('-fullscreen',True)将GUI设置为全屏时,以及同时启用了self.overrideredirect(True)时,它也不允许用户通过键盘进行输入. (我要使用信息亭模式的GUI,因此标准用户无法退出界面)

Basically for some reason, when I create an Entry widget; it will not allow for user input from the keyboard when I set my GUI to be fullscreen through self.attributes('-fullscreen', True) and when self.overrideredirect(True) is enabled as well. (I am going for a kiosk mode style GUI so a standard user cannot exit the interface)

我在这里浏览了此链接( tkinter:无法进入条目窗口小部件),发现他们的问题是相似但不完全相同.我了解在Mac上存在一些问题,但是必须有其他方法才能执行此任务.当我在运行Raspbian的Raspberry Pi上运行此命令时,也会发生此问题.如果全屏未启用,但覆盖重定向处于活动状态,则可以使用;并且当替代重定向未处于活动状态但全屏处于活动状态时,它也可以工作.

I went through this link here (tkinter: can't enter into entry widget) and found that their issue was similar but not exactly the same. I understand that there are some issues with this on Mac but there must be some other way to perform this task. This issue also occurred when I ran this on Raspberry Pi running Raspbian. When fullscreen is not active but override redirect is active, it works; and when override redirect is not active but fullscreen is active it also works.

我尝试使用小部件及其停靠的框架的focus_force()和focus_set()来设置焦点,但是那也不起作用.

I have tried set focus with focus_force() and focus_set() for the widget as well as the frame it is resting in however that did not work either.

下面提供了我的相关代码.如果需要我的更多代码,请告诉我.这段代码的目的是在程序的其余部分中调用用户名/密码登录方法.

My relavent code is provided below. If more of my code is needed, just let me know. The purpose of this piece of code is a username/password login method that will get called throughout the rest of my program.

我正在Mac OS Sierra 10.12.5上运行PyCharm Community Edition 2017.2.预先感谢!

I am running PyCharm Community Edition 2017.2 on Mac OS Sierra 10.12.5. Thanks in advance!

CODE

    def admin(self, root_1):
        master = tk.Tk()
        text = ""

        master.title("Administrator Login")
        w_0, h_0 = master.winfo_screenwidth(), master.winfo_screenheight()
        master.geometry("%dx%d+0+0" % (w_0, h_0))

        f1 = tk.Frame(master, width=w_0, height=h_0, background="red")
        f2 = tk.Frame(master, width=w_0, height=h_0, background="blue")

        f1.pack(fill="both", expand=True)
        f2.place(in_=f1, anchor="c", relx=.5, rely=.5)

        master.wm_attributes('-topmost', 'True')
        master.overrideredirect(True)

        instruction_label = tk.Label(f2, text="Please type your username and password", fg="red", bg="blue",
                                     font=("Helvetica", 26))
        instruction_label.grid(row=0, columnspan=2, padx=20, pady=20)
        user_label = tk.Label(f2, text="Username", font=("Helvetica", 16), bg="red")
        user_entry = tk.Entry(f2, bg="red")
        pass_label = tk.Label(f2, text="Password", font=("Helvetica", 16), bg="red")
        pass_entry = tk.Entry(f2, bg="red", show="*")

        user_label.grid(row=1, column=0, sticky=("E", "W"), padx=20, pady=10)
        user_entry.grid(row=1, column=1, sticky="W", padx=20, pady=10)
        pass_label.grid(row=2, column=0, sticky=("E", "W"), padx=20, pady=10)
        pass_entry.grid(row=2, column=1, sticky="W", padx=20, pady=10)

        user_entry.focus_force()

        submit_button = tk.Button(f2, text="Submit", command=lambda: correct_login(text), bg="red", width = 20, height = 4)
        submit_button.grid(row=3, column=0, sticky=("N", "S", "E"), padx=20, pady=20)
        quit_button = tk.Button(f2, text="Quit", command=destroy, bg="red", width = 20, height = 4)
        quit_button.grid(row=3, column=1, sticky=("N", "S", "W"), padx=20, pady=20)

        user_entry.delete(0)
        user_entry.insert(0, "")

        pass_entry.delete(0)
        pass_entry.insert(0, "")

        return

推荐答案

我在您的代码中看到一个应该修正的危险标记(它不会解决您的问题,但是是免费的建议).您要将root传递给您的admin方法,然后再次调用Tk() 以创建您正在使用的顶层.这不是一个好习惯.只需使用命令Toplevel.请参阅下面的示例代码.

I see a red flag in your code that should be fixed (it won't solve your problem, but it's free advice). You're passing in root to you admin method, and then you're calling Tk() again to create the toplevel that you're working with. This is not a good practice. Just use the command Toplevel. See my sample code below.

您所看到的帖子对于MAC上关于overrideredirect的内容是一致的(并且显然是正确的).顶级窗口上的此属性告诉窗口管理器忽略非父级窗口上的某些事件.显然,MAC不会将按键和释放事件发送到未父级顶级窗口中的窗口小部件.

The posts you've seen are consistent (and correct apparently) for the MAC regarding overrideredirect. This attribute on a toplevel window tells the window manager to ignore some events on unparented windows. Apparently, the MAC does not send keypress and release events to widgets within an unparented toplevel window.

为此,一种解决方法是允许窗口成为父窗口(正常),但是将"WM_DELETE_WINDOW"协议设置为拦截窗口关闭的函数或方法.我只发布您的admin方法的顶部.

One workaround for this would be to allow the window to be parented (normal), but set the "WM_DELETE_WINDOW" protocol to a function or method to intercept window closure. I'm only posting the top portion of your admin method.

def admin(self, root_1):
    master = Toplevel()
    text = ""

    master.title("Administrator Login")
    w_0, h_0 = master.winfo_screenwidth(), master.winfo_screenheight()
    master.geometry("%dx%d+0+0" % (w_0, h_0))
    master.protocol("WM_DELETE_WINDOW", self.dontDeleteWindow)


def dontDeleteWindow(self):
    print("User tied to close window")

这篇关于具有Overrideredirect和全屏的Tkinter条目小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆