【问题标题】:How do i pass a class variable to another class?如何将类变量传递给另一个类?
【发布时间】:2019-05-02 10:03:24
【问题描述】:

我正在制作条形码生成器。我需要类 GUI 中的输入由 Barcode 类读取,以便它可以在画布上打印线条。

from tkinter import *
class GUI(Frame):
    def __init__(self, master=None):
    ...
        self.code_input = Entry(master)
        self.code_input.pack()
        self.code = self.code_input.get()
    ...
        self.barcode = Barcode(master, height=250, width=200)
        self.barcode.pack()
        self.barcode.draw()
    ....

class Barcode(Canvas):
    def draw(self):
        self.ean = GUI.code

如果我像上面那样直接引用,它会说AttributeError: type object 'GUI' has no attribute 'code'

如果我做继承方法(基于https://stackoverflow.com/a/19993844/10618936),class Barcode(Canvas, GUI)它说和以前一样

如果我使用 setter 和 getter 方法:

class GUI(Frame)
    def __init__(self, master=None):
    ...
        @property
        def code(self):
            return self.__code

        @code.setter
        def code(self, code):
            self.__code = code

然后self.ean = GUI.code,它不会运行程序并说 改为TypeError: 'property' object is not subscriptable

我该如何解决这个问题?我的程序结构真的很糟糕吗?我真的一点都不擅长编程...我只是希望将 GUI 中的变量转移到 Barcode 类中,这样它就可以计算并将结果返回给 GUI 以显示条形码

【问题讨论】:

  • 更新:现在我正在尝试另一种结构,Barcode 类将消失,其功能移至 GUI 类。我对 OOP 标准的分数可能会降低,但没关系。我只需要让程序运行。但是我还是想知道在另一个类中使用一个类的变量的各种方法,比我想象的要难
  • .code 是一个成员变量,如果不先创建一个GUI 对象,您将无法访问它。 (旁注:我不使用 tkinter)
  • 看看我的回答here 用几个简单的例子来解释各种相关的概念。

标签: python oop tkinter


【解决方案1】:

您需要创建GUI 的实例,否则您只是在引用未定义code 的静态类。你可以像这个例子一样访问它

class A():
    def __init__(self):
        self.code = "A"

class B():
    def __init__(self):
        self.code = "B"
    def foo(self):
        print(self.code + A().code)

b = B()
b.foo() # Outputs "BA"

否则,要像类中的静态变量一样访问它,您需要在类根级别中定义它

class A():
    code = "C"
    def __init__(self):
        self.code = "A"

class B():
    def __init__(self):
        self.code = "B"
    def foo(self):
        print(self.code + A.code)

b = B()
b.foo() # Outputs "BC"

【讨论】:

  • 虽然这些代码 sn-ps 是正确的,但认为每次都像第一个那样创建一个新对象并像第二个那样拥有一个静态代码都不是预期的行为。
  • 无论这里的测试用例如何,OP 似乎首先对如何执行此操作感到困惑。我关心 OP 首先了解基础知识,因此她/他可以将它们推断为要在此处解决的特定案例。在我看来,这是 OP 的双赢。
  • 我明白了,但是在这种情况下,您仍然没有包括对我来说最合乎逻辑的选项,即创建类 A 的实例并将对该类的引用作为参数传递给类 B。
  • 我不反对。有很多方法可以通过它,你的方法是另一种。我的示例仍然解决了 OP 在原始代码中的确切错误。就是这样。
【解决方案2】:

您应该将GUI 对象传递给Barcode 类,在您创建Barcode 实例时,该类是self。如果您希望Barcode 位于GUI 框架内,也可以直接将其用作Canvas master。
需要注意的另一件事是,按照您现在的方式,self.code 将保持为空字符串,因为您仅在创建 Entry 小部件后立即定义它,此时它是空的。当您想对此时的内容执行某些操作时,您应该在条目上使用get

from tkinter import *

class GUI(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.code_input = Entry(self)
        self.code_input.pack()

        self.barcode = Barcode(self, height=250, width=200)
        self.barcode.pack()

        Button(self, text="Update Barcode", command=self.barcode.draw).pack()


class Barcode(Canvas):
    def __init__(self, master, height, width):
        Canvas.__init__(self, master, height=height, width=width)
        self.master = master
        self.text = self.create_text((100,100))

    def draw(self):
        self.itemconfig(self.text, text=self.master.code_input.get())        

root = Tk()
gui = GUI(root)
gui.pack()
root.mainloop()

为了便于说明,我在画布上创建了一个文本对象,并在单击按钮时使用条目的当前值对其进行更新。

【讨论】:

    猜你喜欢
    • 2011-03-15
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    • 2020-11-01
    • 2014-01-03
    • 2023-03-31
    • 1970-01-01
    相关资源
    最近更新 更多