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 d631cb1

Browse files
动态控件基础例子 - 动态生成按钮
1 parent 73376dc commit d631cb1

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed
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_())
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>

0 commit comments

Comments
(0)

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