使Pyside更聪明一点。
smartside的Python项目详细描述
smartside是以简单方式连接pyside信号的许多可用方法之一。
它能为您做什么的示例
假设您使用qt设计器设计了一个gui
编译时使用:
$ pyside-uic.exe myform.ui -o myform_ui.py
# if you have resources included compile them too
$ pyside-rcc.exe myresources.qrc -o myresources_rc.py
然后使用这样的代码显示表单并绑定一些信号:
importsysfrommyform_uiimport*# this will also include `QtCore` and `QtGui`importsmartside.signalassmartsignalclassMyApplication(QtGui.QMainWindow,Ui_MainWindow,smartsignal.SmartSignal):def__init__(self,parent=None):super(MyApplication,self).__init__(parent)self.setupUi(self)# create any local UI object here, so they signal are# going to be auto-connected tooself.auto_connect()# will respond to stateChanged signal from checkBox widget# notice the double underline between widget name and signal namedef_on_checkBox__stateChanged(self):print'check',self.sender().isChecked()# will respond to `pressed` signal of btn_add widgetdef_on_btn_add__pressed(self):print'btn_add was pressed'# list some widgets and use regex `regex`, to select multiples.# starting with underline is mandatory_myfuncs='btn_base, btn_format, `btn_.+log.+`, btn_sqr'# will respond to clicked signal of all widget listed abovedef_when_myfuncs__clicked(self):print'multiples',self.sender()if__name__=="__main__":app=QtGui.QApplication(sys.argv)window=MyApplication()window.show()# uncomment line below to print a list of ALL signals available on your form# window.print_all_signals()sys.exit(app.exec_())
在本例中,您的表单应该被称为ui_mainwindow。
首先,我们像往常一样使用setupUi来创建接口。
然后auto_connect将在成员函数匹配时将它们连接到信号。
最后一种情况使用多个连接,因此多个小部件将调用相同的回调函数。您还可以使用regex来选择相关的小部件。在上面的例子中,我们通过显式名称选择了一些小部件,还选择了所有名称以'btn\_'开头并且在其名称的某些部分有'log'的小部件。它们都将连接到_when_myfuncs__clicked。
是的,它也适用于行动。就像def _on_actionTest__triggered(self):。这在通过代码创建上下文菜单时很有用。只要记住在创建菜单后调用auto_connect。
在Windows任务栏上显示图标
通常windows 7+将python脚本作为一个组执行,并将定义到gui的每个图标作为python任务栏图标的子图标放置,因为python实际上托管了代码。即使将.pyw作为python脚本的扩展,也会发生这种情况。
要解决此问题,您必须通过调用smartside.setAsApplication()告诉windows您的脚本是一个应用程序,并将脚本的唯一标识符传递给此函数,例如:“company.product.version”。
# ....if__name__=="__main__":fromsmartsideimportsetAsApplicationsetAsApplication('example_co.exampleProd.'+__version__)app=QtGui.QApplication(sys.argv)window=MyApplication()window.show()sys.exit(app.exec_())
控制台小部件
使用qt设计器将qplaintextedit提升为ConsoleWidget,并使用smartside.console作为头(源)。
然后,在这个表单的__init__内部使用:self.name_of_widget.setLocals({'name': object, 'me': self})。
这将使升级后的qplaintextedit成为一个可访问两个对象的python控制台:name和me。
语言
函数getBestTranslation用于发现应用程序可用的最佳翻译文件。
它将按以下顺序在文件夹中查找.qm文件:
- en-us.qm
- en_us.qm
- en.qm
您可以指定所需语言的列表,或让函数检查系统语言。如果找不到翻译,将使用母语。
# ....if__name__=="__main__":fromsmartsideimportgetBestTranslation# this will look for translations inside folder ./i18n# it will search by system languagestranslator=getBestTranslation('i18n')app=QtGui.QApplication(sys.argv)app.installTranslator(translator)window=MyApp()window.show()sys.exit(app.exec_())
# if you want to specify the languagestranslator=getBestTranslation('language',['pt-BR','es'])
在这种情况下,它将尝试:
- 语言/pt-br.qm
- 语言/pt_br.qm
- 语言/pt.qm
- 语言/es.qm
更改历史记录
0.1.7: | Added language locator. |
---|---|
0.1.6: | Fixed setup typo. |
0.1.5: | Added support to python 3. |
0.1.4: | Added ^{tt10}$ class. |
0.1.3: | Added ^{tt19}$. |
0.1.2: | Added QAction support; For every QAction created before calling auto_connect() you can use ^{tt20}$ like you do with signals. |
0.1.1: | Small fix. |
- 开发:
- https://github.com/xgvargas/smartside-如果发现问题或认为pyside上的任何其他任务可以简化,请使用此空间。