在不同的类中使用变量 [英] Use variable in different class

查看:73
本文介绍了在不同的类中使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的初学者。我在不同的类中使用变量时遇到问题。请帮忙。
这是来自使用Tkinter中的按钮导航到应用程序的不同页面?

I am a beginner in python. I have a problem with using variable in different class. Please help. Here is the sample code from Using buttons in Tkinter to navigate to different pages of the application?

import Tkinter as tk

class Page(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
    def show(self):
        self.lift()

class Page1(Page):
   def __init__(self, *args, **kwargs):
       Page.__init__(self, *args, **kwargs)
       label = tk.Label(self, text="This is page 1")
       label.pack(side="top", fill="both", expand=True)

       entry = tk.Entry(self)
       entry.pack()

class Page2(Page):
   def __init__(self, *args, **kwargs):
       Page.__init__(self, *args, **kwargs)
       label = tk.Label(self, text="This is page 2")
       label.pack(side="top", fill="both", expand=True)

       text = tk.Text(self, entry.get())
       root.after(...)

class MainView(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        p1 = Page1(self)
        p2 = Page2(self)


        buttonframe = tk.Frame(self)
        container = tk.Frame(self)
        buttonframe.pack(side="top", fill="x", expand=False)
        container.pack(side="top", fill="both", expand=True)

        p1.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        p2.place(in_=container, x=0, y=0, relwidth=1, relheight=1)


        b1 = tk.Button(buttonframe, text="Page 1", command=p1.lift)
        b2 = tk.Button(buttonframe, text="Page 2", command=p2.lift)


        b1.pack(side="left")
        b2.pack(side="left")


        p1.show()

if __name__ == "__main__":
    root = tk.Tk()
    main = MainView(root)
    main.pack(side="top", fill="both", expand=True)
    root.wm_geometry("400x400")
    root.mainloop()

然后它有两个问题:

 NameError: global name 'entry' is not defined
 NameError: global name 'root' is not defined

如何使用这些变量?请帮忙!

How can i use these variable? Please help!

推荐答案

您正在将变量 entry 定义为方法,这不会使 entry 可以被全局范围访问,这是您应该嵌入对象的方式:

You're defining the variable entry inside the body of a method, this does not make entry accessible to the global scope, this is how you should embed objects:

class Page1(Page):
   def __init__(self, *args, **kwargs):
       Page.__init__(self, *args, **kwargs)
       self.label = tk.Label(self, text="This is page 1")
       self.label.pack(side="top", fill="both", expand=True)

       self.entry = tk.Entry(self)
       self.entry.pack()

如您所见,您将标签 entry 对象嵌入到 self ,当您调用类 Page1()时,类 Page1 的隐含实例。在方法或函数的主体内分配的任何变量都变为对该方法或函数的 local 。这就是您应该使用代码的方式。

As you can see, you embed label and entry objects to self, the implied instance of class Page1 when you call class Page1(). Any variable assigned inside a body of a method or a function becomes local to that method or function. That's how you should go with your code.

熟悉这个概念: Python作用域规则的简短说明

编辑:

在类 Page2 中如果您确实要访问 Page1类的 self.entry.get / code>,那么您需要将类Page1的对象传递给类Page2的构造函数,然后从传递给 Page2的对象中获取条目。__init __ 类似于以下内容:

In class Page2 If you really want to access self.entry.get of class Page1, then you need to pass the object of class Page1 to the constructor of class Page2 and then fetch the entry from the object you passed to Page2.__init__ like the following:

class Page2(Page):
   def __init__(self, page1_obj, *args, **kwargs):
       Page.__init__(self, *args, **kwargs)
       self.label = tk.Label(self, text="This is page 2")
       self.label.pack(side="top", fill="both", expand=True)

       self.text = tk.Text(self, page1_obj.entry.get())  # access attributes of the passed-in object (page1._object)
       root.after(...)

在这里,您必须将对象传递给构造函数,然后才能访问该对象的属性,包括其绑定方法。我之所以这样做,是因为在您的 Page1 类中,您将 self 传递给了 tk .Entry ,所以我认为您可能想使用返回的 self.entry = tk.Entry(self)的返回结果。 > Page1 。如果您真的不打算使用 self.entry = tk.Entry(self) Page1 类,则可以在 Page2 的构造函数中添加相同的赋值: self.entry = tk.Entry(self)

Here you must pass an object to the constructor and then you'll be able to access the object's attributes including its bound methods. The reason why I did this though is because in your Page1 class you passed self to tk.Entry so I thought you probably wanted to use the returned results of self.entry = tk.Entry(self)of Page1. If you don't really intend to use the returned results of self.entry = tk.Entry(self) of Page1 class then you can add the same assignment in the constructor of Page2: self.entry = tk.Entry(self).

这篇关于在不同的类中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆