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

时间:2018-04-06 08:44:06

标签: python python-3.x tkinter

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

我已经按照已经发布的帖子(How to access variables from different classes in tkinter?)提供了建议,这有帮助,但我仍有问题。

从我需要的地方到以下代码对这些变量进行了评论:

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'。

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

关于。

1 个答案:

答案 0 :(得分:0)

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

self.controller=controller

newUser__init__职能。