Tkinter Python使用Enter键打开新窗口
我使用的是PythonTkinter,我有输入文本框和OK按钮。现在我已经实现了按钮逻辑,比如,如果用户输入=1,在按下OK按钮后,它将打开新窗口。现在,对于相同的实现,我不想按OK按钮,而是想从键盘输入“回车键”并打开新窗口 我不知道如何进行Tkinter Python使用Enter键打开新窗口,python,tkinter,Python,Tkinter,我使用的是PythonTkinter,我有输入文本框和OK按钮。现在我已经实现了按钮逻辑,比如,如果用户输入=1,在按下OK按钮后,它将打开新窗口。现在,对于相同的实现,我不想按OK按钮,而是想从键盘输入“回车键”并打开新窗口 我不知道如何进行 from Tkinter import * def UserInput(): global UI_MainForm UI_MainForm = Tk() UI_MainForm.resizable(1,0) UI_MainForm.geometry
from Tkinter import *
def UserInput():
global UI_MainForm
UI_MainForm = Tk()
UI_MainForm.resizable(1,0)
UI_MainForm.geometry("300x450+0+0")
UI_MainForm.title("ChCM 1.25 Diagnostics Main Menu")
# LabelFrame to add all the Menu menu Display items
labelframe = LabelFrame(UI_MainForm,text="Please select the below options:",width=300, height=100,bd = 2)
labelframe.pack(fill="both")
labelframe.config(relief=RIDGE)
# LabelFrame to add the User input text box and buttons
labelframe1 = LabelFrame(UI_MainForm,text="User Input:",width=300, height=100,bd = 2)
labelframe1.pack(pady=8,fill="both")
labelframe1.config(relief=FLAT)
#Entry the text and display
global entrytext
entrytext = StringVar()
entry = Entry(labelframe1,textvariable=entrytext,width=35)
entry.pack(padx = 1, pady = 5)
MainMenuDisplay = [{"ID": 1,"Description": "Display Data1"},
{"ID": 2,"Description": "Display Data2"}]
for menu in MainMenuDisplay:
temp_text = '{0}. {1}'.format(menu['ID'], menu['Description'])
Label(labelframe, text=temp_text).pack(anchor = W)
ButtonOK = Button(labelframe1, text = "OK", command =OnButtonOK, width =15)
ButtonOK.pack(side = LEFT, padx = 15, pady = 15)
UI_MainForm.mainloop()
return;
def OnButtonOK():
UI_MainForm.withdraw()
Input = int(entrytext.get())
if (Input == 1):
Data1_Menu_Display();
elif (Input == 2):
Data2_Menu_Display();
else:
print "The Input is not valid"
return;
def Data1_Menu_Display():
# self.withdraw()
global top
top = Toplevel(UI_MainForm)
top.geometry("300x450+0+0")
top.title("Display Data1")
return;
def Data2_Menu_Display():
# self.withdraw()
global top
top = Toplevel(UI_MainForm)
top.geometry("300x450+0+0")
top.title("Display Data1")
return;
def Main():
UserInput()
Main()
请尝试以下代码:
import tkinter
class Application:
def __init__(self, master):
self.master = master
self.master.title('GUI')
self.master.geometry('200x100')
self.label = tkinter.Label(master, text='Enter some text:')
self.label.pack()
self.entry = tkinter.Entry(master)
self.entry.bind('<Return>', self.some_action)
self.entry.pack()
self.button = tkinter.Button(master, text='Ok', command=self.some_action)
self.button.pack()
self.output = tkinter.Label(master, text='Your text is: ')
self.output.pack()
def some_action(self, event=None):
""" It's very important to remember that if you use the 'command' argument
to attach a handler function to a button, then the function cannot
take any arguments. On the other hand, if you use bind() to attach
a handler function, the function must take one argument.
To avoid this conflict, use default argument 'event=None'. """
self.output.config(text='Your text is: ' + self.entry.get())
root = tkinter.Tk()
app = Application(root)
root.mainloop()
导入tkinter
班级申请:
定义初始(自我,主):
self.master=master
self.master.title('GUI')
自主几何(“200x100”)
self.label=tkinter.label(master,text='Enter some text:')
self.label.pack()
self.entry=tkinter.entry(主)
self.entry.bind(“”,self.some_action)
self.entry.pack()
self.button=tkinter.button(master,text='Ok',command=self.some\u action)
self.button.pack()
self.output=tkinter.Label(master,text='您的文本是:')
self.output.pack()
定义一些动作(自我,事件=无):
“”“记住,如果使用'command'参数
若要将处理程序函数附加到按钮,则该函数无法
接受任何参数。另一方面,如果使用bind()附加
作为处理函数,该函数必须具有一个参数。
若要避免此冲突,请使用默认参数“event=None”
self.output.config(text='您的文本是:'+self.entry.get())
root=tkinter.Tk()
app=应用程序(根)
root.mainloop()
请务必记住,如果使用“command”参数将处理程序函数附加到按钮,则该函数不能接受任何参数。另一方面,如果使用bind()附加处理函数,则该函数必须具有一个参数。若要避免此冲突,请在函数中使用默认参数“event=None”。请尝试以下代码:
import tkinter
class Application:
def __init__(self, master):
self.master = master
self.master.title('GUI')
self.master.geometry('200x100')
self.label = tkinter.Label(master, text='Enter some text:')
self.label.pack()
self.entry = tkinter.Entry(master)
self.entry.bind('<Return>', self.some_action)
self.entry.pack()
self.button = tkinter.Button(master, text='Ok', command=self.some_action)
self.button.pack()
self.output = tkinter.Label(master, text='Your text is: ')
self.output.pack()
def some_action(self, event=None):
""" It's very important to remember that if you use the 'command' argument
to attach a handler function to a button, then the function cannot
take any arguments. On the other hand, if you use bind() to attach
a handler function, the function must take one argument.
To avoid this conflict, use default argument 'event=None'. """
self.output.config(text='Your text is: ' + self.entry.get())
root = tkinter.Tk()
app = Application(root)
root.mainloop()
导入tkinter
班级申请:
定义初始(自我,主):
self.master=master
self.master.title('GUI')
自主几何(“200x100”)
self.label=tkinter.label(master,text='Enter some text:')
self.label.pack()
self.entry=tkinter.entry(主)
self.entry.bind(“”,self.some_action)
self.entry.pack()
self.button=tkinter.button(master,text='Ok',command=self.some\u action)
self.button.pack()
self.output=tkinter.Label(master,text='您的文本是:')
self.output.pack()
定义一些动作(自我,事件=无):
“”“记住,如果使用'command'参数
若要将处理程序函数附加到按钮,则该函数无法
接受任何参数。另一方面,如果使用bind()附加
作为处理函数,该函数必须具有一个参数。
若要避免此冲突,请使用默认参数“event=None”
self.output.config(text='您的文本是:'+self.entry.get())
root=tkinter.Tk()
app=应用程序(根)
root.mainloop()
请务必记住,如果使用“command”参数将处理程序函数附加到按钮,则该函数不能接受任何参数。另一方面,如果使用bind()附加处理函数,则该函数必须具有一个参数。为避免此冲突,请在函数中使用默认参数“event=None”。请提供一个最小、完整且可测试的示例。请重新格式化。只需将
事件绑定到您的root
小部件。更多我开始知道如何使用bind,但不知道如何在我的代码中实现。这是正确的方法:labelframe1.bind(“,OnButtonOK”)。它不起作用我使用UI_MainForm.bind(“”,OnButtonOK)。现在可以使用了。请提供一个最小、完整且可测试的示例。请重新格式化。只需将
事件绑定到您的root
小部件。更多我开始知道如何使用bind,但不知道如何在我的代码中实现。这是正确的方法:labelframe1.bind(“,OnButtonOK”)。它不起作用我使用UI_MainForm.bind(“”,OnButtonOK)。现在开始工作了