简体   繁体   中英

Get value from Entry of Tkinter

Similar questions have been asked, but none of them address the particular way my script is constructed:

from Tkinter import *
from ttk import *
class Gui(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)       #Gui inherits from built in Frame Class
        self.parent = parent
        self.initUI()       

    def initUI(self):
        self.parent.title("Shoes Ware")
        self.pack(fill=BOTH, expand=1)

        run_val = Entry(self)          
        run_val["width"] = 5
        run_val.place(x=80, y=40)

        quit_B = Button(self, text="Submit", command=self.submit)   
        quit_B.place(x=130, y=170)

        def submit(self):
            value = run_val.get()
            print value
            self.quit()

def main():
    root = Tk()
    root.geometry("300x200+50+50")
    app = Gui(root)
    root.mainloop()

if __name__ == '__main__':
    main()

I get "NameError: global name 'run_val' is not defined" when I hit the submit button. What am I doing wrong here. Right now the print statement is just to check my work. Later on, I'll be using that value in a program.

You are not storing the reference to the Entry widget in initUI .

def initUI(self):
    # ...
    self.run_val = Entry(self)          
    self.run_val["width"] = 5
    self.run_val.place(x=80, y=40)

Then you can retrieve the value of self.run_val.get() without any problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM