how to access members from a class

61 Views Asked by At

im using QtDesigner which drops its code in a Class when converted to .py.

I have to access the buttons and labels and textboxes outside of the class to do operations on them. I cant put my functions above the class because of unresolved error. I dont want to add my functions inside the class.. theres alot of stuff and its messy. so i have to put my functions after the class.

here is a over simplified sample python code structure that Qt Designer converts to

class MyWindowUI():
    def setupUI(self):
        self.myButton1 = 'A'
        self.myButton2 = 'B'

    def stuff(self):
        self.myButton3 = 'A'
        self.myButton4 = 'B'
    test = "something"

myBtn = MyWindowUI()
print (myBtn.test)
print (myBtn.myButton4)

so, myBtn.test works. it prints 'something', but, myBtn.myButton4 doesnt. Says MyWindowUI instance has no attribute 'myButton4.

How do I get information from "stuff" and "setupUI" from outside the class?

1

There are 1 best solutions below

3
On BEST ANSWER

Since you are not calling methods, your code never enters those code blocks to create variables.

One way is calling them in __init__ but you can call them anywhere you like after creating your class instance. Just be sure, your code goes through that line.

class MyWindowUI():
    def __init__(self):
        self.setupUI()
        self.stuff()

    def setupUI(self):
        self.myButton1 = 'A'
        self.myButton2 = 'B'

    def stuff(self):
        self.myButton3 = 'A'
        self.myButton4 = 'B'
    test = "something"

myBtn = MyWindowUI()
#myBtn.setupUI()
#myBtn.stuff()
print (myBtn.test)
print (myBtn.myButton4)