When I change between 2 options, it always returns double result (tex,tex/sculpt,sculpt). It was happening it my past projects too but I never got it solved. Restarting maya didn't work, even with rewritten code it kept happening. Any suggestions?
import maya.cmds as cmds
class UI(object):
def __init__(self):
a=cmds.window()
cmds.showWindow(a)
cmds.columnLayout()
self.displaceOptions = cmds.radioButtonGrp(la2=['Texture', 'Sculpting'], nrb=2, en=True, cc=self.check)
def check(self, *args):
option = cmds.radioButtonGrp(self.displaceOptions, q=True, sl=True)
if option == 1:
self.dispTexture()
elif option == 2:
self.dispSculpt()
def dispTexture(*args):
print('tex')
def dispSculpt(*args):
print('sculpt')
UI()
The reason is that the changeCommand reacts to the state change which is changed twice, first one radiobutton is deactivated, then another is activated. The very first call of the UI() has no radio button selected, if you select one, the callback is called only once because the state only changes once.
You could use onCommand, or offCommand which should behave a bit more as you expect.
Related
I’m writing a thing in maya and have run in to trouble. Really don’t know what I did, I was going to adress something else when this happened, the last thing I did was add a button to the layout.
I have been messing around with this for a long time now and as far as I can tell, the uiScript flag doesn’t like arguments passed in the method call…? what happens is, it never sets the restore flag to True so that bit never hits. resulting in it spawning multiple windows in maya. I’m also trying to figure out where the cmds.deleteUI try clause should go, not entrirely sure where I had it before this happened. If anyone could offer any insight I would be most grateful, cheers /S
in the script below, I have replaced an instance of my ui with just a button, it makes no difference on the behaviour.
code:
//////////
from PySide2 import QtWidgets, QtCore
from maya.app.general.mayaMixin import MayaQWidgetDockableMixin
import maya.OpenMayaUI as mui
import maya.cmds as cmds
import weakref
if not 'customMixinWindow' in globals():
customMixinWindow = None
class DockableWidget(MayaQWidgetDockableMixin, QtWidgets.QWidget):
instances = list()
CONTROL_NAME = 'customMixinWindow'
def __init__(self, parent=None):
super(DockableWidget, self).__init__(parent=parent)
DockableWidget.delete_instances()
self.__class__.instances.append(weakref.proxy(self))
self.main_layout = QtWidgets.QVBoxLayout()
self.button = QtWidgets.QPushButton()
self.main_layout.addWidget(self.button)
self.setLayout(self.main_layout)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
#staticmethod
def delete_instances():
print "deleting"
for ins in DockableWidget.instances:
try:
ins.setParent(None)
ins.deleteLater()
except:
pass
DockableWidget.instances.remove(ins)
del ins
def DockableWidgetUIScript(restore=False):
global customMixinWindow
if restore == True:
restoredControl = mui.MQtUtil.getCurrentParent()
customMixinWindow = DockableWidget()
if customMixinWindow is None:
#customMixinWindow = DockableWidget()
customMixinWindow.setObjectName('customMayaMixinWindow')
if restore == True:
mixinPtr = mui.MQtUtil.findControl(customMixinWindow.objectName())
mui.MQtUtil.addWidgetToMayaLayout(long(mixinPtr), long(restoredControl))
else:
try:
cmds.workspaceControl('customMayaMixinWindowWorkspaceControl', e=True, close=True)
cmds.deleteUI('customMayaMixinWindowWorkspaceControl')
except:
pass
customMixinWindow.show(dockable=True, restore=True, height=400, width=400, uiScript='import dockWin; dockWin.DockableWidgetUIScript(restore=True)')
def main():
ui = DockableWidgetUIScript()
return ui
if __name__ == 'dockWin':
main()
ok, problem solved.
after a lot of troubleshooting, it turned out to be a QSpacerItem in my ui that caused the window to open once and then crash maya the second time. which made me think it had something to do with the deleteUI stuff.
I had the QSpacerItem added like so:
self.spacerFive = QtWidgets.QSpacerItem(5 , 5)
self.myLayout.addSpacerItem(self.spacerFive)
this made maya crash out completely when deleteing the UI/workspaceControl…
no idea why, garbage collection?
this fixed it:
self.myLayout.addSpacerItem(QtWidgets.QSpacerItem(5, 5))
alright, everything working as expected again. cheers /S
self.pushButton.clicked.connect(self.search)
def search(self):
import subprocess
keyword = str(unicode(self.lineEdit.text()))
subprocess.call(["some command"])
video_list = []
self.listWidget.clear()
video_ret = parse_vid(video_list)
self.listWidget.addItems(video_ret)
self.listWidget.itemDoubleClicked.connect(self.surf)
#######################################################################
def surf(self):
print "hello"
This code work fine for first time. But if I click pushbutton for the second time item click in listwidget gives me two execution of surf method. If I click pushbutton third time item click in listwidget execute surf method three time. Can any one help me with this weird problem??
A signal can be connected to multiple slots, in your case whenever you use the search function you are adding a new slot, it is better to add it once.
self.listWidget.itemDoubleClicked.connect(self.surf)
self.pushButton.clicked.connect(self.search)
def search(self):
import subprocess
keyword = str(unicode(self.lineEdit.text()))
subprocess.call(["some command"])
video_list = []
self.listWidget.clear()
video_ret = parse_vid(video_list)
self.listWidget.addItems(video_ret)
#######################################################################
def surf(self):
print("hello")
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.
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)
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)