user185578
user185578

Reputation: 71

Tkinter button add line to message

I'm trying to make a simple gui application in python using tkinter. There is a message field, an entry, and a button. I am trying to write a command for the button which sends the text in the entry to the message field then clears the entry. Here is my code:

from tkinter import *
from tkinter.ttk import *

class window(Frame):

    def __init__(self, parent):
        messageStr="Hello and welcome to my incredible chat program its so awesome"

        Frame.__init__(self, parent)

        self.parent = parent

        self.parent.title("Hello World")
        self.style = Style()
        self.style.theme_use("default")

        self.pack(fill=BOTH, expand="yes")

        lfone = Frame(self)
        lfone.pack(fill=BOTH, expand="yes")


        myentry = Entry(self).pack(side=LEFT, fill=BOTH, expand="yes", padx=5)

        messager = Message(lfone, text=messageStr, anchor=S+W, justify=LEFT).pack(fill=BOTH, expand="yes", padx=5, pady=5)

        sendbutton = Button(self, text="Send", command=self.sendbuttoncommand).pack(side=RIGHT)

    def sendbuttoncommand(*args):
        messager.text = messager.text + myentry.get()
        myentry.delete(first, last=None)


def main():

    root = Tk()
    root.geometry("300x200+300+300")
    app=window(root)
    root.mainloop()

if __name__ == '__main__':
    main()

I've tried a few variations of sendbuttoncommand, including nesting it within def init, and changing messager and my entry to self.messager/myentry but have had no success. As it stands, when I try to run it I get a nameerror on messager. How can I affect variables outside of the scope of the method? I was hoping to avoid using global variables in this instance.

Upvotes: 1

Views: 478

Answers (2)

Marcin
Marcin

Reputation: 238249

In addition to @JulienSpronock anwser, you need to know that when you write this:

myentry = Entry(self).pack(side=LEFT, fill=BOTH, expand="yes", padx=5)
messager = Message(lfone, text=messageStr, anchor=S+W, justify=LEFT).pack(fill=BOTH, expand="yes", padx=5, pady=5)
sendbutton = Button(self, text="Send", command=self.sendbuttoncommand).pack(side=RIGHT)

myentry, messager and sendbutton are all None. Dont do this. It should be:

myentry = Entry(self)
myentry.pack(side=LEFT, fill=BOTH, expand="yes", padx=5)
messager = Message(lfone, text=messageStr, anchor=S+W, justify=LEFT)
messager.pack(fill=BOTH, expand="yes", padx=5, pady=5)
sendbutton = Button(self, text="Send", command=self.sendbuttoncommand)
sendbutton.pack(side=RIGHT)

The reason is that pack() (or grid()) returns None.

Upvotes: 3

Julien Spronck
Julien Spronck

Reputation: 15433

In your sendbuttoncommand method, messager is not defined since it is only locally defined in __init__. That's what is causing the NameError.

If you want to re-use messager in the sendbuttoncommand method, just make it an argument of the instance by defining it as self.messager in your __init__ method by calling self.messager in sendbuttoncommand.

However, I suspect you will get other errors afterwards.

Upvotes: 3

Related Questions