LesM76 February 2016

Getting a entered variable to print - somethings amiss

I'm a bit stuck getting the 'get' function to work correctly. The error occurs when I click the search button, it brings up the 'entry1' is not defined message in the shell. I've tried positioning the variable declaration in different positions, commenting it out and a variety of other tests but have come up stumps about what I'm doing wrong, I'm sure its fairly obvious but for a beginner I need a pointer.

The code should print numbers entered into the Shop Order field into the python shell and is a work in progress, hence does do too much.

from tkinter import *
from tkinter import ttk
#import sys

class GUI:
    def __init__(self, rootWindow):

        #variable
        entry1 = StringVar()        
        var2 = 3
        var3 = 4 

        #item1
        self.label = ttk.Label(rootWindow, text="Shop Order:")
        self.label.grid(row=1, column=1, sticky=(E), padx=3, pady=3)

        self.entry1 = ttk.Entry(rootWindow, width=5)
        self.entry1.grid(row=1, column=2, sticky=(W, E), padx=3, pady=3) 
        #item2
        self.label2 = ttk.Label(rootWindow, text="Quantity:")
        self.label2.grid(row=1, column=3, sticky=(E), padx=3, pady=3)

        self.entry2 = ttk.Entry(rootWindow, width=5, textvariable=var2)
        self.entry2.grid(row=1, column=4, sticky=(W, E), padx=3, pady=3) 

        #item3
        self.label3 = ttk.Label(rootWindow, text="Description:")
        self.label3.grid(row=2, column=1, sticky=(E), padx=3, pady=3)

        self.entry3 = ttk.Entry(rootWindow, width=5, textvariable=var3)
        self.entry3.grid(row=2, column=2, columnspan=3, sticky=(W, E), padx=3, pady=3) 

        #output
        self.label4 = ttk.Label(rootWindow, text="Drawings:")
        self.label4.grid(row=3, column=1, sticky=(N,E), padx=3, pady=3)

        #buttons

        self.button3 = ttk.Button(rootWindow, text="Exit", command=rootWindow.destroy)
        self.button3.grid(row=3, column=6, sticky=(N,W), padx=3, pady=3)

        self.tex        

Answers


Bryan Oakley February 2016

You have created a global variable named entry1, and you have an attribute named self.entry1. That isn't necessarily wrong, but it does make your code hard to understand. I'll explain why later.

You are trying to get the value of the variable like this:

def DoASearch():
    print (entry1.get())

However, you never assigned entry1 to any widget, so the value will always just be whatever the default value is. Oddly, later on you attempt to use this same variable as if it were the entry widget when you do this:

def clear_text():
    entry1.delete(0, 'end')

That is why it is bad to have two different objects with very similar names. It's easy to confuse the two.

My advice is to remove the use of StringVar completely. You don't need it, and as you can see it can lead to confusion.

Instead, just directly get the value from the entry widget when you need it. Either by self.entry1.get() if calling from within the class, or gui.entry1.get() when called externally.

I recommend making DoASearch and clear_text methods of the class, so that you can use self.entry1 everywhere.


R. Kap February 2016

I ran your code and did not get any entry1 not defined error, but I was able to recreate your issue that the DoASearch function was not printing into the shell what was entered in the Entry widget for Shop Order. I have been able to remedy that by first renaming your StringVar() variable to entry10 instead of entry1 since the program was confusing the Entry widget with the StringVar() as they were both assigned the same variable. Also, in your first Entry widget, you did not assign a text variable method to it, so it was not outputting what was entered into the Shop Order: field anyways. Finally, in your clear_text function, the exception name 'entry1' is not defined was thrown since entry1 is an attribute of self, and thus just entry1 cannot return what is entered into its assigned Entry widget. Instead self.entry1 should be used to do that. Therefore, the following changes to your code should make it work as expected:

  • Replace

    entry1 = StringVar()
    

    with

    entry10 = StringVar()
    
  • Add textvariable to the Entry widget, like so:

    self.entry1 = ttk.Entry(rootWindow, width=5, textvariable = entry10)
    
  • In the DoASearch function, replace

    print (entry1.get())
    

    with

    try:
        print(int(entry10.get()))
    except ValueError:
        messagebox.showwarning("Not a number", "That is not a number. Please enter a number")
    

    to make sure that it prints out a number if the user enters one in the Shop Order field, otherwise show a warning dialog if the user inputs anything other than a number.

  • In the clear_text function, replace

    entry1. 

Post Status

Asked in February 2016
Viewed 1,453 times
Voted 8
Answered 2 times

Search




Leave an answer