4

I am making a chat program and decided to use Tkinter for the interface.

What I wanna do is a breeze in C# but Tkinter is new to me.

Basically I have a form with a Entry control and a Text control.

I want to know how to append text from the Entry control to the Text control after the user presses Enter.

Here's my code so far:

from tkinter import *

class Application:

    def hello(self):
        msg = tkinter.messagebox.askquestion('title','question')

    def __init__(self, form):
        form.resizable(0,0)
        form.minsize(200, 200)
        form.title('Top Level')

        # Global Padding pady and padx
        pad_x = 5
        pad_y = 5

        # create a toplevel menu
        menubar = Menu(form)
        #command= parameter missing.
        menubar.add_command(label="Menu1")
        #command= parameter missing.
        menubar.add_command(label="Menu2")
        #command= parameter missing.
        menubar.add_command(label="Menu3")

        # display the menu
        form.config(menu=menubar)

        # Create controls

        label1 = Label(form, text="Label1")
        textbox1 = Entry(form)
        #command= parameter missing.
        button1 = Button(form, text='Button1')

        scrollbar1 = Scrollbar(form)
        textarea1 = Text(form, width=20, height=10)

        textarea1.config(yscrollcommand=scrollbar1.set)
        scrollbar1.config(command=textarea1.yview)

        textarea1.grid(row=0, column=1, padx=pad_x, pady=pad_y, sticky=W)
        scrollbar1.grid(row=0, column=2, padx=pad_x, pady=pad_y, sticky=W)
        textbox1.grid(row=1, column=1, padx=pad_x, pady=pad_y, sticky=W)
        button1.grid(row=1, column=2, padx=pad_x, pady=pad_y, sticky=W)

        form.mainloop()

root = Tk()
Application(root)
suchislife
  • 4,251
  • 10
  • 47
  • 78
  • What part are you having a problem with? I don't see any bindings where you are even trying to process data from the entry widget. Are you familiar with the `bind` method? – Bryan Oakley Feb 20 '14 at 00:08
  • Possible duplicate of [How do I bind the enter key to a function in tkinter?](https://stackoverflow.com/questions/16996432/how-do-i-bind-the-enter-key-to-a-function-in-tkinter) – Stevoisiak Feb 22 '18 at 15:07
  • Wow.. you know it’s been 4 years now right? – suchislife Feb 22 '18 at 15:11

1 Answers1

9

So you're using a tkinter.Text box, which supports the .insert method. Let's use it!

def __init__(self,form):

# Lots of your code is duplicated here, so I'm just highlighting the main parts

    button1 = Button(form, text='Button1', command = self.addchat)
    self.textbox = textbox1 # to make it accessible outside your __init__
    self.textarea = textarea1 # see above

    form.bind("<Return>", lambda x: self.addchat())
    # this is the magic that makes your enter key do something

def addchat(self):
    txt = self.textbox.get()
    # gets everything in your textbox
    self.textarea.insert(END,"\n"+txt)
    # tosses txt into textarea on a new line after the end
    self.textbox.delete(0,END) # deletes your textbox text
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
  • Sweet! Just a quick follow up question. How would you keep the user from posting blank entries to the text area? – suchislife Feb 20 '14 at 00:31
  • after `txt = self.textbox.get()`, do `if not txt: return`. Alternatively do `if txt:` and indent the rest of the function. – Adam Smith Feb 20 '14 at 00:32
  • 1
    Why the `lambda x:`? You can also just bind to `self.addchat` and then change `def addchat` to include an optional parameter of `event`, using `def addchat(self, event = None):`. Your `lambda x:` just catches the `event` and throws it away with the unused variable `x`. If you're going to insist on throwing it away, at least use the more Pythonic name for an explicitly ignored variable of `_`. – ArtOfWarfare Sep 28 '14 at 14:32