Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing PyQt4 UI elements from other functions referenced from another module
    primarykey
    data
    text
    <p>I'm using PyQt4 using Qt designer. This is the first time I've used it so I'm just getting my head round a few things but this has me stumped. </p> <p>The problem I'm having is that once the UI window has been defined and created, I try to call a function to update a UI element and it produces a "not defined" error. </p> <p>However, if I place the same code in the block of code where the element is initially defined, it works fine.</p> <p>I have written an example below to show what I mean more clearly.</p> <p>The problem is I need to call to update the lcd number from a different module which contains a function that is called when a button is clicked.</p> <p>To show this, I've made a form with an lcdnumber (lcd1) and a pushbutton (button1) in qtdesigner, compiled the code in the file "testui.py", which creates the class "MainWindow".</p> <p>In a.py, I have put:</p> <pre><code>import testui as interface import sys import b from PyQt4 import QtCore, QtGui ui= interface.Ui_MainWindow() def updateui(): for i in range(0, 100): ui.lcd1.setProperty("value", i) if __name__ == "__main__": app = QtGui.QApplication(sys.argv) test = QtGui.QMainWindow() ui.setupUi(test) ui.lcd1.setProperty("value", 1) ui.button1.clicked.connect(b.updatelcd) test.show() app.exec_() </code></pre> <p>This opens the window, and sets the initial state and connects the button to a function in b.py.</p> <p>In b.py, I have put:</p> <pre><code>from a import updateui def updatelcd(): updateui() </code></pre> <p>When I click the button this calls the function in a.py, which leads to the error:</p> <p>"Module has no attribute lcd1"</p> <p>This has been confusing me for a while because ui.lcd1.setProperty works in the <strong>main</strong> loop! </p> <p>It must be something fundamentally wrong with what I'm doing but I can't work it out. </p> <p>Any ideas appreciated</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload