PDA

View Full Version : [PyQt] QDialog Window Not Popping up when in Function


TDgirl
07-26-2012, 09:20 PM
Hi,

My dialog window is not popping up when being called from inside a function.

I’ve done this before & have been successful, but this particular one is not working.

Could there be a setting I messed up or missed?

This is my code:


from PyQt4 import QtGui (http://qt-project.org/doc/QtGui.html), uic, QtCore (http://qt-project.org/doc/QtCore.html), Qt (http://qt-project.org/doc/Qt.html)

def MAIN():
widget = uic.loadUi("/PATH/TO/file.ui")
widget.show()

MAIN()

(I tried to simplify it as much as possible, but nothing works…)

But it works if I do this:


from PyQt4 import QtGui (http://qt-project.org/doc/QtGui.html), uic, QtCore (http://qt-project.org/doc/QtCore.html), Qt (http://qt-project.org/doc/Qt.html)

widget = uic.loadUi("/PATH/TO/file.ui")
widget.show()

Any ideas? Thanks!!

If the ui code would be useful, please let me know.

fezz
07-31-2012, 05:27 PM
Your widget object will be garbage collected if its instance count is 0, which means it will be destroyed when the function ends. To get around it, your function will have to return the widget instance:

def main():
widget = uic.loadUi("/PATH/TO/file.ui")
widget.show()
return widget

win = main()

TDgirl
07-31-2012, 05:55 PM
Thanks, fezz :) That worked!!

TDgirl
07-31-2012, 10:03 PM
It works, but I noticed that the QDialog window would only show up once if I run this command in Maya:

from scriptFile import *

The QDialog would only pop up once (the first time) & do nothing I run the command again.

fezz
07-31-2012, 10:08 PM
That doesn't tell me anything without seeing the code that's being imported.

zeroeffect
07-31-2012, 10:54 PM
You shouldn't be executing your code through an import statement. When you want the dialog to be displayed you should be calling the function itself. Import the module and then make the function call.

From the Python docs:

"For efficiency reasons, each module is only imported once per interpreter session."

TDgirl
08-01-2012, 02:50 PM
That doesn't tell me anything without seeing the code that's being imported.

Well, I tried the code snippet you provided above:


from PyQt4 import QtGui, uic, QtCore, Qt

def main():
widget = uic.loadUi("/PATH/TO/file.ui")
widget.show()
return widget

win = main()

TDgirl
08-01-2012, 02:51 PM
You shouldn't be executing your code through an import statement. When you want the dialog to be displayed you should be calling the function itself. Import the module and then make the function call.

From the Python docs:

"For efficiency reasons, each module is only imported once per interpreter session."

Hi,

If I try just calling the main() func, then it won't show the window at all:

from PyQt4 import QtGui, uic, QtCore, Qt
def main():
widget = uic.loadUi("/PATH/TO/file.ui")
widget.show()
return widget

win = main()

zeroeffect
08-01-2012, 03:20 PM
The call to:
win = main()
Should not be included in the file with your function (test_file.py):
from PyQt4 import QtGui

def show_dialog():
widget = QtGui.QDialog()
widget.show()
return widget

Main file would then be (main.py):
import test_file

win = show_dialog()

TDgirl
08-01-2012, 03:32 PM
Thanks, Zero :) It makes more sense now & that works every time.

fezz
08-01-2012, 03:44 PM
zeroeffect beat me to it... :)

But to reiterate, if you just import that code you posted, the widget is created in that module's namespace, and as it exists in memory after the first call, re-importing will be ignored. If you're doing this to run in Maya, to assign the widget to a button you'd do what zeroeffect suggested:

import test_file
win = test_file.main()


If you're new to Python, it's a good idea to really get acquainted with importing and namespaces up front....it'll save you a lot of headaches later on. A lot of people coming from mel think that importing in Python is the same as sourcing in mel, which really isn't the case. :)

TDgirl
08-01-2012, 03:46 PM
Thanks for the information & help, fezz :)

CGTalk Moderation
08-01-2012, 03:46 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.