Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.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 Can';t访问来自其他类的变量-tkinter_Python_Variables_Python 3.x_Tkinter - Fatal编程技术网

Python Can';t访问来自其他类的变量-tkinter

Python Can';t访问来自其他类的变量-tkinter,python,variables,python-3.x,tkinter,Python,Variables,Python 3.x,Tkinter,我正试图在tkinter中编写一个程序,用户点击一个带有他们名字的按钮,一个验证页面会显示给他们。我遇到的问题是变量正在重置,或者我访问它时出错: import tkinter as tk from tkinter import * from tkinter import ttk LARGE_FONT = ("Times New Roman", 12) NORM_FONT = ("Times New Roman", 10) root = Tk() root.withdraw() class

我正试图在tkinter中编写一个程序,用户点击一个带有他们名字的按钮,一个验证页面会显示给他们。我遇到的问题是变量正在重置,或者我访问它时出错:

import tkinter as tk
from tkinter import *
from tkinter import ttk

LARGE_FONT = ("Times New Roman", 12)
NORM_FONT = ("Times New Roman", 10)
root = Tk()
root.withdraw()


class DIS(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.iconbitmap(self, default="")
        tk.Tk.wm_title(self, "program")
        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.frames = {}

        for F in (StartPage, contactQues, nameVerify):

            frame = F(container, self)

            self.frames[F] = frame

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

            self.show_frame(StartPage)

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


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        button2 = ttk.Button(self, text = "Name Select",
                        command=lambda:           controller.show_frame(contactQues))
        button2.pack()


class contactQues(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)  
        self.controller = controller
        global name
        name = StringVar()

        label1 = tk.Label(self, text = "Select Your Name", font = LARGE_FONT)
        label1.pack(pady=10, padx=10)

        button2 = ttk.Button(self, text = "Bojangles", command = self.bojangles)
        button2.pack(pady=5)

    def bojangles(self):

        name.set("Mr. Bojangles")
        self.controller.show_frame(nameVerify)
    #
    #Many other names to select
    #

class nameVerify(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        namename = name.get()

        label5 = tk.Label(self, text = "Your Name:", font = LARGE_FONT)
        label5.pack(pady=10, padx=10)
        labelcontact = tk.Label(self, text = namename, font = NORM_FONT)
        labelcontact.pack()

app = DIS()
app.mainloop()
因此,本质上,我希望发生的是: -程序运行,用户按“名称选择”,用户选择其名称,最后一页显示其选择

我尝试过处理全局变量、
labelcontact
label的textvariables、StringVar()等,但似乎无法确定这一点

有更好的方法吗?还是我做了一些天生错误的事情


谢谢您的帮助。

我建议将
name
作为
DIS
类的一个属性。然后,您的StartPage和nameVerify实例可以通过其
控制器属性访问它。如果希望
labelcontact
name
时自动更新,请使用
textvariable
属性

此外,您需要删除
root=Tk()
root.draw()
行。我不知道为什么,但只要它们在那里,labelcontact标签就不会正确更新。在任何情况下,它们似乎都不会做任何事情——希望它们对您的实际代码不是至关重要的

import tkinter as tk
from tkinter import *
from tkinter import ttk

LARGE_FONT = ("Times New Roman", 12)
NORM_FONT = ("Times New Roman", 10)

class DIS(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.iconbitmap(self, default="")
        tk.Tk.wm_title(self, "program")
        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.name = StringVar()
        self.frames = {}

        for F in (StartPage, contactQues, nameVerify):

            frame = F(container, self)

            self.frames[F] = frame

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

            self.show_frame(StartPage)

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


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        button2 = ttk.Button(self, text = "Name Select",
                        command=lambda:           controller.show_frame(contactQues))
        button2.pack()


class contactQues(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)  
        self.controller = controller

        label1 = tk.Label(self, text = "Select Your Name", font = LARGE_FONT)
        label1.pack(pady=10, padx=10)

        button2 = ttk.Button(self, text = "Bojangles", command = self.bojangles)
        button2.pack(pady=5)

    def bojangles(self):

        self.controller.name.set("Mr. Bojangles")
        self.controller.show_frame(nameVerify)
    #
    #Many other names to select
    #

class nameVerify(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        label5 = tk.Label(self, text = "Your Name:", font = LARGE_FONT)
        label5.pack(pady=10, padx=10)
        labelcontact = tk.Label(self, textvariable = self.controller.name, font = NORM_FONT)
        labelcontact.pack()

app = DIS()
app.mainloop()

我意识到我本可以把它缩短一点。我的错。是的,请缩短这个。请详细描述未正确设置的内容:“标签”是不够的,因为程序中有几个标签。还放入一些打印语句,以跟踪有问题的值;这可能显示错误的流程。这是一个有趣的问题。。。我最初认为用
textvariable=name
切换
text=namename
是一个非常简单的修复方法,这应该会导致labelcontact在
name
时自动更新。但由于某些原因,它不是gui上的.Trace变量,而是在另一个类函数中。我从来没有想过这一点。我尝试了global
name
和所有东西,但从未接触过
DIS.
它似乎像预期的那样工作--非常感谢+1re:“此外,您需要删除root=Tk()和root.draw()行。我不知道为什么。”-之所以“为什么”是因为
DIS
Tk
的一个子类,而tkinter程序应该只有一个
Tk
@BryanOakley的实例。我确实有一种感觉与此有关。我不得不添加它,以使整个程序的其他部分正常工作,但我稍后将返回到该部分,并查看如何在没有第二个实例的情况下解决该问题。谢谢你的澄清。