I've tried applying the various examples given in several other SO Question Answers but it's eluding me.
My goal is to be able to send a line of text to a telnet server when "enter" is pressed. My steps toward that are to simply send the last line (since the previous "enter" was pressed) to the output panel.
I've gotten a clumsy set of windows and seem to have attached Text boxes to them, but when I apply any binding type code I end up being unable to write in the input text box. When I remove the binding code I can at least type text into either text box.
This issue relates back to my other query, which is why I am now trying to absorb GUI coding.
The code, of which the skeleton came from the tut' info at Thinking in Tkinter
EDIT: Code lines changed re: Tiger's comments
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()