Python 输入行将不会获得键盘输入 - 而是在终端中输入
Python Entry Line Will Not Get Keyboard Input - Types in Terminal Instead
过去几周我一直在做一个项目,通过 Raspberry Pi 和 python 通过无线连接和使用 REST 协议与工业机器人进行交互。我是 python 的新手,但熟悉 C++,所以我对编程并不陌生。
我已经在 python 中使用 Tkinter 创建了一个 GUI(已经感谢您的大量帮助),并且我正在尝试创建一种信息亭模式。我已经让 GUI 启动全屏,但没有关闭它的选项。我坚持的是,我试图在我的 GUI 中的一个弹出窗口 windows 中创建一个输入行,当输入正确的密码字符串时,它将关闭 GUI 以供管理员或授权人员使用如果需要,访问 Raspberry Pi 文件的其余部分。我相信我知道如何使用 root.quit() 方法(父 window 是根)使这个过程工作,但输入行甚至不会显示任何文本。我输入的所有文本都是在终端中输入的,即使我的 GUI 是全屏的。这是代码的特定部分,其中包含我试图让输入行位于的弹出窗口 window 的注释:
################## 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() 定义中,因为这是从我的程序的其余部分调用的方法。如果您对为什么不让我输入输入行有任何想法,我们将不胜感激。如果需要我的代码的更多示例,我可以 post 更多,但我试图让这个 post 简短。
你可以做一个
passEntry.bind('<Return>', myfunc)
调用密码检查功能,决定要做什么等。如果您需要隐藏全屏 gui,请尝试
root.tk.call('wm', 'withdraw', root)
如果不了解您的程序是如何工作的,您实际上不需要创建顶层 window 来请求管理员访问权限。您可以使用 tkinters simpledialog askstring
函数要求输入密码,然后使用 iconify()
最小化 window.
看看这个例子。
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()
过去几周我一直在做一个项目,通过 Raspberry Pi 和 python 通过无线连接和使用 REST 协议与工业机器人进行交互。我是 python 的新手,但熟悉 C++,所以我对编程并不陌生。
我已经在 python 中使用 Tkinter 创建了一个 GUI(已经感谢您的大量帮助),并且我正在尝试创建一种信息亭模式。我已经让 GUI 启动全屏,但没有关闭它的选项。我坚持的是,我试图在我的 GUI 中的一个弹出窗口 windows 中创建一个输入行,当输入正确的密码字符串时,它将关闭 GUI 以供管理员或授权人员使用如果需要,访问 Raspberry Pi 文件的其余部分。我相信我知道如何使用 root.quit() 方法(父 window 是根)使这个过程工作,但输入行甚至不会显示任何文本。我输入的所有文本都是在终端中输入的,即使我的 GUI 是全屏的。这是代码的特定部分,其中包含我试图让输入行位于的弹出窗口 window 的注释:
################## 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() 定义中,因为这是从我的程序的其余部分调用的方法。如果您对为什么不让我输入输入行有任何想法,我们将不胜感激。如果需要我的代码的更多示例,我可以 post 更多,但我试图让这个 post 简短。
你可以做一个
passEntry.bind('<Return>', myfunc)
调用密码检查功能,决定要做什么等。如果您需要隐藏全屏 gui,请尝试
root.tk.call('wm', 'withdraw', root)
如果不了解您的程序是如何工作的,您实际上不需要创建顶层 window 来请求管理员访问权限。您可以使用 tkinters simpledialog askstring
函数要求输入密码,然后使用 iconify()
最小化 window.
看看这个例子。
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()