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.
Related
I am building an user interface using Python, Gtk3 and Glade. I want to change several things on the UI at the same time (i.e start an animation and display a new text) which leads to the application freezing.
I have read that Gtk wasn't thread safe so I didn't used the Thread module.
Instead, I used Glib.idle_add and Gdk.threads_add_idle functions. I am tryig to update a treeview, display some text and show an animated logo at the same time. The application works but it freezes a few seconds and then everything appears at the same time. I try to set different priorities to the threads but it does'nt seem to fix it.
Gtk.threads_add_idle(Glib.PRIORITY_DEFAULT, label.set_text, "text_to_set")
Gtk.threads_add_igle(GLib.PRIORITY_DEFAULT, function_to_display_logo)
I expect the different texts and the treeview and the logo to be displayed without any freeze. Does anyone know how I can fix that ?
Please have a look here at a script example in https://github.com/f4iteightiz/UWR_scoreboard : a GTK window is updated all 0,2s for example (countdowns of several timers appearing in labels; I think anything else could be updated) and it stay reactiv the whole time. No freezing noticeable.
I found out what my error was. I was using the GLib.idle_add function too many times even in some cases where I had no use for it.
For example in the main code I had :
Glib.idle_add(my_function,buffer)
but my_function looked like this :
def myfuntion(buffer):
GLib.idle_add(buffer.set_text,"text")
I deleted the GLib.idle_add call in the main code and now it works perfectly.
I used to use the Interactive Editor for Python, and then I "upgraded" to Pyzo (since IEP was merged into Pyzo). One of my programs uses tkinter to create a GUI. The code used to work just fine: I would run the file as a script, and then, in the interpreter, I would call main, which would launch the application.
The code skeleton looks like this.
def main():
class Application(tk.Frame):
def __init__(self, master=None):
# a bunch of stuff
# several more methods here
front=Application()
front.mainloop()
# then I can either call main in the interpreter, or I can add this:
# the name==main part is to handle some multiprocessing that occurs within the application class
if __name__=="__main__":
main()
This worked like a charm in IEP. However, in Pyzo, main() never launches, or, rather, it launches, but the gui never appears and it doesn't let me do anything. Instead, I get this message: Note: The GUI event loop is already running in the pyzo kernel. Be aware that the function to enter the main loop does not block.
This message occurs in Pyzo when I am using the CPython 3 or PyPy interpreter, but not when I'm using Anaconda 3 (I actually need to use PyPy because the work I'm doing is computationally expensive).
The other alternative is to not use Pyzo, but that's no fun.
I discovered the answer a while ago but I didn't get back to posting the answer until now. Essentially, there is a setting in Pyzo itself which attempts to detect GUIs. Switching that setting from auto to none fixed the problem.
I'm creating Tkinter GUI and want to add windows CMD into tkinter widget. I would like to use console to connect to database. I did some research and found only pyconsole module, but with some bugs:
cls is not going to do what you expect;
edit is not going to show an editor (try start edit instead);
prompt anything fails too;
the color command is not implemented;
the great ^C isn't supported too (it actually copies text, instead of interrupting a process).
Especially ^C command ommited is huge limitation for sql scripts i want to run.
I'm able to open console like this:
Popen(["cmd.exe"], creationflags=CREATE_NEW_CONSOLE)
But with this approach I don't know how to interact with the GUI (is it even possible?)
Also my Text widget can read output from command line, but I need to also write in that command line, not just read it...
Is there a possibility to get regular CMD into Tkinter widget, which will react with the rest of widgets in GUI?
Desired behaviour would be CMD console on the right side as you can see on picture below (in tkinter window), that would interact with the Listbox on the left. I'm not looking for exact code (that's why no my code stated here), but method/solution how to put CMD into tkinter.
Photo:
Thanks
Honza
I think you can use an Entry to input the commands you want to execute.
Then you can use subprocess.run and subprocess.Popen to execute the commands, and a Text or even better a tkinter.scrolledtext.ScrolledText widget to show the results.
I have a PyQt4 program which tend to crash on exit. I have tried forcing exit of QtGui.QMainWindow by using
def closeEvent(self, event):
exit()
But it didn't stop the program from crashing. I also tried to use the
app.setActiveWindow(MainWindow)
exit(app.exec_())
But didn't seems to fix the problem. Finally, I found out a SO post mentioned about the similar probelm which is fixed by setting sip.setdestroyonexit() to False.
Reference
When the Python interpreter exits PyQt4 (by default) calls the C++
destructor of all wrapped instances that it owns. This happens in a
random order and can therefore cause the interpreter to crash. This
behavior can be disabled by calling the sip.setdestroyonexit()
function.
PyQt5 always calls sip.setdestroyonexit() automatically.
I managed to prevent my program from crashing by setting this setting to False.
Now, I am wondering will there be any consequences of setting sip.setdestroyonexit() to False? What will happen if I disable the calls to C++ destructors?
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.