0

I'm new im Python, just started to learn about class and tkinter, so forgive me "messy" code. I'm trying to enter some string to field nr1, and after click a button, print this string in console and store this value for later:

from tkinter import Tk, BOTH, RIGHT, RAISED, BOTTOM, TOP, X, StringVar
from tkinter.ttk import Frame, Button, Entry


class AD(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent, v=None, raw_input=None)
        self.parent = parent
        self.parent.geometry("250x150+300+300")
        self.parent.title("Trolollo")
        self.parent.resizable(False, False)
        self.inp = None
        self.v = StringVar()
        self.raw_input = None

        self.initUI()

    def user_input(self):
        global inp
        a = self.raw_input(self.v.get())
        inp = a
        return inp


    def initUI(self):
        self.pack(fill=BOTH, expand=True)

        frame = Frame(self, relief=RAISED, borderwidth=0)
        frame.pack(fill=BOTH, expand=True)

        self.entry1 = Entry(frame, textvariable=self.v)
        self.entry1.pack(side=TOP, fill=X, expand=False, padx=2, pady=2)
        self.entry1.focus_set()

        rename_button = Button(frame, text="Dispaly text", command =         self.user_input())
        rename_button.pack(side=TOP, expand=False, padx=2, pady=2)

        entry2 = Entry(frame)
        entry2.pack(side=TOP, fill=X, expand=False, padx=2, pady=2)


        quit_button = Button(self, text="Quit", command=self.quit)
        quit_button.pack(side=RIGHT, padx=5, pady=5)

        ok_button = Button(self, text="OK")
        ok_button.pack(side=RIGHT, padx=5, pady=5)


def main():
    root = Tk()


    app = AD(root)
    root.mainloop()


if __name__ == '__main__':
    main()

After executing code, i get: TypeError: 'NoneType' object is not callable

Any help would me appreciated

5
  • 2
    in short, command = self.user_input() remove parantheses. Commented Jan 11, 2017 at 11:32
  • 1
    raw_input is None and you are trying to call it by using parantheses in a=self. line. No idea what you are trying to achieve there. Commented Jan 11, 2017 at 11:33
  • Are you using python 2 or 3? You call raw_input() which is python 2, but you import tkinter, not Tkinter, which implies python 3. Then you tagged the question python-3.5 Commented Jan 11, 2017 at 12:48
  • Python 3.5 - is there any substitution fot raw_input()?. EDIT: ok, simply input(). Stll can't figure out how this shoul look a like Commented Jan 11, 2017 at 13:07
  • Using the variable from entry/button in another function in Tkinter This is how you take input from user using Entry&Button and use it as you like. Commented Jan 11, 2017 at 13:39

1 Answer 1

1

ISSUES:

  1. First issue laid in your rename_button's option "command=self.user_input()". You were suppose to name the function and not execute the function. Putting the () symbol meant you executed the function when your code loaded, i.e. it executed once w/o pressing the rename button.
  2. Second issue was the erroneous code in your function user_input. This caused your error msg.

ANSWER: Code with the suggested corrections.

from tkinter import *
from tkinter.ttk import *


class AD(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent, v=None, raw_input=None)
        self.parent = parent
        self.parent.geometry("250x150+300+300")
        self.parent.title("Trolollo")
        self.parent.resizable(False, False)
        self.inp = None
        self.v = StringVar()
        self.raw_input = None

        self.initUI()

    def user_input(self):
        # Get entry1 value, store it as an attribute and print to console
        self.raw_input = self.v.get()
        print(self.raw_input)


    def initUI(self):
        self.frame = Frame(self, relief=RAISED, borderwidth=0)
        self.frame.pack(fill=BOTH, expand=True)

        self.entry1 = Entry(self.frame, textvariable=self.v)
        self.entry1.pack(side=TOP, fill=X, expand=False, padx=2, pady=2)
        self.entry1.focus_set()


        #self.rename_button = Button(self.frame, text="Dispaly text",
        #                            command = self.user_input())
        self.rename_button = Button(self.frame, text="Display text",
                                    command = self.user_input)
        self.rename_button.pack(side=TOP, expand=False, padx=2, pady=2)


        # You can remove the triple quotes to display these widgets 
        """
        self.entry2 = Entry(self.frame)
        self.entry2.pack(side=TOP, fill=X, expand=False, padx=2, pady=2)


        self.quit_button = Button(self.frame, text="Quit", command=self.quit)
        self.quit_button.pack(side=RIGHT, padx=5, pady=5)

        self.ok_button = Button(self.frame, text="OK")
        self.ok_button.pack(side=RIGHT, padx=5, pady=5)

        """

        self.pack(fill=BOTH, expand=True)


def main():
    root = Tk()


    app = AD(root)
    root.mainloop()

Your GUI : enter image description here

SUGGESTIONS:

  • Do remember to put self. in front of your widgets.
  • Do test one widget at a time to help you debug your code.
Sign up to request clarification or add additional context in comments.

2 Comments

That's exactly what i wanted to do - thanks a lot. I'm not sure i understand everythin correctly (this () on the function name).
@Fangir Welcome. When you type the name of a function on IDLE command line, python creates an function object. However, when you type the function name followed by () with no whitespace between the name and (), you will see that python run/execute your function (it's like getting the function object to do it's job). My 2 cents worth...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.