2 Replies - 355 Views - Last Post: 20 April 2013 - 12:36 AM Rate Topic: -----

#1 mzu  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 08-March 12

Python 3 tkinter question

Posted 19 April 2013 - 01:08 PM

Hi,

this is probably a stupid question, but I've been using Python for a little while now and wanted to try some GUI programming with tkinter. I'm just trying to achieve a simple thing, get a guy with an entry field in which I type whatever, press a button which will then show the text in a label. Here is my try at doing this:

from tkinter import *

class App:
    def __init__(self, master):
        frame = Frame(master)
        frame.pack()

        entry1 = Entry(frame)
        entry1.grid(row=0)
    
        self.button = Button(frame, text="Get text", command=get_text).grid(row=2)
    
def get_text():
    text = entry1.get()
    text = text.set(text)
    lbl1 = Label(frame, textvariable=text).grid(row=1)

root = Tk()
app = App(root)
root.mainloop()



this, however, gives me the error "NameError: global name 'entry1' is not defined" when I hit the button. I have tried to change the indentation of def get_text(): one step to the right, but then I get "NameError: global name 'get_text' is not defined".
I thought this was a good example to start understanding some tkinter but it turns out to be a nightmare. I have gone through a few examples on the net but I don't seem to find one that shows what I'm trying to do.

Any help in the right direction or some good tkinter tutorial would be awesome.

Thanks
Michael

Is This A Good Question/Topic? 0
  • +

Replies To: Python 3 tkinter question

#2 woooee  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 78
  • Joined: 21-November 12

Re: Python 3 tkinter question

Posted 19 April 2013 - 05:09 PM

get_text() is outside the class so does not know about entry1. A link to info on Tkinter variables
class App:
    def __init__(self):
        master = Tk()
        frame = Frame(master)
        frame.pack()

        self.entry1 = Entry(frame)
        self.entry1.grid(row=0)
    
        self.text = StringVar()
        self.text.set("")

        ## grid() returns None
        Label(frame, textvariable=self.text).grid(row=1)
        Button(frame, text="Get text", command=self.get_text).grid(row=2)
    
        self.entry1.focus_set()
        master.mainloop()

    def get_text(self):
        ent_text = self.entry1.get()
        self.text.set(ent_text)

app = App() 

This post has been edited by woooee: 19 April 2013 - 05:18 PM

Was This Post Helpful? 1
  • +
  • -

#3 mzu  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 08-March 12

Re: Python 3 tkinter question

Posted 20 April 2013 - 12:36 AM

Hi,

Thanks for the reply. Appreciated. I can see why you do certain things different to what I did like defining self.text as a StringVar() and so on. So great example. Thanks for that. However, I have now adapted my example so it is the same as yours but it still says "global name get_text is not defined" and I don't see why.

from tkinter import *

class App:
    def __init__(self):
        master = Tk()
        frame = Frame(master)
        frame.pack()

        self.entry1 = Entry(frame)
        self.entry1.grid(row=0)

        self.text = StringVar()
        self.text.set("")
        
        self.lbl1 = Label(frame, textvariable=self.text).grid(row=1)
        self.button = Button(frame, text="Get text", fg="red", command=get_text).grid(row=2)

        self.entry1.focus_set()
        master.mainloop()
    
    def get_text(self):
        ent_text = self.entry1.get()
        self.text.set(ent_text)
        

app = App()



Any help is appreciated.

Thanks
Michael

I found it :-)

I forgot to set the button command to self.get_text. I had command=get_text instead of self.get_text

Thanks again, now I understand some of the basics.

Best,
Michael

This post has been edited by mzu: 20 April 2013 - 12:36 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1