AngryWeeb
AngryWeeb

Reputation: 85

How do i pass a class variable to another class?

I'm making a barcode generator. I need the input in class GUI to be read by class Barcode so it can print the lines on the canvas.

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

If I reference directly like the above it says AttributeError: type object 'GUI' has no attribute 'code'

If I do inheritance method (based on https://stackoverflow.com/a/19993844/10618936),class Barcode(Canvas, GUI) it says the same as the previous

If I use setter and getter methods:

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

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

then self.ean = GUI.code, it won't run the program and say TypeError: 'property' object is not subscriptable instead

how do I fix this problem? Is the structure of my program really bad? I'm really not good at programming at all... I just want the variable in GUI to be transferred to class Barcode so it can compute and return the result back to GUI to display the barcode

Upvotes: 2

Views: 244

Answers (2)

fhdrsdg
fhdrsdg

Reputation: 10532

You should pass the GUI object to the Barcode class, which at the point you create the Barcode instance is self. If you want the Barcode to be inside the GUI frame, you can also directly use it as the Canvas master.
Another thing to notice is that with the way you have it now, self.code will be and remain an empty string, since you only define it right after you've created the Entry widget, at which point it is empty. You should use get on the Entry at the time you want to do something with the contents at that point.

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()

For illustration purposes I create a text object on the canvas and update that with the current value of the Entry on a Button click.

Upvotes: 1

b-fg
b-fg

Reputation: 4137

You need to create an instance of the GUI, otherwise you are just referencing to the static class for which code is not defined. You could access it like in this example

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"

Otherwise, to access it like an static variable within the class you need to define it inside the class root level

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"

Upvotes: 2

Related Questions