在类__init__中使用Tkinter Stringvar

时间:2019-07-17 15:26:12

标签: python tkinter

我正在为正在参加的课程编写一个欺凌滥用热线程序。我正在尝试在一个类中使用Tkinter stringvar。

我已经使用martineau(Tkinter: How to show pages with buttons on page?)的代码作为程序的基础,并且该部分代码运行良好,但是当我制作一个Entry小部件并将其绑定到stringvar t时,它给出了我的错误:

  

回溯(最近通话最近):     文件“ main.py”,行190,在       main = MainView(根目录)      init 中的文件“ main.py”,第14行       页面=页面(容器,控制器=自身)      init 中的文件“ main.py”,第148行       Bulliedentry = Entry(自我,文本变量= t)   NameError:名称“ t”未定义

这是我的代码:


    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()

我想让一个条目绑定到当我按下一个小按钮时打印的文本变量t。目前,我从一开始就收到一个错误,显示在顶部。

1 个答案:

答案 0 :(得分:0)

这可以通过确保在类定义之前定义t来解决。

t = None添加到文件顶部为我解决了此问题。 您也可以在所有内容上方实例化变量t

我发现的下一个问题是您为StringVar()输入了错误的名称,您还需要V也要大写