Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python输入行将不会在终端中获取键盘输入类型_Python_User Interface_Tkinter_Password Protection_Tkinter Entry - Fatal编程技术网

Python输入行将不会在终端中获取键盘输入类型

Python输入行将不会在终端中获取键盘输入类型,python,user-interface,tkinter,password-protection,tkinter-entry,Python,User Interface,Tkinter,Password Protection,Tkinter Entry,在过去的几周里,我一直在做一个项目,通过无线连接与一个工业机器人进行交互,并通过Raspberry Pi和python使用REST协议。我对Python很新,但熟悉C++,所以我对编程不陌生。p> 我已经用python中的Tkinter创建了一个GUI(多亏了您的帮助),我正在尝试创建一种kiosk模式。我已经让GUI启动全屏,但没有关闭它的选项。我一直坚持的是,我试图在GUI中的一个弹出窗口中创建一个输入行,当输入正确的密码字符串时,它将关闭GUI,以便管理员或授权人员能够在需要时访问其余的R

在过去的几周里,我一直在做一个项目,通过无线连接与一个工业机器人进行交互,并通过Raspberry Pi和python使用REST协议。我对Python很新,但熟悉C++,所以我对编程不陌生。p> 我已经用python中的Tkinter创建了一个GUI(多亏了您的帮助),我正在尝试创建一种kiosk模式。我已经让GUI启动全屏,但没有关闭它的选项。我一直坚持的是,我试图在GUI中的一个弹出窗口中创建一个输入行,当输入正确的密码字符串时,它将关闭GUI,以便管理员或授权人员能够在需要时访问其余的Raspberry Pi文件。我相信我知道如何使用root.quit()方法(父窗口是root)使这个过程工作,但输入行甚至不会显示任何文本。即使我的GUI是全屏的,我输入的所有文本都是在终端中输入的。下面是我试图获取输入行的弹出窗口的特定代码部分,其中包含注释:

################## Status Window ##################
def statusWindow():                                                     #defines function for popup window for additional options
    window = Toplevel(root)                                             #creates variable to place widgets in window
    window.title('Status')                                              #makes window title for specific window frame
    w, h = window.winfo_screenwidth(), window.winfo_screenheight()      #aquires dimensions from display size
    window.geometry("%dx%d+0+0" % (w, h))                               #sets window size to aquired dimensions
    window.overrideredirect(True)                                       #removes top bar and exit button from parent window frame

    batteryButton = Button(window, text="Battery", fg="green", command=lambda: showBattery(window), width=35, height=12)        #defines criteria for battery button in new window
    statusButton = Button(window, text="Overview", fg="green", command=lambda: showQueueStatus(window), width=35, height=12)    #defines criteria for status button in new window
    returnButton = Button(window, text="Return...", fg="green", command=window.destroy, width=35, height=12)                    ##defines criteria for return button in new window
    passEntry = Entry(window, show="*")

    batteryButton.grid(row=1, column=0, padx=10, pady=5)        #makes button viewable in specified orientation                                                                
    statusButton.grid(row=2, column=0, padx=10, pady=5)         #makes button viewable in specified orientation
    returnButton.grid(row=3, column=0, padx=10, pady=5)         #makes button viewable in specified orientation
    passEntry.grid(row=4, columnspan=10, sticky=W)

    robotName(window)       #call robotName function to display robot name
    winReturn(window)       #call winReturn function to create return label

def robotName(winFrame):                                #define function to display robot name in status window
    label = Label(winFrame, text="Robot Name: ")        #defines variable label for visual display
    name = robot.robot_name()                           #gets robot name from robot and stores it in name variable
    nameLabel = Label(winFrame, text=name)              #defines variable label for visual to hold robot name as string        

    label.grid(row=0, column=0, padx=10, pady=5)        #makes label display in popup window
    nameLabel.grid(row=0, column=3, padx=10, pady=5)    #makes robot name display in popup window

def showBattery(winFrame):                                  #define function to display current battery percentage in popup window
    battery = robot.battery_percentage()                    #retrieves current battery status from robot and stores it in battery variable
    battery = str(battery) + '%'                            #redefines battery variable as previous battery variable string with percentage
    batteryLabel = Label(winFrame, text=battery)            #defines battery label with specified criteria
    batteryLabel.grid(row=1, column=3, padx=10, pady=5)     #displays battery label in popup window

def showQueueStatus(winFrame):                              #define function to display current robot mission queue item status
    queue = robot.robot_state_text()                        #retrieves current queue item status and stores in queue variable
    qStatusLabel = Label(winFrame, text=queue)              #defines queue status label with specified criteria
    qStatusLabel.grid(row=2, column=3, padx=10, pady=5)     #displays queue status label in popup window

def winReturn(winFrame):                                        #define function to display label for return button
    returnLabel = Label(winFrame, text="Close out of window")   #defines return label with specified criteria
    returnLabel.grid(row=3, column=3, padx=10, pady=5)          #displays return label in popup window
我在statusWindow()定义中放置了入口行,因为这是从程序的其余部分调用的方法。如果你有什么想法,为什么不让我在输入行,你的建议将不胜感激。如果需要更多的代码示例,我可以发布更多,但我一直在努力缩短这篇文章。

你可以

passEntry.bind('<Return>', myfunc)

如果看不到您的程序是如何工作的,您实际上不需要创建一个顶级窗口来请求管理员访问。您可以使用tkinters simpledialog
askstring
函数请求密码,然后使用
iconify()
最小化窗口

看看这个例子

from tkinter import *
from tkinter import simpledialog
# from Tkinter import * # for python 2.x
# import tkSimpleDialog as simpledialog # for python 2.x

root = Tk()
root.attributes("-fullscreen", True)# fullscreen without the standard window buttons

def check_admin_password():
    # use askstring here to verify password.
    pass_attempt = simpledialog.askstring("Verifying access",
                                           "Please enter Admin password")
    if pass_attempt == "password":
        root.iconify() # used whatever your instance of Tk() is here in place of root.

admin_minimize_button = Button(root, text = "Minimize window", command = check_admin_password)
admin_minimize_button.pack()

root.mainloop()

嗨,J.Kors。这里有很多代码,如果您能提供一个。您的代码中可能存在导致此问题的内容,如果创建MCVE,您可能会自己发现问题。如果问题仍然存在,那么它将为我们提供一些可以测试的东西。我同意我的代码中有一些东西禁止它允许在输入行中键入用户文本。我已经创建了第二个较小的程序,只适用于入口行。我猜这与我使用的Tkinter框架有关。这可能是小部件的焦点吗?我没有看到您显示的代码中有任何内容会导致用户在控制台而不是输入字段中键入内容。用户是否有鼠标可与之交互或是触摸屏底座?它是一个触摸屏底座GUI,但我计划让管理员可以访问键盘,因为它是在Raspberry Pi上运行的python GUI。您可以尝试
focus\u force()
在条目widgetI上,我尝试了这段代码,看看是否可以使用其中的任何一个,但是当我试图单独执行它时,出现了一个错误,它无法导入simpledialog。我对导入行进行了注释,它打开时只需一个最小化按钮,但没有调用任何命令或方法。我假设发生了这种情况,因为它无法导入simpledialog.@J.kors您使用的是什么版本的python?我在Raspberry Pi 3上使用的是python 2.7。作为对您上面其他评论的回应,我刚刚尝试使用force_focus(),但它仍然奇怪地在terminal中接受键盘输入。@J.kors然后使用
import-tkSimpleDialog
在python 2和python 3之间的导入是不同的。如果您想使用我提供的确切示例,可能需要执行
将tkSimpleDialog导入为simpledialog
。是的,这就是问题所在,我认为如果我在我的示例中实现它,您在这里所做的工作将很好。我将尝试一下,并在稍后发布结果。
from tkinter import *
from tkinter import simpledialog
# from Tkinter import * # for python 2.x
# import tkSimpleDialog as simpledialog # for python 2.x

root = Tk()
root.attributes("-fullscreen", True)# fullscreen without the standard window buttons

def check_admin_password():
    # use askstring here to verify password.
    pass_attempt = simpledialog.askstring("Verifying access",
                                           "Please enter Admin password")
    if pass_attempt == "password":
        root.iconify() # used whatever your instance of Tk() is here in place of root.

admin_minimize_button = Button(root, text = "Minimize window", command = check_admin_password)
admin_minimize_button.pack()

root.mainloop()