1

Creating a popup window which will ask for email entry and then print the email when 'OK' is pressed, this is my code:

import tkinter as tk

class PopUp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        tk.Label(self, text="Main Window").pack()
        popup = tk.Toplevel(self)
        popup.wm_title("EMAIL")
        popup.tkraise(self)
        tk.Label(popup, text="Please Enter Email Address").pack(side="left", fill="x", pady=10, padx=10)
        self.entry = tk.Entry(popup, bd=5, width=35).pack(side="left", fill="x")
        self.button = tk.Button(popup, text="OK", command=self.on_button)
        self.button.pack()

    def on_button(self):
        print(self.entry.get())

app = PopUp()
app.mainloop()

Every time I run it I get this error:

AttributeError: 'NoneType' object has no attribute 'get'

The pop up works how it should but its the input entry that doesn't seem to be working. I've seen this example before but it wasn't in the popup (I can get it work perfectly without the popup).

Any help is appreciated.

JackU
  • 1,406
  • 3
  • 15
  • 43
  • 1
    Duplicate of https://stackoverflow.com/questions/1101750/tkinter-attributeerror-nonetype-object-has-no-attribute-get. All you need to do is separate `self.entry = tk.Entry(popup, bd=5, width=35)`, `self.entry.pack(side="left", fill="x")` – Miraj50 Jan 04 '19 at 08:34
  • @Miraj50 Works now, thank you – JackU Jan 04 '19 at 08:37

1 Answers1

1

You can store the value in a StringVar variable and get() its value.

import tkinter as tk
from tkinter import StringVar

class PopUp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)        
        tk.Label(self, text="Main Window").pack()
        popup = tk.Toplevel(self)
        popup.wm_title("EMAIL")
        popup.tkraise(self)        
        tk.Label(popup, text="Please Enter Email Address").pack(side="left", fill="x", pady=10, padx=10)
        self.mystring = tk.StringVar(popup)
        self.entry = tk.Entry(popup,textvariable = self.mystring, bd=5, width=35).pack(side="left", fill="x")
        self.button = tk.Button(popup, text="OK", command=self.on_button)
        self.button.pack()

    def on_button(self):
        print(self.mystring.get())

app = PopUp()
app.mainloop()
amanb
  • 5,276
  • 3
  • 19
  • 38