Reputation: 1167
I think my answer is related to this answer but can't quite get my head around it.
I am now realising that my code isn't structured very well, however the current setup is:
main_run.py
app = QtGui.QApplication(sys.argv)
app.processEvents()
ui1 = new_main_ui.Ui_Form_Main()
ui1.show()
sys.exit(app.exec_())
new_main_ui
class Ui_Form_Main(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.setupUi(self)
...etc...
within this class are Qlabel objects which have their text updated as buttons are pressed on the UI.
within new_main_ui there is a call to another module (sqr_pull.py) which does some stuff. Halfway through sqr_pull.py I want to update a Qlabel in my UI, but I can't figure out how to reference the UI instance (ui1) without getting:
NameError: name 'ui1' is not defined
I've tried trying to pass variables through as I go using sys.modules[__name__]
as follows:
in main_run: new_main_ui.parent1 = sys.modules[__name__]
in new_main_ui: sqr_pull.parent2 = sys.modules[__name__]
and then in sqr_pull trying to change using `parent2.QLabel.setText("blahblahblah")
but it again doesn't recognise the instance name. What's the best way to do this?
Upvotes: 0
Views: 83
Reputation: 77912
The clean way to give a function access to an object is to pass this object to the function...
# worker.py
def work(ui):
some_result = do_something()
ui.update(some_result)
other_result = do_something_else()
# ui.py
import worker
class Ui(object):
def some_action(self):
worker.work(self)
def update(self, data):
self.show_data_somewhere(data)
Hacking with sys.modules
or whatever will only result in some unmaintainable mess.
Upvotes: 2