Fangir
Fangir

Reputation: 151

python pass variable tkinter

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

Upvotes: 0

Views: 1673

Answers (1)

Sun Bear
Sun Bear

Reputation: 8297

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.

Upvotes: 1

Related Questions