Depending on a conditions I would like to connect/re-connect a button to a different function.
Let's say I have a button:
myButton = QtGui.QPushButton()
For this example let's say I check if there is an internet connection.
if connected == True:
myButton.clicked.connect(function_A)
elif connected == False:
myButton.clicked.connect(function_B)
First of all I would like to disconnect a button from any function it was already connected before the button is being re-assigned/re-connected to another function (function_A or function_B).
Secondly, I have already noticed that after the button is re-connected it takes an extra click for the button to pick up a new function. After the button is re-connected to another function it still attempts to run a previous function - a function to which a button was connected earlier (before a re-connection). Please advice. Thanks in advance!
EDITED LATER:
It appears a widget's .disconnect() method can be used to disconnect a button from a function it it is connected.
myButton.disconnect()
Unfortunately .disconnect() throws an error if a widget is not connected to any function.
To get around it I am using Try/Except. But I would rather use a more elegant solution...
try: myButton.clicked.disconnect()
except Exception: pass
If you need to reconnect signals in many places, then you could define a generic utility function like this:
def reconnect(signal, newhandler=None, oldhandler=None):
try:
if oldhandler is not None:
while True:
signal.disconnect(oldhandler)
else:
signal.disconnect()
except TypeError:
pass
if newhandler is not None:
signal.connect(newhandler)
...
if connected:
reconnect(myButton.clicked, function_A)
else:
reconnect(myButton.clicked, function_B)
(NB: the loop is needed for safely disconnecting a specific handler, because it may have been connected multple times, and disconnect(slot) only removes one connection at a time.).
Try this:
from PyQt4 import QtGui as gui
app = gui.QApplication([])
myButton = gui.QPushButton()
def function_A():
myButton.clicked.disconnect() #this disconnect all!
myButton.clicked.connect(function_B)
print 'function_A'
def function_B():
myButton.clicked.disconnect(function_B) #this disconnect function_B
myButton.clicked.connect(function_A)
print 'function_B'
myButton.clicked.connect(function_A)
myButton.setText("Click me!")
myButton.show()
app.exec_()
Concise way for 3.4+ with contextlib.suppress:
with contextlib.suppress(RuntimeError):
button.clicked.disconnect()
button.connect(func_a if condition else func_b)
Related
I'm trying to make a python3 application for my Raspberry Pi 4B and I have the tkinter windows working fine, but need to add asynchronous handling to allow tkinter widgets to respond while processing asynchronous actions initiated by the window's widgets.
The test code is using asyncio and tkinter. However, without root.mainloop(), since asyncio loop.run_forever() is called at the end instead. The idea is that when the user clicks the main window's close box, RequestQuit() gets called to set the quitRequested flag and then when control returns to the event loop, root.after_idle(AfterIdle) would cause AfterIdle to be called, where the flag is checked and if true, the event loop is stopped, or that failing, the app is killed with exit(0).
The loop WM_DELETE_WINDOW protocol coroutine RequestQuit is somehow not getting called when the user clicks the main window close box, so the AfterIdle coroutine never gets the flag to quit and I have to kill the app by quitting XQuartz.
I'm using ssh via Terminal on MacOS X Big Sur 11.5.2, connected to a Raspberry Pi 4B with Python 3.7.3.
What have I missed here?
(I haven't included the widgets or their handlers or the asynchronous processing here, for brevity, since they aren't part of the problem at hand.)
from tkinter import *
from tkinter import messagebox
import aiotkinter
import asyncio
afterIdleProcessingIntervalMsec = 500 # Adjust for UI responsiveness here.
busyProcessing = False
quitRequested = False
def RequestQuit():
global quitRequested
global busyProcessing
if busyProcessing:
answer = messagebox.askquestion('Exit application', 'Do you really want to abort the ongoing processing?', icon='warning')
if answer == 'yes':
quitRequested = True
def AfterIdle():
global quitRequested
global loop
global root
if not quitRequested:
root.after(afterIdleProcessingIntervalMsec, AfterIdle)
else:
print("Destroying GUI at: ", time.time())
try:
loop.stop()
root.destroy()
except:
exit(0)
if __name__ == '__main__':
global root
global loop
asyncio.set_event_loop_policy(aiotkinter.TkinterEventLoopPolicy())
loop = asyncio.get_event_loop()
root = Tk()
root.protocol("WM_DELETE_WINDOW", RequestQuit)
root.after_idle(AfterIdle)
# Create and pack widgets here.
loop.run_forever()
The reason why your program doesn't work is that there is no Tk event loop, or its equivalent. Without it, Tk will not process events; no Tk callback functions will run. So your program doesn't respond to the WM_DELETE_WINDOW event, or any other.
Fortunately Tk can be used to perform the equivalent of an event loop as an asyncio.Task, and it's not even difficult. The basic concept is to write a function like this, where "w" is any tk widget:
async def new_tk_loop():
while some_boolean:
w.update()
await asyncio.sleep(sleep_interval_in_seconds)
This function should be created as an asyncio.Task when you are ready to start processing tk events, and should continue to run until you are ready to stop doing that.
Here is a class, TkPod, that I use as the basic foundation of any Tk + asyncio program. There is also a trivial little demo program, illustrating how to close the Tk loop from another Task. If you click the "X" before 5 seconds pass, the program will close immediately by exiting the mainloop function. After 5 seconds the program will close by cancelling the mainloop task.
I use a default sleep interval of 0.05 seconds, which seems to work pretty well.
When exiting such a program there are a few things to think about.
When you click on the "X" button on the main window, the object sets its app_closing variable to false. If you need to do some other clean-up, you can subclass Tk and over-ride the method close_app.
Exiting the mainloop doesn't call the destroy function. If you need to do that, you must do it separately. The class is a context manager, so you can make sure that destroy is called using a with block.
Like any asyncio Task, mainloop can be cancelled. If you do that, you need to catch that exception to avoid a traceback.
#! python3.8
import asyncio
import tkinter as tk
class TkPod(tk.Tk):
def __init__(self, sleep_interval=0.05):
self.sleep_interval = sleep_interval
self.app_closing = False
self.loop = asyncio.get_event_loop()
super().__init__()
self.protocol("WM_DELETE_WINDOW", self.close_app)
# Globally suppress the Tk menu tear-off feature
# In the following line, "*tearOff" works as documented
# while "*tearoff" does not.
self.option_add("*tearOff", 0)
def __enter__(self):
return self
def __exit__(self, *_x):
self.destroy()
def close_app(self):
self.app_closing = True
# I don't know what the argument n is for.
# I include it here because pylint complains otherwise.
async def mainloop(self, _n=0):
while not self.app_closing:
self.update()
await asyncio.sleep(self.sleep_interval)
async def main():
async def die_in5s(t):
await asyncio.sleep(5.0)
t.cancel()
print("It's over...")
with TkPod() as root:
label = tk.Label(root, text="Hello")
label.grid()
t = asyncio.create_task(root.mainloop())
asyncio.create_task(die_in5s(t))
try:
await t
except asyncio.CancelledError:
pass
if __name__ == "__main__":
asyncio.run(main())
I am developing a plugin for the GIS software, QGIS. I have a QAction icon which, when checked, connects layers in a group to a function whenever their visibility is toggled. Then when it is unchecked, it is supposed to disconnect these functions but instead I receive an error:
Traceback (most recent call last):
File "C:/Users/Me/.qgis2/python/plugins\Example\Example.py", line 248, in run
layers.visibilityChanged.disconnect(print_one)
TypeError: 'function' object is not connected
This is an example code:
def run(self, checked):
root = QgsProject.instance().layerTreeRoot()
group = root.findGroup('Group')
def print_one():
print 'one'
if checked == True:
for layers in group.children():
layers.visibilityChanged.connect(print_one)
else:
for layers in group.children():
layers.visibilityChanged.disconnect(print_one)
Why is the signal not being disconnected?
I could just use layers.visibilityChanged.disconnect() but this disconnects all signals to it so is not in my interest.
For what I understood in PyQt manual, you should try this way :
layers.disconnect(print_one)
But I'm not sure and sadly I don't have time to try it...
From the documentation (emphasis mine):
disconnect([slot])
Disconnect one or more slots from a signal. An exception will be raised if the slot is not connected to the signal or if the signal has no connections at all.
Thus, you are receiving an exception because the signal is not connected to the slot when you are trying to disconnect it.
As a workaround:
if checked == True:
for layers in group.children():
layers.visibilityChanged.connect(print_one)
else:
for layers in group.children():
try:
layers.visibilityChanged.disconnect(print_one)
except:
pass
Guess I found an alternative method which is to include an if statement to check if the QAction icon is checked or not and place this inside the print_one() function:
def run(self):
root = QgsProject.instance().layerTreeRoot()
group = root.findGroup('Group')
def print_one():
if self.plugin_icon.isChecked():
print 'one'
else:
layers.visibilityChanged.disconnect(print_one)
for layers in group.children():
layers.visibilityChanged.connect(print_one)
Still curious as to why I couldn't disconnect it using the method shown in the question but in the meantime, this works.
I wrote an pyqt gui and used threading to run code which needs a long time to be executed, but I want to have the choice to stop the execution safely. I dont want to use the get_thread.terminate() method. I want to stop the code by a special function (maybe del()). My problem is that, I wrote the code in a own class and just want to abort the class without changing a lot of syntax.
Edit: It was mentioned that one has to pass a flag to the class, which has to be checked constantly. How do I send this flag to the class? Because the flag has to change the value, when one presses the stop button.
Edit 2: My solution so far is, to declerate a global variable with the name running_global. I changed self.get_thread.terminate() to running_global = False and I check constantly in my long_running_prog if the variable has been set False. I think this solution is ugly, so I would be pretty happy if someone has a better idea.
This is my code for the dialog where I start the thread:
class SomeDialog(QtGui.QDialog,
userinterface_status.Ui_status_window):
finished = QtCore.pyqtSignal(bool)
def __init__(self):
"""
:param raster: Coordinates which are going to be scanned.
"""
super(self.__class__, self).__init__() # old version, used in python 2.
self.setupUi(self) # It sets up layout and widgets that are defined
self.get_thread = SomeThread()
# Conencting the buttons
self.start_button.clicked.connect(self.start)
self.stop_button.clicked.connect(self.stop)
self.close_button.clicked.connect(self.return_main)
# Connecting other signals
self.connect(self.get_thread, QtCore.SIGNAL("stop()"), self.stop)
self.connect(self.get_thread, QtCore.SIGNAL("update_status_bar()"), self.update_status_bar)
def return_main(self):
"""
Function is excecuted, when close button is clicked.
"""
print("return main")
self.get_thread.terminate()
self.close()
def start(self):
"""
Starts the thread, which means that the run method of the thread is started.
"""
self.start_button.setEnabled(False)
self.get_thread.start()
def stop(self):
print("Stop programm.")
self.start_button.setEnabled(True)
self.get_thread.quit()
def end(self):
QtGui.QMessageBox.information(self, "Done!", "Programm finished")
def closeEvent(self, event):
"""
This method is called, when the window is closed and will send a signal to the main window to activaete the
window again.
:param event:
"""
self.finished.emit(True)
# close window
event.accept()
In the following class is the code for the thread:
class SomeThread(QtCore.QThread):
finished = QtCore.pyqtSignal(bool)
def __init__(self):
QtCore.QThread.__init__(self)
def __del__(self):
print("del")
self.wait()
def run(self):
self.prog = long_running_prog(self.emit) # Sending from the prog signals
self.prog.run()
self.prog.closeSystem() # Leaving the programm in a safe way.
So if one presses the stop button, the programm should instantly shut down in a save way. Is there a way to abort the class in a save way? For example can I pass a variable to the long_running_prog class which turns True, when one presses the stop button? If somethin like this is possible, could one tell me how?
Thanks for your help in advance
I hope you understand my problem.
Greetings
Hizzy
This is impossible to do unless prog.run(self) would periodically inspect a value of a flag to break out of its loop. Once you implement it, __del__(self) on the thread should set the flag and only then wait.
I am trying to code an application that consists of various windows (e.g., generic message dialog, login dialog, main interface, etc.) and am having trouble getting the gtk.main_quit function to be called: either I get a complaint about the call being outside the main loop, or the function doesn't get called at all.
I am a newbie to both Python and GTK+, but my best guess as to how to get this to work is to have a "root" window, which is just a placeholder that is never seen, but controls the application's GTK+ loop. My code, so far, is as follows:
import pygtk
pygtk.require("2.0")
import gtk
class App(gtk.Window):
_exitStatus = 0
# Generic message box
def msg(self, title, text, type = gtk.MESSAGE_INFO, buttons = gtk.BUTTONS_OK):
# Must always have a button
if buttons == gtk.BUTTONS_NONE:
buttons = gtk.BUTTONS_OK
dialog = gtk.MessageDialog(None, 0, type, buttons, title)
dialog.set_title(title)
dialog.set_geometry_hints(min_width = 300)
dialog.set_resizable(False)
dialog.set_deletable(False)
dialog.set_position(gtk.WIN_POS_CENTER)
dialog.set_modal(True)
dialog.format_secondary_text(text)
response = dialog.run()
dialog.destroy()
return response
def nuke(self, widget, data):
gtk.main_quit()
exit(self._exitStatus)
def __init__(self):
super(App, self).__init__()
self.connect('destroy', self.nuke)
try:
raise Exception()
except:
self.msg('OMFG!', 'WTF just happened!?', gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE)
self._exitStatus = 1
self.destroy()
if self.msg('OK', 'Everything worked fine') == gtk.RESPONSE_OK:
self.destroy()
# Let's go!
App()
gtk.main()
The nuke function never gets called, despite the explicit calls to destroy.
DIFF On #DonQuestion's advice:
- self.destroy()
+ self.emit('destroy')
- App()
+ app = App()
This didn't solve the problem...
UPDATE Accepted #jku's answer, but also see my own answer for extra information...
First, there is a bit of a test problem with the code: You call Gtk.main_quit() from the App initialization: this happens before main loop is even running so signals probably won't work.
Second, you'll probably get a warning on destroy(): 'destroy' handler only takes two arguments (self plus one) but yours has three...
Also with regards to your comment about control flow: You don't need a Window to get signals as they're a GObject feature. And for your testing needs you could write a App.test_except() function and use glib.idle_add (self.test_except) in the object initialization -- this way test_except() is called when main loop is running.
I think #jku's answer identifies my key error, so I have marked it accepted, but while playing around, I found that the MessageDialog does not need to run within the GTK+ loop. I don't know if this is as designed, but it works! So, I broke my generic message dialog out into its own function and then kept the main app altogether in a class of its own, which respects the main loop as I was expecting:
import pygtk
pygtk.require("2.0")
import gtk
def msg(title, text, type = gtk.MESSAGE_INFO, buttons = gtk.BUTTONS_OK):
# Only allowed OK, Close, Cancel, Yes/No and OK/Cancel buttons
# Otherwise, default to just OK
if buttons not in [gtk.BUTTONS_OK, gtk.BUTTONS_CLOSE, gtk.BUTTONS_CANCEL, gtk.BUTTONS_YES_NO, gtk.BUTTONS_OK_CANCEL]:
buttons = gtk.BUTTONS_OK
dialog = gtk.MessageDialog(None, 0, type, buttons, title)
dialog.set_title(title)
dialog.set_geometry_hints(min_width = 300)
dialog.set_resizable(False)
dialog.set_deletable(False)
dialog.set_position(gtk.WIN_POS_CENTER)
dialog.set_modal(True)
dialog.format_secondary_text(text)
response = dialog.run()
dialog.destroy()
return response
class App:
def __init__(self):
# Build UI
# Connect signals
# Show whatever
def appQuit(self, widget):
gtk.main_quit()
def signalHandler(self, widget, data = None):
# Handle signal
# We can call msg here, when the main loop is running
# Load some resource
# We can call msg here, despite not having invoked the main loop
try:
# Load resource
except:
msg('OMFG!', 'WTF just happened!?', gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE)
exit(1)
# n.b., Calls to msg work even without the following code
App()
gtk.main()
exit(0)
I've made a runnable example that demonstrates the buggy behavior: http://pastebin.com/8KpzD4pw
This issue is EXTREMELY aggravating. I have a wx.ProgressDialog up while I'm saving a file, and upon IOError, I want to close the progress dialog and display an error message. Unfortunately, this seems to be impossible. The progress dialog blatantly refuses to close before the message box closes:
As you can see, the message box appears below the progress dialog, so the user has to manually switch focus to the message box to see it's contents. When the message box is closed, the progress dialog disappears as well. Here is the code for the save function:
def save(self, path=None):
# Show a loading dialog while the document is staving.
progress = shared.show_loading(self, 'Saving document')
try:
raise IOError('Error message')
if not path:
self.document.save()
else:
self.document.save_to_file(path)
except IOError as e:
progress.done()
message = 'Failed to save file:\n\n{}'.format(e.message)
wx.MessageBox(message, 'Error', wx.OK | wx.ICON_ERROR)
progress.done()
The show_loading and progress.done functions are just shortcuts for using the wx.ProgressDialog (source).
Why does the progress dialog not disappear before the message box is opened? How can I fix it?
I have also tried using wx.CallAfter to open the message box, to no avail:
# ...
except IOError as e:
message = 'Failed to save file:\n\n{}'.format(e.message)
def show_error():
wx.MessageBox(message, 'Error', wx.OK | wx.ICON_ERROR)
progress.done()
wx.CallAfter(show_error)
# ...
I have also tried to sleep for 100ms between closing the progress dialog and opening the message box using wx.MicroSleep without success.
I have also tried calling wx.Yield() and wx.WakeUpIdle() right after destroying the progress dialog, neither having any effect.
Just out of curiosity... Have you tried using wx.SafeYield() or wx.Yield() or wx.YieldIfNeeded() right after the call to progressdialog.Destroy()?
Your sample is not runnable as it stands so I am just shooting in the dark.
I think Infinity77 has the right answer here. People forget that GUI calls are not synchronous -- they aren't finished by the time they return. That "done" call sends a message to the window, and in response to that, the window probably queues up several more messages to clean itself up. When you fire up a model message box, that creates its OWN message loop, while leaving the original message loop in suspended animation. Thus, the cleanup messages cannot be processed until the message box returns and your main message loop runs again. A Yield call will allow those queued up messages to drain.
I had a similar case, which I finally resolved by calling:
dlg.Update( dlg.GetRange( ) )
It seems that, at least when you put the progress dialog into "pulse" mode, it won't immediately respond to Destroy calls. No amount of sleeping or yielding before or after destroying it would convince my progress dialog to stop displaying. However, by instead simply updating the value to the max, it seems to automatically destroy (or at least hide) itself immediately.
The wxPython demo shows how to interrupt a ProgressDialog. It also shows that you need to Destroy() it instead of Close() it, which is the normal way of getting rid of dialogs. In your exception handler, you will want to stop whatever the ProgressDialog is keeping track of and Destroy() it. Then show your MessageBox.
I have figured out a workaround. It turns out that I can't remove the native windows progress dialog right after I create it. I have to wait a while, probably for the dialog to be completely initialized, before I'm allowed to destroy it. I added this code:
wx.MilliSleep(50)
Into my progress dialog shortcut function, which introduces an unnoticeable delay after opening the progress dialog and allows me to destroy the progress dialog when ever I want.
Complete shortcut function:
def show_loading(parent, title, message=None, maximum=100):
if not message:
message = title
# A class for the return value.
class LoadingObject(object):
def __init__(self, dialog):
self.dialog = dialog
self.is_done = False
def done(self):
if not self.is_done:
self.dialog.Destroy()
self.is_done = True
def pulse(self, message):
self.dialog.Pulse(message)
def progress(self, current, message=None):
# Don't allow the progress to reach 100%, since it will freeze the
# dialog.
if current >= maximum:
current = current - 1
if message is not None:
self.dialog.Update(current, message)
else:
self.dialog.Update(current)
# Create the progress dialog.
dlg_style = wx.PD_APP_MODAL | wx.PD_ELAPSED_TIME | wx.PD_REMAINING_TIME
dlg = wx.ProgressDialog(
title, message, parent=parent, style=dlg_style, maximum=maximum
)
dlg.Pulse()
# Wait just a little bit to allow the progress dialog to initialize.
wx.MilliSleep(50)
# Return an instance of the LoadingDialog with the progress dialog attached.
return LoadingObject(dlg)
Final save function:
def save(self, path=None):
# Show a loading dialog while the document is staving.
progress = shared.show_loading(self, 'Saving document')
try:
if not path:
self.document.save()
else:
self.document.save_to_file(path)
except IOError as e:
message = 'Failed to save file:\n\n{}'.format(e.message)
wx.MessageBox(message, 'Error', wx.OK | wx.ICON_ERROR)
finally:
progress.done()
I took a slightly different approach to this problem. Had multiple function calls in a linear order, and wanted to show the overall progress as function calls were done. I simply wrapped the progress bar in a function, taking the function to call and its arguments. On exception I destroy the progress bar, and raise the exception. Example below:
def _progress_wrap(self, func, *args, **kwargs):
self.count += 1
self.progress.Update(self.count)
res = None
try:
res = func(*args, **kwargs)
except Exception:
self.progress.Destroy()
raise
return(res)