0

I am writing a bullying-abuse hotline program for a class I am taking. I am trying to use a Tkinter stringvar inside a class.

I have used code by martineau (Tkinter: How to show pages with buttons on page?) as the base of my program, and that part of the code works well, but when I am making an Entry widget and tie it to the stringvar t, it gives me the error:

Traceback (most recent call last): File "main.py", line 190, in main = MainView(root) File "main.py", line 14, in init page = Page(container, controller=self) File "main.py", line 148, in init bulliedentry = Entry(self, textvariable = t) NameError: name 't' is not defined

This is my code:


    from tkinter import *


    class MainView(Frame):
        def __init__(self, parent, *args, **kwargs):
            super().__init__(parent, *args, **kwargs)

            container = Frame(self)
            container.pack(side='top', fill='both', expand=True)

            # Create dictionary of page class instances.
            self.pages = {}
            for Page in (Home, Bullying, Abuse, Other):
                page = Page(container, controller=self)
                self.pages[Page.__name__] = page
                page.place(in_=container, x=0, y=0, relwidth=1, relheight=1)

            menubar = Menu(parent)
            filemenu = Menu(menubar, tearoff=0)

            filemenu.add_command(label='Home', command=self.pages['Home'].lift)
            filemenu.add_command(label='Bullying', command=self.pages['Bullying'].lift)
            filemenu.add_command(label='Abuse', command=self.pages['Abuse'].lift)
            filemenu.add_command(label='Other', command=self.pages['Other'].lift)

            filemenu.add_separator()
            filemenu.add_command(label='Quit', command=exit)
            menubar.add_cascade(label='Navigate', menu=filemenu)

            parent.config(menu=menubar)
            self.show_page('Home')

        def show_page(self, page_name):
            page = self.pages[page_name]
            page.tkraise()


    class Home(Frame):
       def __init__(self, parent, controller):
           super().__init__(parent)

           label = Label(self, text='Welcome to the anonymous Help Hotline! '
                                       'Please select your concern.', relief='groove')
           label.grid(row=0, column=1, columnspan=3, sticky='nesw', padx=5, pady=5)

           namelabel = Label(self, text = 'First Name', relief = 'groove')

           bully_button = Button(self, text='Bullying',
                                    command=lambda: controller.pages['Bullying'].lift())
           bully_button.grid(row = 2, column=1, padx=5, pady=5, sticky='nsew')

           abuse_button = Button(self, text='Abuse',
                                    command=lambda: controller.pages['Abuse'].lift())
           abuse_button.grid(row=2, column=2, padx=5, pady=5, sticky='nsew')

           other_button = Button(self, text='Other',
                                    command=lambda: controller.pages['Other'].lift())
           other_button.grid(row=2, column=3, padx=5, pady=5, sticky='nsew')


    class Bullying(Frame):
       def __init__(self, parent, controller):
            super().__init__(parent)

            global t

            def bullied():
               bulliedlabel.grid(row = 3, column = 1, sticky='nsew', padx=5, pady=5)
               bulliedbuttons1.grid(row = 4, column = 1, padx = 5, pady = 5)
               bulliedbuttons2.grid(row = 7, column = 1, padx = 5, pady = 5)
               bulliedbuttons3.grid(row = 10, column = 1, padx = 5, pady = 5)
               bulliedentry.grid(row = 8, column =1, padx = 5, pady = 5, sticky = 'nsew')
               bullylabel.grid_remove()
               bullybuttons1.grid_remove()
               bullybuttons2.grid_remove()
               bullybuttons3.grid_remove()
               bullyentry.grid_remove()

            def bully():
               bullylabel.grid(row = 3, column = 2, sticky='nsew', padx=5, pady=5)
               bullybuttons1.grid(row = 4, column = 2, padx = 5, pady = 5)
               bullybuttons2.grid(row = 5, column = 2, padx = 5, pady = 5)
               bullybuttons3.grid(row = 6, column = 2, padx = 5, pady = 5)
               bullyentry.grid(row = 7, column = 2, padx = 5,  pady = 5, sticky = 'nsew')
               bulliedlabel.grid_remove()
               bulliedbuttons1.grid_remove()
               bulliedbuttons2.grid_remove()
               bulliedbuttons3.grid_remove()
               bulliedentry.grid_remove()

            def bulliedhome():
                print(t.get())

            def bulliedschool():
                print(t.get())

            def bulliedother():
                print(t.get())

            def bullyhome():
                print(t.get())

            def bullyschool():
                print(t.get())

            def bullyother():
                print(t.get())

            def setup():
               bulliedlabel.grid(row = 3, column = 1, sticky='nsew', padx=5, pady=5)
               bulliedbuttons1.grid(row = 4, column = 1, padx = 5, pady = 5)
               bulliedbuttons2.grid(row = 5, column = 1, padx = 5, pady = 5)
               bulliedbuttons3.grid(row = 6, column = 1, padx = 5, pady = 5)
               bulliedentry.grid(row = 8, column =1, padx = 5, pady = 5, sticky = 'nsew')
               bullylabel.grid(row = 3, column = 2, sticky='nsew', padx=5, pady=5)
               bullybuttons1.grid(row = 4, column = 2, padx = 5, pady = 5)
               bullybuttons2.grid(row = 5, column = 2, padx = 5, pady = 5)
               bullybuttons3.grid(row = 6, column = 2, padx = 5, pady = 5)
               bullyentry.grid(row = 7, column = 2, padx = 5,  pady = 5, sticky = 'nsew')
               bulliedlabel.grid_remove()
               bulliedbuttons1.grid_remove()
               bulliedbuttons2.grid_remove()
               bulliedbuttons3.grid_remove()
               bulliedentry.grid_remove()
               bullylabel.grid_remove()
               bullybuttons1.grid_remove()
               bullybuttons2.grid_remove()
               bullybuttons3.grid_remove()
               bullyentry.grid_remove()

            bullylab1 = Label(self, text='This is the Help Hotline page on bullying. If you think someone is a bully or being bullied, use this page.', width=85, relief='groove')
            bullylab1.grid(row=0, column=1, columnspan = 2, padx=5, pady=5)

            bullylab2 = Label(self, text='Press the button that is most accurate to your case. If none are accurate, type text in the text box.', width=85, relief='groove')
            bullylab2.grid(row=1, column=1, columnspan = 2, padx=5, pady=5)

            bullied_button = Button(self, text='Bullied', command=bullied)
            bullied_button.grid(row = 2, column = 1, sticky='nsew', padx = 5, pady = 5)

            bulliedlabel = Label(self, text='Location', relief='groove')

            bulliedbuttons1 = Button(self, text='Home', width = 30, command=bulliedhome)

            bulliedbuttons2 = Button(self, text='School', width = 30, command=bulliedschool)

            bulliedbuttons3 = Button(self, text='Other', width = 30, command=bulliedother)

            bulliedentry = Entry(self, textvariable = t)

            bully_button = Button(self, text='Bully', command=bully)
            bully_button.grid(row = 2, column = 2, sticky='nsew', padx = 5, pady = 5)

            bullylabel = Label(self, text='Location', relief='groove')

            bullybuttons1 = Button(self, text='Home', width = 30, command=bullyhome)

            bullybuttons2 = Button(self, text='School', width = 30, command=bullyschool)

            bullybuttons3 = Button(self, text='Other', width = 30, command=bullyother)

            bullyentry = Entry(self, textvariable = t)

            setup()

    class Abuse(Frame):
       def __init__(self, parent, controller):
           super().__init__(parent)

           abuselab1 = Label(self, text='This is the Help Hotline page on abuse. If you think someone is abusive or being abused, use this page.', width=85, relief='groove')
           abuselab1.grid(row=0, column=1, columnspan = 2, padx=5, pady=5)

           abuselab2 = Label(self, text='Press the button that is most accurate to your case. If none are accurate, type text in the text box.', width=85, relief='groove')
           abuselab2.grid(row=1, column=1, columnspan = 2, padx=5, pady=5)


    class Other(Frame):
       def __init__(self, parent, controller):
           super().__init__(parent)

           otherlab1 = Label(self, text='This is the Help Hotline page on special cases. If you think someone is in danger or dangerous, use this page.', width=85, relief='groove')
           otherlab1.grid(row=0, column=1, columnspan = 2, padx=5, pady=5)

           otherlab2 = Label(self, text='Press the button that is most accurate to your case. If none are accurate, type text in the text box.', width=85, relief='groove')
           otherlab2.grid(row=1, column=1, columnspan = 2, padx=5, pady=5)


    root = Tk()
    root.geometry('800x500')
    main = MainView(root)
    main.pack(side='top', fill='both', expand=True)
    t = Stringvar()
    root.title('Bullying and Abuse Hotline')
    root.mainloop()

I want to have an entry tied the textvariable t that prints when I press one of the small buttons. Currently, I get an error at the very start, shown at the top.

  • The error is telling you exactly what's wrong. You're trying to use something named `t` without creating something named `t` first. – Bryan Oakley Jul 17 '19 at 15:31

1 Answers1

0

This is fixable by making sure that t is defined before class definition.

Adding t = None to the top of the file fixed this issue for me. You can also just instantiate the variable t above everything.

The next issue I found was you had the wrong name for StringVar(), you need the V to be uppercase as well

Jmonsky
  • 1,519
  • 1
  • 9
  • 16