-1

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?

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
に か
  • 117
  • 1
  • 3
  • 12
  • Please read the PyQt documentation on this subject: [Using Qt Designer](http://pyqt.sourceforge.net/Docs/PyQt4/designer.html). – ekhumoro Feb 11 '16 at 18:35
  • Yes, I read. The actual .ui file conversation and the one in the docs don't match. That's why I'm asking. The converter script doesn't create a py script with __init__ even though, I specify the flags to do so. I'm over that though, onto other solutions. – に か Feb 11 '16 at 22:03
  • You have misunderstood the documentation. The examples shown are scripts that you must create yourself. The module generated by `pyuic` should be imported into the script. – ekhumoro Feb 11 '16 at 23:17
  • ohhhhh, i see.. that makes sense now. :) thx – に か Feb 11 '16 at 23:41

1 Answers1

1

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)
Lafexlos
  • 7,618
  • 5
  • 38
  • 53
  • I'm fairly new to Qt. The script generator didn't add a __init__. I think if I add __init__ it will break the gui. I'll try when I get back to class in the morning. Thanks for your quick response. – に か Feb 11 '16 at 10:43
  • Is there a way to do it without adding __init__? – に か Feb 11 '16 at 10:45
  • Commented two lines at the bottom. You can remove init and uncomment those two lines. – Lafexlos Feb 11 '16 at 11:18