If I'm trying to create a window or prompt a file dialog in the IDLE shell, nothing opens and the shell restarts. Is this a bug of some kind? I can't find anything about it. I'm new to PyQt (and Python in general) but had been able to get tutorials to work correctly. The last day or so, if I open IDLE and import PyQt4, QtGui, etc and then run something simple like QFileDialog.getOpenFileName, the shell just restarts. Any ideas?
You need to have a QApplication before you can use anything else from PyQt. Try rereading some of the tutorials you followed, or do a few more. This one for example.
In the first code sample of the above tutorial, pay special attention to these lines (I've included the comments from the tutorial for convenience):
app = QtGui.QApplication(sys.argv)
Every PyQt4 application must create an application object. The
application object is located in the QtGui module. The sys.argv
parameter is a list of arguments from the command line. Python scripts
can be run from the shell. It is a way, how we can control the startup
of our scripts.
and
sys.exit(app.exec_())
Finally, we enter the mainloop of the application. The event handling
starts from this point. The mainloop receives events from the window
system and dispatches them to the application widgets. The mainloop
ends, if we call the exit() method or the main widget is destroyed.
The sys.exit() method ensures a clean exit. The environment will be
informed, how the application ended.
The exec_() method has an underscore. It is because the exec is a
Python keyword. And thus, exec_() was used instead.
It appears you might have forgotten about these. Or maybe you haven't realized that this means that you normally can't use PyQt with a running event loop in the interactive shell. However, there is a trick for that, see here.
Related
I have two PySide programs that use a UI with many of the same elements. I've constructed a UI that imports several shared QGroupBox and one that differs. One program works, the other locks up before the GUI appears, and no longer responds to keyboard interrupts.
But. I want to know why introducing set_trace() fixes things. The following:
ui = UI(initValues)
vcs.show()
hangs. The code successfully makes it through the instantiation, but then the show() never shows anything. However:
ui = UI(initValues)
ipdb.set_trace()
vcs.show()
followed by typing "c" to the ipdb prompt and everything works as I expect it to. The GUI shows up and the program works fine. I've tried replacing the set_trace() with input() and with sleep(), both of which exhibit the original problem.
I'm currently working on a GUI interface that runs a GNU Radio script rendering some Qt windows (plotting real time histogramm, waterfall graph, or other nice gui...). I control its parameters and launch it from my first main windows. I'd like to integrate the QApp created by launching the GNU Radio script inside a widget of my first main windows (which is another QApp).
I already tried to run a second QApp in the same time of my first one, for some reasons, I get errors like QCoreApplication::sendPostedEvents: Cannot send posted events for objects in another thread.
I did not work around this error because a nicer idea would be to have a kind of container whom I would give a PyQt4.QtGui.QApplication object to make him execute and display it without creating a second windows.
Does that kind of container exist ?
Thanks a lot !
i am facing some understanding problems since i am trying do dive into pyqt´s gui programming.
I use
exec(open("./regression.py").read())
inside my pyqt gui program to call a script where my calculations are made after the user has pushed some buttons (these define some variables in regression.py)
so if i run this in an empty script all my definitions are callable (and i see them in the variable explorer).
if i try to run this in my gui program - the script executes but if i wanted to reuse one variable or a method i defined in the regression.py in another action of my gui it is not possible.
Everything is gone after execution.
...
self.connect(self.buttonOK,
QtCore.SIGNAL("clicked()"), self.onOK)
def onOK(self):
if self.button1.checkState():
a=3
if self.button2.checkState():
a=1
exec(open("./regression.py").read())# this scripts takes the value of "a" to run
# it prints some calculations - but everything is gone after pressing "OK" button
app = QtGui.QApplication(sys.argv)
dialog = MeinDialog()
dialog.show()
Why is that the case? how can i find a solution? is my method of spitting gui and actual calculations using modules like numpy and so on right?
exec is a python call. In PyQt you need to use exec_().
[edit] I read the question too quickly. I thought the problem was starting the eventloop, not getting variables from another process. Can you import your regression module and run it in process, instead of using exec?
Continued from How to use wxSpellCheckerDialog in Django?
I have added spell checking to Django application using pyenchant.
It works correctly when first run. But when I call it again (or after several runs) it gives following error.
PyAssertionError at /quiz/submit/
C++ assertion "wxThread::IsMain()"
failed at ....\src\msw\evtloop.cpp(244) in wxEventLoop::Dispatch():
only the main thread can process Windows messages
How to fix this?
You don't need wxPython to use pyEnchant. And you certainly shouldn't be using wx stuff with django. wxPython is for desktop GUIs, while django is a web app framework. As "uhz" pointed out, you can't call wxPython methods outside the main thread that wxPython runs in unless you use its threadsafe methods, such as wx.CallAfter. I don't know why you'd call wxPython from Django though.
It seems you are trying to use wx controls from inside Django code, is that correct? If so you are doing a very weird thing :)
When you write a GUI application with wxPython there is one main thread which can process Window messages - the main thread is defined as the one where wx.App was created. You are trying to do a UI thing from a non-UI thread. So probably at first run everything works (everything is performed in the GUI thread) but on second attempt a different python thread (spawned by django?) is performing some illegal GUI actions. You could try using wx.CallAfter which would execute a function from the argument in GUI thread but this is non-blocking. Also I've found something you might consider: wxAnyThread wxAnyThread. But I didn't use it and I don't know if it applies in your case.
i'm doing application that locks the PC using pyGtk, but i have a problem, when i click on the ok button the function of the button should get the time from the textbox, hide the window then sleep for a while, and at last lock the pc using a bash command. but it just don't hide.
and here is the complete program
Provided you are using Gnome on Ubuntu
import os
os.system('gnome-screensaver-command –-lock')
Is there any reason for the main class to be a thread? I would make it just a normal class, which would be a lot easier to debug. The reason its not working is that all gtk related stuff must happen in the gtk thread, so do all widget method calls like this: gobject.idle_add(widget.method_name). So to hide the password window: gobject.idle_add(self.pwdWindow.hide)
You'll have to import gobject first of course (You might need to install it first).
EDIT: I don't think that that was your problem, either way I edited your program a lot, here is the modified code.