-2
class FCMenu:
def __init__(self,master):
    frame=Frame(master)
    frame.pack()
    Label(frame, text="What is the number?").grid(row=0)
    self.num = IntVar()
    self.entry = Entry(frame,textvariable=self.num).grid(row=1)
    button = Button(frame, text="Factorize", command=self.calc).grid(row=2)
    self.resultvar = StringVar()
    Label(frame, textvariable=self.resultvar).grid(row=3)

def calc(self):
    e = int(self.entry.get())
    print(e,self.num.get())
    ...

I am trying to create a Python GUI with tkinter, as shown above. However, every time I call .get() on the entry or the textvariable, it fails. With the entry itself, it explains that there is no .get() function for NoneType. If I remove that and use only self.num.get(), it prints 0 or 0.0, depending on whether or not I turn it to an integer. If I turn self.num to a StringVar, it prints nothing at all. Simply, I cannot find a way to obtain the input that I want to retrieve.

StardustGogeta
  • 3,331
  • 2
  • 18
  • 32

1 Answers1

1

Made a simplified version of your code, and it prints correctly as the Entry widget is changed. Could be error elsewhere in your code? Or wrong indentation? Here is the code I tested with (this is 2.7 code, but worked with a 3.5 version of the code also):

import Tkinter as tk

class FCMenu:
    def __init__(self, master):
        frame = tk.Frame(master)

        self.num = tk.IntVar()
        self.entry = tk.Entry(frame, textvariable=self.num)
        self.button = tk.Button(frame, text='Calc', command=self.calc)

        frame.pack()
        self.entry.pack()
        self.button.pack()

    def calc(self):
        print(self.num.get(), self.entry.get())

root = tk.Tk()
frame = FCMenu(root)
root.mainloop()

It prints from both self.num.get() and self.entry.get(). The first is an Int and the second as String it seems.

oystein-hr
  • 551
  • 4
  • 9
  • Thank you very much! It seems that the grid was the issue, as your .pack() solution works perfectly. – StardustGogeta Dec 31 '15 at 00:33
  • Good that you got it to work. I think mixing pack and grid might cause problems, now that you mention it. Personally I have used only Grid for the GUIs I have made, never tried mixing them. Pack I have only used for quick tests. – oystein-hr Dec 31 '15 at 04:04