动态控件基础例子 - 动态生成菜单

This commit is contained in:
Lin JH 2018-04-16 15:55:07 +08:00
parent d631cb15c6
commit e3e316970b
5 changed files with 208 additions and 0 deletions

View file

@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'D:\pyPro\dynamic_Menu\动态控件.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(370, 403)
Dialog.setSizeGripEnabled(True)
self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
self.verticalLayout.setObjectName("verticalLayout")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())

View file

@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Project SYSTEM "Project-5.1.dtd">
<!-- eric project file for project dynamic_Controls -->
<!-- Saved: 2018-04-11, 02:00:19 -->
<!-- Copyright (C) 2018 , -->
<Project version="5.1">
<Language>en_US</Language>
<Hash>b35a15d37a7cb52779310396c238b2e70ac76888</Hash>
<ProgLanguage mixed="0">Python3</ProgLanguage>
<ProjectType>PyQt5</ProjectType>
<Version>0.1</Version>
<Author></Author>
<Email></Email>
<Eol index="0"/>
<Sources>
<Source>Ui_动态控件.py</Source>
<Source>__init__.py</Source>
<Source>动态控件.py</Source>
</Sources>
<Forms>
<Form>动态控件.ui</Form>
</Forms>
<Translations/>
<Resources/>
<Interfaces/>
<Others/>
<Vcs>
<VcsType>None</VcsType>
</Vcs>
<FiletypeAssociations>
<FiletypeAssociation pattern="*.e4p" type="OTHERS"/>
<FiletypeAssociation pattern="*.idl" type="INTERFACES"/>
<FiletypeAssociation pattern="*.md" type="OTHERS"/>
<FiletypeAssociation pattern="*.py" type="SOURCES"/>
<FiletypeAssociation pattern="*.py3" type="SOURCES"/>
<FiletypeAssociation pattern="*.pyw" type="SOURCES"/>
<FiletypeAssociation pattern="*.pyw3" type="SOURCES"/>
<FiletypeAssociation pattern="*.qm" type="TRANSLATIONS"/>
<FiletypeAssociation pattern="*.qrc" type="RESOURCES"/>
<FiletypeAssociation pattern="*.rst" type="OTHERS"/>
<FiletypeAssociation pattern="*.ts" type="TRANSLATIONS"/>
<FiletypeAssociation pattern="*.txt" type="OTHERS"/>
<FiletypeAssociation pattern="*.ui" type="FORMS"/>
<FiletypeAssociation pattern="README" type="OTHERS"/>
<FiletypeAssociation pattern="README.*" type="OTHERS"/>
</FiletypeAssociations>
</Project>

View file

@ -0,0 +1,103 @@
# -*- coding: utf-8 -*-
"""
Module implementing Dialog.
"""
from PyQt5 import QtGui, QtWidgets, QtCore, QtWinExtras
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from functools import partial
from Ui_动态控件 import Ui_Dialog
class Dialog(QDialog, Ui_Dialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setupUi(self)
self.dynamic1()
self.dynamic2()
# 法一
def dynamic1(self):
for i in range(5):
self.pushButton = QtWidgets.QPushButton(self)
self.pushButton.setText("pushButton%d"%i)
self.pushButton.setObjectName("pushButton%d"%i)
self.verticalLayout.addWidget(self.pushButton)
self.pushButton.setContextMenuPolicy(Qt.CustomContextMenu)
self.pushButton.customContextMenuRequested.connect(lambda:self.helpMenu(i))#右键请求,传入i实际上一直是4
self.pushButton.clicked.connect(self.pr)
# 法二
def dynamic2(self):
for i in range(5, 8):
txt="""
self.pushButton_{i} = QtWidgets.QPushButton(self);
self.pushButton_{i}.setText("pushButton{i}");
self.pushButton_{i}.setObjectName("pushButton{i}");
self.verticalLayout.addWidget(self.pushButton_{i});
self.pushButton_{i}.setContextMenuPolicy(Qt.CustomContextMenu)
self.pushButton_{i}.customContextMenuRequested.connect(partial(self.helpMenu,i))#右键请求,用lambda会报错,partial需要import
self.pushButton_{i}.clicked.connect(self.pr)
""".format(i=i)
exec(txt)
#只能法二可用的方式
self.pushButton_5.clicked.connect(self.pr2)
self.pushButton_6.clicked.connect(self.pr2)
self.pushButton_7.clicked.connect(self.pr2)
def helpMenu(self, *type):
'''帮助按钮的右键菜单'''
print(type)
popMenu =QMenu()
if type[0]==5 or self.sender().text()== 'pushButton0':
popMenu.addAction(u'关于',self.pr)
popMenu.addSeparator()
elif type[0]==6 or self.sender().text()== 'pushButton1':
popMenu.addAction(u'清理图片',self.pr)
popMenu.addSeparator()
elif type[0]==7 or self.sender().text()== 'pushButton2':
gitMenu=QMenu('同步到Git', popMenu)
self.pushAction=QAction(u'上传', self, triggered=lambda:self.pr)
gitMenu.addAction(self.pushAction)
self.pullAction=QAction(u'下载', self, triggered=lambda:self.pr)
gitMenu.addAction(self.pullAction)
gitMenu.addSeparator()
gitMenu.addAction(u'配置仓库',lambda:self.pr)
popMenu.addAction(gitMenu.menuAction())#!!
popMenu.exec_(QCursor.pos())#鼠标位置
def pr(self):
'''法一和法二都可用的调用
if self.sender().objectName=='XXX':
self.pr2()
'''
print(self.sender().text())
print(self.sender().objectName())
print(self.pushButton.text())
def pr2(self):
print(2)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
ui = Dialog()
ui.show()
sys.exit(app.exec_())

View file

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>370</width>
<height>403</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<property name="sizeGripEnabled">
<bool>true</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout"/>
</widget>
<resources/>
<connections/>
</ui>