from PySide2 import QtWidgets, QtCore
from abc import ABCMeta, abstractmethod
class Widget():
__metaclass__ = ABCMeta
@abstractmethod
def __call__(self, **kwargs):
for key in kwargs:
if key == "t":
self.title()
if key == "n":
self.widgetName()
if key == "c":
self.color()
if key == "tt":
self.toolTip()
if key == "p":
self.parent()
def title(self):
print "this will give the widget a title"
def widgetName(self):
print "this will give the widget a name"
def color(self):
print "this will give the widget color"
def toolTip(self):
print "this will pop up a tooltip"
def parent(self):
print "this will give a parent to the widget"
class PushButton(Widget):
def __call__(self, **kwargs):
super(PushButton, self).__call__(**kwargs)
class ComboBox(Widget):
def __call__(self, **kwargs):
super(ComboBox, self).__call__(**kwargs)
# Run
pushBtn = PushButton()
pushBtn(t="push me", n="pushBtnA")
pushBtn(t="push me too", n="pushBtB", c=("rgb(100, 25, 75)"), p=None, tt="I am here to help")
cmbBox = ComboBox()
cmbBox(n="comboBoxA")
--Thanks,R
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/e44bf7ee-783f-4080-bde6-3a286a1f65f5%40googlegroups.com.
I see. I am going to the corner and think about what I've done. I' ll have a second look at it and try again.
Thanks,I appreciate the feedback
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/9a56546f-93b6-4960-a9bf-3cde380d3ed2%40googlegroups.com.
I see. I am going to the corner and think about what I've done. I' ll have a second look at it and try again.
Thanks,I appreciate the feedback
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/9a56546f-93b6-4960-a9bf-3cde380d3ed2%40googlegroups.com.
My goal here is to avoid having to wright multiple lines to set a widget. So instead of using .setColor() then .setTitle() then .setObjectName() etc... Have it all in on call that works with any type of widget.
So I am just thinking about a way to abstract it, or maybe create just a wrapper. I don't know yet what would be a clean approach
from PySide2 import QtWidgets, QtCore
class WidgetFunctor(object):
def __call__(self, widget, **kwargs):
self.widget = widget
for key in kwargs:
self.setTitle(kwargs[key]) if key == "title" else None
self.setObjectName(kwargs[key]) if key == "objectName" else None
self.setSize(kwargs[key][0], kwargs[key][1]) if key == "size" else None
return self.widget
def setTitle(self, title):
self.widget.setText(title)
print "setting widget title success!"
def setObjectName(self, objectName):
self.widget.setObjectName(objectName)
print "setting objectName success!"
def setSize(self, width, height):
self.widget.resize(width, height)
print "setting parent success!"
# And so on with more methods...
# Using the functor
widgetFunctor = WidgetFunctor()
buttonA = widgetFunctor(QtWidgets.QPushButton(), title="foo", objectName="buttonA_btn", size=(60, 20))
buttonB = widgetFunctor(QtWidgets.QPushButton(), title="luu", objectName="buttonB_btn"))
comboBoxA = widgetFunctor(QtWidgets.QComboBox(), objectName="comboA_btn", size=(40, 50))
# Equivalent without functor (this is what I want to avoid)
buttonA = QtWidgets.QPushButton()
buttonA.setText("foo")
buttonA.setObjectName("buttonA_btn")
buttonA.resize(60, 20)
buttonB = QtWidgets.QPushButton()
buttonB.setText("luu")
buttonB.setObjectName("buttonB_btn")
comboBoxA = QtWidgets.QComboBox()
comboBoxA.setObjectName("comboA_btn")
comboBoxA.resize(40, 50)
My goal here is to avoid having to wright multiple lines to set a widget.
What’s wrong with using multiple lines to initialise a widget?
Equivalent without functor (this is what I want to avoid)
I don’t have 40 years of experience, but frankly that example is a lot easier to read IMO. Have you considered subclassing?
buttonA = MyPushButton()
class MyPushButton(QtWidgets.QPushButton):
def __init__(self, text, parent=None):
super(MyPushButton, self).__init__(text, parent)
self.setObjectName("myPushButton")
self.resize(60, 20)
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/898d0237-0aed-4d2c-adcf-c4783ac5fe24%40googlegroups.com.
R
Hey Marcus,
Subclassing is how I had my previous code. I am just exploring to see if can come up with something that could create any type of widget using one unique function, but
I might be over thinking it. Does the design I posted make any sense or is it completely wrong for any reason I don't see?
I forgot to mention that the methods are supposed to be private. So basically, the idea was to create a function object to set up any type of widget.
R
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/96a7b5f6-9ab4-4028-93a3-c57fe1df6c2b%40googlegroups.com.