簡體   English   中英

使用Enter鍵的Tkinter Python,打開新窗口

[英]Tkinter Python using Enter Key, open the new window

我正在使用Python Tkinter,我有輸入文本框和確定按鈕。 現在,我已經實現了Button邏輯,例如,如果User Input = 1,則在按下OK Button之后,它將打開新窗口。 現在,對於相同的實現,不是要單擊“確定”按鈕,而是要從“鍵盤”輸入“ Enter鍵”並打開新窗口。

我不知道如何進行。

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()

重要的是要記住,如果使用“命令”參數將處理程序函數附加到按鈕,則該函數不能接受任何參數。 另一方面,如果使用bind()附加處理程序函數,則該函數必須帶有一個參數。 為了避免這種沖突,請在函數中使用默認參數'event = None'。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM