2015-04-21 108 views
0

我試過應用其他幾個SO問題答案中給出的各種示例,但它躲過了我。Python TKinter綁定輸入密鑰

我的目標是當按下「enter」鍵時能夠將一行文本發送到telnet服務器。我的步驟是簡單地將最後一行(因爲前一個「輸入」被按下)發送到輸出面板。

我已經得到了一套笨拙的窗口,似乎已經附加了文本框,但是當我應用任何綁定類型代碼時,我最終無法在輸入文本框中寫入。當我刪除綁定代碼時,我至少可以將文本輸入到任一文本框中。

這個問題涉及到我的其他query,這就是爲什麼我現在試圖吸收GUI編碼。

的代碼,它的骨架來自嘖嘖」的信息在Thinking in Tkinter

編輯:行代碼改變重:老虎的意見

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

回答

1

我做了一些更改代碼,標籤關於知名度的評論T3。這允許您在ButtonsText字段中使用Return,並且實際上使用「連接」按鈕「救援」焦點。

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

感謝您的意見。我試過了,現在我至少可以在InputText框中輸入文本,但是現在事件正在被觸發,我可以看到.. – Dee

+0

對不起,** no **事件,我可以看到。我在名爲(returnkey())的函數中放置了myParentdestroy(),但是bupkis,=/ – Dee

+0

您仍然有一些問題將按鍵綁定到'Button'小部件,並且您具有綁定到多個函數到「開關連接按鈕顏色」功能和「退出」功能)。將它綁定到一個命令,如'button1Click',到一個控件,比如'self.myParent'。然後,每當用戶按下回車鍵,「連接」按鈕將切換顏色。如果要將按鍵綁定到退出函數,請嘗試將''綁定到'self.myParent'。 – TigerhawkT3