Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 01b2eb3

Browse files
Merge pull request #12 from 625781186/master
增加1.20例子
2 parents 74c4356 + da7f446 commit 01b2eb3

File tree

11 files changed

+382
-1
lines changed

11 files changed

+382
-1
lines changed

‎README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
- [1.17 下拉选择联动](下拉选择联动/)
2525
- [1.18 人脸描点检测](人脸描点检测/)
2626
- [1.19 腾讯视频热播列表](腾讯视频热播列表/)
27-
- [1.20 exec()动态生成控件](partner_625781186/1.exec动态生成控件/)
27+
- 1.20 exec()动态生成控件
28+
- [1. 动态控件基础例子 - 动态生成按钮](partner_625781186/1.exec动态生成控件/dynamic_button)
29+
- [2. 动态控件基础例子 - 动态生成菜单](partner_625781186/1.exec动态生成控件/dynamic_Menu)
30+
- [3. 配合setting记录模型类型](partner_625781186/1.exec动态生成控件/)
2831
- [1.21 仿QQ设置面板](仿QQ设置面板/)
2932
- [1.22 Json生成QTreeWidget](Json生成QTreeWidget/)
3033
- [1.23 嵌入外部窗口](嵌入外部窗口/)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Form implementation generated from reading ui file 'D:\pyPro\dynamic_Menu\动态控件.ui'
4+
#
5+
# Created by: PyQt5 UI code generator 5.9.2
6+
#
7+
# WARNING! All changes made in this file will be lost!
8+
9+
from PyQt5 import QtCore, QtGui, QtWidgets
10+
11+
class Ui_Dialog(object):
12+
def setupUi(self, Dialog):
13+
Dialog.setObjectName("Dialog")
14+
Dialog.resize(370, 403)
15+
Dialog.setSizeGripEnabled(True)
16+
self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
17+
self.verticalLayout.setObjectName("verticalLayout")
18+
19+
self.retranslateUi(Dialog)
20+
QtCore.QMetaObject.connectSlotsByName(Dialog)
21+
22+
def retranslateUi(self, Dialog):
23+
_translate = QtCore.QCoreApplication.translate
24+
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
25+
26+
27+
if __name__ == "__main__":
28+
import sys
29+
app = QtWidgets.QApplication(sys.argv)
30+
Dialog = QtWidgets.QDialog()
31+
ui = Ui_Dialog()
32+
ui.setupUi(Dialog)
33+
Dialog.show()
34+
sys.exit(app.exec_())
35+

‎partner_625781186/1.exec动态生成控件/dynamic_Menu/__init__.py

Whitespace-only changes.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE Project SYSTEM "Project-5.1.dtd">
3+
<!-- eric project file for project dynamic_Controls -->
4+
<!-- Saved: 2018年04月11日, 02:00:19 -->
5+
<!-- Copyright (C) 2018 , -->
6+
<Project version="5.1">
7+
<Language>en_US</Language>
8+
<Hash>b35a15d37a7cb52779310396c238b2e70ac76888</Hash>
9+
<ProgLanguage mixed="0">Python3</ProgLanguage>
10+
<ProjectType>PyQt5</ProjectType>
11+
<Version>0.1</Version>
12+
<Author></Author>
13+
<Email></Email>
14+
<Eol index="0"/>
15+
<Sources>
16+
<Source>Ui_动态控件.py</Source>
17+
<Source>__init__.py</Source>
18+
<Source>动态控件.py</Source>
19+
</Sources>
20+
<Forms>
21+
<Form>动态控件.ui</Form>
22+
</Forms>
23+
<Translations/>
24+
<Resources/>
25+
<Interfaces/>
26+
<Others/>
27+
<Vcs>
28+
<VcsType>None</VcsType>
29+
</Vcs>
30+
<FiletypeAssociations>
31+
<FiletypeAssociation pattern="*.e4p" type="OTHERS"/>
32+
<FiletypeAssociation pattern="*.idl" type="INTERFACES"/>
33+
<FiletypeAssociation pattern="*.md" type="OTHERS"/>
34+
<FiletypeAssociation pattern="*.py" type="SOURCES"/>
35+
<FiletypeAssociation pattern="*.py3" type="SOURCES"/>
36+
<FiletypeAssociation pattern="*.pyw" type="SOURCES"/>
37+
<FiletypeAssociation pattern="*.pyw3" type="SOURCES"/>
38+
<FiletypeAssociation pattern="*.qm" type="TRANSLATIONS"/>
39+
<FiletypeAssociation pattern="*.qrc" type="RESOURCES"/>
40+
<FiletypeAssociation pattern="*.rst" type="OTHERS"/>
41+
<FiletypeAssociation pattern="*.ts" type="TRANSLATIONS"/>
42+
<FiletypeAssociation pattern="*.txt" type="OTHERS"/>
43+
<FiletypeAssociation pattern="*.ui" type="FORMS"/>
44+
<FiletypeAssociation pattern="README" type="OTHERS"/>
45+
<FiletypeAssociation pattern="README.*" type="OTHERS"/>
46+
</FiletypeAssociations>
47+
</Project>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
Module implementing Dialog.
5+
"""
6+
7+
from PyQt5 import QtGui, QtWidgets, QtCore, QtWinExtras
8+
from PyQt5.QtCore import *
9+
from PyQt5.QtGui import *
10+
from PyQt5.QtWidgets import *
11+
12+
from functools import partial
13+
from Ui_动态控件 import Ui_Dialog
14+
15+
class Dialog(QDialog, Ui_Dialog):
16+
17+
def __init__(self, parent=None):
18+
19+
super(Dialog, self).__init__(parent)
20+
self.setupUi(self)
21+
22+
self.dynamic1()
23+
24+
self.dynamic2()
25+
26+
# 法一
27+
def dynamic1(self):
28+
for i in range(5):
29+
self.pushButton = QtWidgets.QPushButton(self)
30+
self.pushButton.setText("pushButton%d"%i)
31+
self.pushButton.setObjectName("pushButton%d"%i)
32+
self.verticalLayout.addWidget(self.pushButton)
33+
self.pushButton.setContextMenuPolicy(Qt.CustomContextMenu)
34+
self.pushButton.customContextMenuRequested.connect(lambda:self.helpMenu(i))#右键请求,传入i实际上一直是4
35+
self.pushButton.clicked.connect(self.pr)
36+
37+
# 法二
38+
def dynamic2(self):
39+
for i in range(5, 8):
40+
txt="""
41+
self.pushButton_{i} = QtWidgets.QPushButton(self);
42+
self.pushButton_{i}.setText("pushButton{i}");
43+
self.pushButton_{i}.setObjectName("pushButton{i}");
44+
self.verticalLayout.addWidget(self.pushButton_{i});
45+
46+
self.pushButton_{i}.setContextMenuPolicy(Qt.CustomContextMenu)
47+
self.pushButton_{i}.customContextMenuRequested.connect(partial(self.helpMenu,i))#右键请求,用lambda会报错,partial需要import
48+
49+
self.pushButton_{i}.clicked.connect(self.pr)
50+
""".format(i=i)
51+
exec(txt)
52+
#只能法二可用的方式
53+
self.pushButton_5.clicked.connect(self.pr2)
54+
self.pushButton_6.clicked.connect(self.pr2)
55+
self.pushButton_7.clicked.connect(self.pr2)
56+
57+
58+
def helpMenu(self, *type):
59+
'''帮助按钮的右键菜单'''
60+
print(type)
61+
popMenu =QMenu()
62+
63+
if type[0]==5 or self.sender().text()== 'pushButton0':
64+
popMenu.addAction(u'关于',self.pr)
65+
popMenu.addSeparator()
66+
67+
elif type[0]==6 or self.sender().text()== 'pushButton1':
68+
popMenu.addAction(u'清理图片',self.pr)
69+
popMenu.addSeparator()
70+
71+
elif type[0]==7 or self.sender().text()== 'pushButton2':
72+
gitMenu=QMenu('同步到Git', popMenu)
73+
74+
self.pushAction=QAction(u'上传', self, triggered=lambda:self.pr)
75+
gitMenu.addAction(self.pushAction)
76+
77+
self.pullAction=QAction(u'下载', self, triggered=lambda:self.pr)
78+
gitMenu.addAction(self.pullAction)
79+
80+
gitMenu.addSeparator()
81+
gitMenu.addAction(u'配置仓库',lambda:self.pr)
82+
83+
popMenu.addAction(gitMenu.menuAction())#!!
84+
popMenu.exec_(QCursor.pos())#鼠标位置
85+
86+
def pr(self):
87+
'''法一和法二都可用的调用
88+
if self.sender().objectName=='XXX':
89+
self.pr2()
90+
'''
91+
print(self.sender().text())
92+
print(self.sender().objectName())
93+
print(self.pushButton.text())
94+
95+
def pr2(self):
96+
print(2)
97+
98+
if __name__ == "__main__":
99+
import sys
100+
app = QtWidgets.QApplication(sys.argv)
101+
ui = Dialog()
102+
ui.show()
103+
sys.exit(app.exec_())
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>Dialog</class>
4+
<widget class="QDialog" name="Dialog">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>370</width>
10+
<height>403</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>Dialog</string>
15+
</property>
16+
<property name="sizeGripEnabled">
17+
<bool>true</bool>
18+
</property>
19+
<layout class="QVBoxLayout" name="verticalLayout"/>
20+
</widget>
21+
<resources/>
22+
<connections/>
23+
</ui>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Form implementation generated from reading ui file 'D:\pyPro\dynamic_button\动态控件.ui'
4+
#
5+
# Created by: PyQt5 UI code generator 5.9.2
6+
#
7+
# WARNING! All changes made in this file will be lost!
8+
9+
from PyQt5 import QtCore, QtGui, QtWidgets
10+
11+
class Ui_Dialog(object):
12+
def setupUi(self, Dialog):
13+
Dialog.setObjectName("Dialog")
14+
Dialog.resize(370, 403)
15+
Dialog.setSizeGripEnabled(True)
16+
self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
17+
self.verticalLayout.setObjectName("verticalLayout")
18+
19+
self.retranslateUi(Dialog)
20+
QtCore.QMetaObject.connectSlotsByName(Dialog)
21+
22+
def retranslateUi(self, Dialog):
23+
_translate = QtCore.QCoreApplication.translate
24+
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
25+
26+
27+
if __name__ == "__main__":
28+
import sys
29+
app = QtWidgets.QApplication(sys.argv)
30+
Dialog = QtWidgets.QDialog()
31+
ui = Ui_Dialog()
32+
ui.setupUi(Dialog)
33+
Dialog.show()
34+
sys.exit(app.exec_())
35+

‎partner_625781186/1.exec动态生成控件/dynamic_button/__init__.py

Whitespace-only changes.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE Project SYSTEM "Project-5.1.dtd">
3+
<!-- eric project file for project dynamic_Controls -->
4+
<!-- Saved: 2018年04月11日, 02:00:19 -->
5+
<!-- Copyright (C) 2018 , -->
6+
<Project version="5.1">
7+
<Language>en_US</Language>
8+
<Hash>b35a15d37a7cb52779310396c238b2e70ac76888</Hash>
9+
<ProgLanguage mixed="0">Python3</ProgLanguage>
10+
<ProjectType>PyQt5</ProjectType>
11+
<Version>0.1</Version>
12+
<Author></Author>
13+
<Email></Email>
14+
<Eol index="0"/>
15+
<Sources>
16+
<Source>Ui_动态控件.py</Source>
17+
<Source>__init__.py</Source>
18+
<Source>动态控件.py</Source>
19+
</Sources>
20+
<Forms>
21+
<Form>动态控件.ui</Form>
22+
</Forms>
23+
<Translations/>
24+
<Resources/>
25+
<Interfaces/>
26+
<Others/>
27+
<Vcs>
28+
<VcsType>None</VcsType>
29+
</Vcs>
30+
<FiletypeAssociations>
31+
<FiletypeAssociation pattern="*.e4p" type="OTHERS"/>
32+
<FiletypeAssociation pattern="*.idl" type="INTERFACES"/>
33+
<FiletypeAssociation pattern="*.md" type="OTHERS"/>
34+
<FiletypeAssociation pattern="*.py" type="SOURCES"/>
35+
<FiletypeAssociation pattern="*.py3" type="SOURCES"/>
36+
<FiletypeAssociation pattern="*.pyw" type="SOURCES"/>
37+
<FiletypeAssociation pattern="*.pyw3" type="SOURCES"/>
38+
<FiletypeAssociation pattern="*.qm" type="TRANSLATIONS"/>
39+
<FiletypeAssociation pattern="*.qrc" type="RESOURCES"/>
40+
<FiletypeAssociation pattern="*.rst" type="OTHERS"/>
41+
<FiletypeAssociation pattern="*.ts" type="TRANSLATIONS"/>
42+
<FiletypeAssociation pattern="*.txt" type="OTHERS"/>
43+
<FiletypeAssociation pattern="*.ui" type="FORMS"/>
44+
<FiletypeAssociation pattern="README" type="OTHERS"/>
45+
<FiletypeAssociation pattern="README.*" type="OTHERS"/>
46+
</FiletypeAssociations>
47+
</Project>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
Module implementing Dialog.
5+
"""
6+
7+
from PyQt5 import QtGui, QtWidgets, QtCore, QtWinExtras
8+
from PyQt5.QtCore import *
9+
from PyQt5.QtGui import *
10+
from PyQt5.QtWidgets import *
11+
12+
from Ui_动态控件 import Ui_Dialog
13+
14+
15+
class Dialog(QDialog, Ui_Dialog):
16+
17+
def __init__(self, parent=None):
18+
19+
super(Dialog, self).__init__(parent)
20+
self.setupUi(self)
21+
self.dynamic1()
22+
self.dynamic2()
23+
24+
# 法一
25+
def dynamic1(self):
26+
for i in range(5):
27+
self.pushButton = QtWidgets.QPushButton(self)
28+
self.pushButton.setText("pushButton%d"%i)
29+
self.pushButton.setObjectName("pushButton%d"%i)
30+
self.verticalLayout.addWidget(self.pushButton)
31+
self.pushButton.clicked.connect(self.pr)
32+
# 法二
33+
def dynamic2(self):
34+
for i in range(4):
35+
txt="""
36+
self.pushButton_{i} = QtWidgets.QPushButton(self);
37+
self.pushButton_{i}.setText("pushButton{i}");
38+
self.pushButton_{i}.setObjectName("pushButton{i}");
39+
self.verticalLayout.addWidget(self.pushButton_{i});
40+
self.pushButton_{i}.clicked.connect(self.pr)
41+
""".format(i=i)
42+
exec(txt)
43+
#只能法二可用的方式
44+
self.pushButton_1.clicked.connect(self.pr2)
45+
self.pushButton_2.clicked.connect(self.pr2)
46+
self.pushButton_3.clicked.connect(self.pr2)
47+
48+
def pr(self):
49+
'''法一和法二都可用的调用
50+
if self.sender().objectName=='XXX':
51+
self.pr2()
52+
'''
53+
print(self.sender().text())
54+
print(self.sender().objectName())
55+
print(self.pushButton.text())
56+
57+
def pr2(self):
58+
print(2)
59+
60+
if __name__ == "__main__":
61+
import sys
62+
app = QtWidgets.QApplication(sys.argv)
63+
ui = Dialog()
64+
ui.show()
65+
sys.exit(app.exec_())

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /