Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在tkinter中的不同类和定义之间传递变量_Python_Python 3.x_Tkinter - Fatal编程技术网

Python 在tkinter中的不同类和定义之间传递变量

Python 在tkinter中的不同类和定义之间传递变量,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我的代码有问题。在tkinter框架中按下submit按钮后,我无法将变量传递给另一个类 我已经听从了一个帖子()的建议,这对我有所帮助,但我仍然有一些问题 从何处到何处我需要这些变量在下面的代码中进行注释: import tkinter as tk from tkinter import StringVar LARGE_FONT = ("Verdana", 12) class Club(tk.Tk): def get_page(self, page_class):

我的代码有问题。在tkinter框架中按下submit按钮后,我无法将变量传递给另一个类

我已经听从了一个帖子()的建议,这对我有所帮助,但我仍然有一些问题

从何处到何处我需要这些变量在下面的代码中进行注释:

import tkinter as tk
from tkinter import StringVar
LARGE_FONT = ("Verdana", 12)

class Club(tk.Tk):

    def get_page(self, page_class):
        return self.frames[page_class]

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)

        container.pack(side="top", fill="both", expand=True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.shared_data = {
            "username": tk.StringVar(),
            "password": tk.StringVar(),
        }

        self.frames = {}

        for F in (Terminal, newUser, newUserSubmitButton, delUser):
            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(Terminal)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()


class Terminal(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Welcome to the club terminal. Click the options below", font=LARGE_FONT)
        label.grid(columnspan=3)

        button = tk.Button(self, text="Visit new User",
                           command=lambda: controller.show_frame(newUser))
        button.grid(row=1, column=0)

        button2 = tk.Button(self, text="Visit del User",
                            command=lambda: controller.show_frame(delUser))
        button2.grid(row=1, column=1)

class newUser(tk.Frame):

    def __init__(self, parent, controller):

        def submitButton():
            username = self.controller.shared_data["username"].get()
            print(username)
            controller.show_frame(newUserSubmitButton)
            ##username variable from here to...

        tk.Frame.__init__(self, parent)
        welcomelabel = tk.Label(self, text="Add New User/s", font=LARGE_FONT)
        welcomelabel.grid(columnspan=2, sticky="ew")

        userNameLabel = tk.Label(self, text="Username")
        userNameLabel.grid(row=1, column=0, sticky="e")
        userNameEntry = tk.Entry(self, textvariable=self.controller.shared_data["username"])
        userNameEntry.grid(row=1, column=1)

        userMemTypeLabel = tk.Label(self, text="Membership Type")
        userMemTypeLabel.grid(row=2, column=0, sticky="e")
        variable = StringVar(self)
        variable.set("Full")
        userMemTypeMenu = tk.OptionMenu(self, variable, "Full", "Half")
        userMemTypeMenu.grid(row=2, column=1)

        userMemYearsLabel = tk.Label(self, text="Years that member is in the club")
        userMemYearsLabel.grid(row=3, column=0, sticky="e")
        userMemYearsEntry = tk.Entry(self)
        userMemYearsEntry.grid(row=3, column=1)
        self.controller = controller

        newusersubmitbutton = tk.Button(self, text="submit", command=submitButton)
        newusersubmitbutton.grid(columnspan=2)

class newUserSubmitButton(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        ##username variable goes here
        page1 = self.controller.get_page(newUser.submitButton)
        page1.username.set("Hello, world")
        print(page1)

class delUser(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="del User!!!", font=LARGE_FONT)
        label.pack(pady=10, padx=10)

        button1 = tk.Button(self, text="Back to Home",
                            command=lambda: controller.show_frame(Terminal))
        button1.pack()

        button2 = tk.Button(self, text="new User",
                            command=lambda: controller.show_frame(newUser))
        button2.pack()

app = Club()
app.title("Club Terminal")
app.iconbitmap("table.ico")
app.mainloop()
每当我运行这段代码时,我都会得到一个AttributeError:“newUser”对象没有属性“controller”

非常感谢您的帮助,我非常乐意尝试任何想法


关于。

此代码中有更多问题,但要解决此问题,请添加以下行:

self.controller=controller

newUser
classes
\uuuu init\uuuu
函数。

请用错误消息的全文更新您的问题。您在定义该属性之前正在使用该属性。就这么简单。@quamrana我现在明白了,但是将它添加到
\uuuu init\uuuu
的第一行,在我尝试时就消除了错误。不知道发生了什么事。Python是一种动态语言,向类添加属性的顺序很重要。不,但我在自己的代码中已经对这个错误感到很失望。@quamrana抱歉,以为您是OP-刚刚意识到