Python TKinter 绑定输入键

Python TKinter binding to enter key

我已经尝试应用其他几个 SO 问题解答中给出的各种示例,但它让我望而却步。

我的目标是能够在按下 "enter" 时向 telnet 服务器发送一行文本。我的步骤是简单地将最后一行(因为之前的 "enter" 被按下)发送到输出面板。

我得到了一组笨拙的 windows 并且似乎在它们上面附加了文本框,但是当我应用任何绑定类型代码时我最终无法在输入文本框中写入。当我删除绑定代码时,我至少可以在任一文本框中键入文本。

这个问题与我的另一个问题有关 query,这就是我现在尝试学习 GUI 编码的原因。

代码,其骨架来自 Thinking in Tkinter

的 tut' 信息

编辑:代码行已更改回复:Tiger 的评论

from Tkinter import *

class MyApp:
    def __init__(self, parent):

        #------ constants for controlling layout of buttons ------
        button_width = 15
        button_padx = "2m"
        button_pady = "1m"
        buttons_frame_padx =  "3m"
        buttons_frame_pady =  "0m"
        buttons_frame_ipadx = "3m"
        buttons_frame_ipady = "0m"
        # -------------- end button constants ----------------

        # First declare ORIGINAL Window area
        self.myParent = parent
        self.myParent.geometry("640x400")

        # Layer ONE
        ### Our First layer/frame is called frameLayerOne
        self.frameLayerOne = Frame(parent, bg="green")
        self.frameLayerOne.pack(expand=YES, fill=BOTH)

        ### We will stack vertically inside frameLayerOne.
        ### Inside frameLayerOne, we will create 
        ### a menu_frame, then a sub frame into which we will
        ### put an output_frame and an input_frame.

        # Layer TWO
        # MENU FRAME - Layer Two
        self.menu_frame = Frame(self.frameLayerOne, borderwidth=5,  relief=RIDGE, bg="cyan")
        self.menu_frame.pack(side=TOP, expand=NO,  padx=0, pady=0, ipadx=0, ipady=0, fill=X)

        MessageMenuFrame="Menu frame.\n"
        # Label(self.menu_frame, text=MessageMenuFrame, justify=LEFT).pack(side=TOP, anchor=W)

        # buttons frame
        self.buttons_frame = Frame(self.menu_frame) # , bg="red"
        self.buttons_frame.pack(side=TOP,
            ipadx=buttons_frame_ipadx,
            ipady=buttons_frame_ipady,
            padx=buttons_frame_padx,
            pady=buttons_frame_pady,)

        # now we add the buttons to the buttons_frame
        self.button1 = Button(self.buttons_frame, command=self.button1Click)
        self.button1.configure(text="CONNECT", background= "green")
        self.button1.focus_force()
        self.button1.configure(width=button_width,
            padx=button_padx,
            pady=button_pady)

        self.button1.pack(side=LEFT)
        self.button1.bind("<Return>", self.button1Click_a)

        self.button2 = Button(self.buttons_frame, command=self.button2Click)
        self.button2.configure(text="QUIT", background="red")
        self.button2.configure(width=button_width,
            padx=button_padx,
            pady=button_pady)

        self.button2.pack(side=RIGHT)
        self.button2.bind("<Return>", self.button2Click_a)

        # SUB FRAME - Layer Two
        self.sub_frame = Frame(self.frameLayerOne) # , bg="red"
        self.sub_frame.pack(side=BOTTOM, expand=YES,  padx=0, pady=0, ipadx=0, ipady=0, fill=BOTH)

        # Layer THREE
        # INPUT FRAME - Layer Three
        self.input_frame = Frame(self.sub_frame, borderwidth=5,  relief=RIDGE, bg="black")
        self.input_frame.pack(side=BOTTOM, expand=NO,  padx=0, pady=0, ipadx=0, ipady=0, fill=X)

        # Text widget for user input to send to server
        self.InputText = Text(self.input_frame, height=4, bg="black", fg="green")
        self.InputText.pack()
        self.InputText.insert(END, "User input here")

        # Moded these two lines on Tiger's advice
        self.input_frame.bindtags(('.input_frame','input_frame','post-class-bindings', '.', 'all'))
        self.input_frame.bind_class('<Return>', self.return_key)

        # OUTPUT FRAME - Layer Three
        self.output_frame = Frame(self.sub_frame, borderwidth=5,  relief=RIDGE, bg="blue")
        self.output_frame.pack(side=BOTTOM, expand=YES,  padx=0, pady=0, ipadx=5, ipady=25, fill=BOTH)

        # Text widget for output from code and server
        self.OutputText = Text(self.output_frame, bg="black", fg="green")
        self.OutputText.pack()
        self.OutputText.insert(END, "server and code output here")


    def return_key(self, event):
        text = "you pressed Return"
        self.InputText.insert(END, text)

    def button1Click(self):
        if self.button1["background"] == "green":
            self.button1["background"] = "yellow"
        else:
            self.button1["background"] = "green"

    def button1Click_a(self, event):
        self.button1Click()


    def button2Click(self):
        self.myParent.destroy()


    def button2Click_a(self, event):
        self.button2Click()


root = Tk()
myapp = MyApp(root)
root.mainloop()

我对您的代码进行了一些更改,标记注释 T3 以提高可见性。这允许您在 ButtonsText 字段上使用 Return,实际上 "connect" 按钮 "rescuing" 焦点。

from Tkinter import *

class MyApp:
    def __init__(self, parent):

        #------ constants for controlling layout of buttons ------
        button_width = 15
        button_padx = "2m"
        button_pady = "1m"
        buttons_frame_padx =  "3m"
        buttons_frame_pady =  "0m"
        buttons_frame_ipadx = "3m"
        buttons_frame_ipady = "0m"
        # -------------- end button constants ----------------

        # First declare ORIGINAL Window area
        self.myParent = parent
        self.myParent.geometry("640x400")

        # Layer ONE
        ### Our First layer/frame is called frameLayerOne
        self.frameLayerOne = Frame(parent, bg="green")
        self.frameLayerOne.pack(expand=YES, fill=BOTH)

        ### We will stack vertically inside frameLayerOne.
        ### Inside frameLayerOne, we will create 
        ### a menu_frame, then a sub frame into which we will
        ### put an output_frame and an input_frame.

        # Layer TWO
        # MENU FRAME - Layer Two
        self.menu_frame = Frame(self.frameLayerOne, borderwidth=5,  relief=RIDGE, bg="cyan")
        self.menu_frame.pack(side=TOP, expand=NO,  padx=0, pady=0, ipadx=0, ipady=0, fill=X)

        MessageMenuFrame="Menu frame.\n"
        # Label(self.menu_frame, text=MessageMenuFrame, justify=LEFT).pack(side=TOP, anchor=W)

        # buttons frame
        self.buttons_frame = Frame(self.menu_frame) # , bg="red"
        self.buttons_frame.pack(side=TOP,
            ipadx=buttons_frame_ipadx,
            ipady=buttons_frame_ipady,
            padx=buttons_frame_padx,
            pady=buttons_frame_pady,)

        # now we add the buttons to the buttons_frame
        self.button1 = Button(self.buttons_frame, command=self.button1Click)
        self.button1.configure(text="CONNECT", background= "green")
        self.button1.focus_force()
        self.button1.configure(width=button_width,
            padx=button_padx,
            pady=button_pady)

        self.button1.pack(side=LEFT)
        # T3 - bound to regular button1Click
        self.button1.bind("<Return>", self.button1Click)

        self.button2 = Button(self.buttons_frame, command=self.button2Click)
        self.button2.configure(text="QUIT", background="red")
        self.button2.configure(width=button_width,
            padx=button_padx,
            pady=button_pady)

        self.button2.pack(side=RIGHT)
        # T3 - bound to regular button2Click
        self.button2.bind("<Return>", self.button2Click)

        # SUB FRAME - Layer Two
        self.sub_frame = Frame(self.frameLayerOne) # , bg="red"
        self.sub_frame.pack(side=BOTTOM, expand=YES,  padx=0, pady=0, ipadx=0, ipady=0, fill=BOTH)

        # Layer THREE
        # INPUT FRAME - Layer Three
        self.input_frame = Frame(self.sub_frame, borderwidth=5,  relief=RIDGE, bg="black")
        self.input_frame.pack(side=BOTTOM, expand=NO,  padx=0, pady=0, ipadx=0, ipady=0, fill=X)

        # Text widget for user input to send to server
        self.InputText = Text(self.input_frame, height=4, bg="black", fg="green")
        self.InputText.pack()
        self.InputText.insert(END, "User input here")

        self.InputText.bind('<Return>', self.return_key)

        # OUTPUT FRAME - Layer Three
        self.output_frame = Frame(self.sub_frame, borderwidth=5,  relief=RIDGE, bg="blue")
        self.output_frame.pack(side=BOTTOM, expand=YES,  padx=0, pady=0, ipadx=5, ipady=25, fill=BOTH)

        # Text widget for output from code and server
        self.OutputText = Text(self.output_frame, bg="black", fg="green")
        self.OutputText.pack()
        self.OutputText.insert(END, "server and code output here")


    def return_key(self, event):
        text = "you pressed Return"
        self.InputText.insert(END, text)

    def button1Click(self, event=None): # T3 - give it a default event of None
        if self.button1["background"] == "green":
            self.button1["background"] = "yellow"
        else:
            self.button1["background"] = "green"
        self.button1.focus_set() # T3 - set focus to this button so Return works

    def button2Click(self, event=None): # T3 - give it a default event of None
        self.myParent.destroy()

root = Tk()
myapp = MyApp(root)
root.mainloop()