求助,写好了界面,不懂怎么事先功能,求帮帮忙!
本帖最后由 adds52339 于 2021-11-25 17:38 编辑import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtGui import QIcon
from PyQt5.QtPrintSupport import *
from PyQt5.QtCore import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(946, 655)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(670, 330, 141, 51))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(670, 390, 141, 51))
self.pushButton_2.setObjectName("pushButton_2")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(190, 40, 91, 31))
self.label.setStyleSheet("font: 25 16pt \"等线 Light\";")
self.label.setObjectName("label")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(30, 80, 561, 451))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.textBrowser = QtWidgets.QTextBrowser(self.verticalLayoutWidget)
self.textBrowser.setObjectName("textBrowser")
self.verticalLayout.addWidget(self.textBrowser)
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(640, 80, 221, 41))
self.lineEdit.setStyleSheet("font: 14pt \"楷体\";")
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(640, 130, 221, 41))
self.lineEdit_2.setStyleSheet("font: 14pt \"黑体\";")
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(640, 180, 221, 41))
self.lineEdit_3.setStyleSheet("font: 14pt \"黑体\";")
self.lineEdit_3.setObjectName("lineEdit_3")
self.lineEdit_4 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_4.setGeometry(QtCore.QRect(640, 230, 221, 41))
self.lineEdit_4.setStyleSheet("font: 14pt \"黑体\";")
self.lineEdit_4.setObjectName("lineEdit_4")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(450, 540, 121, 31))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(320, 540, 121, 31))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_5.setGeometry(QtCore.QRect(670, 510, 141, 41))
self.pushButton_5.setObjectName("pushButton_5")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(640, 40, 121, 31))
self.label_2.setStyleSheet("font: 25 16pt \"等线 Light\";")
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(640, 290, 221, 31))
self.label_3.setStyleSheet("font: 25 16pt \"等线 Light\";")
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(650, 470, 221, 31))
self.label_4.setStyleSheet("font: 25 16pt \"等线 Light\";")
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(30, 580, 581, 31))
self.label_5.setStyleSheet("font: 25 16pt \"等线 Light\";")
self.label_5.setObjectName("label_5")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
# self.pushButton.clicked.connect(QPixmap('./image/gongpai.png'))
# self.pushButton_4.clicked.connect(self.showpushButton_4dailog)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "工牌打印"))
self.pushButton.setText(_translate("MainWindow", "单个横版工牌"))
self.pushButton_2.setText(_translate("MainWindow", "单个竖版工牌"))
self.label.setText(_translate("MainWindow", "预览图片"))
self.lineEdit.setText(_translate("MainWindow", "姓名"))
self.lineEdit_2.setText(_translate("MainWindow", "部门"))
self.lineEdit_3.setText(_translate("MainWindow", "职务"))
self.lineEdit_4.setText(_translate("MainWindow", "工号"))
self.pushButton_3.setText(_translate("MainWindow", "打印工牌"))
self.pushButton_4.setText(_translate("MainWindow", "打印设置"))
self.pushButton_5.setText(_translate("MainWindow", "合成工牌"))
self.label_2.setText(_translate("MainWindow", "第一步输入"))
self.label_3.setText(_translate("MainWindow", "第二部选择横版或竖版"))
self.label_4.setText(_translate("MainWindow", "第三部合成工牌"))
self.label_5.setText(_translate("MainWindow", "第四部点击打印设置设置版面大小后,点击打印工牌完成打印"))
# def showpushButton_4dailog(self):
# pushButton_4dailog.clicked.connect(self.pushButton_4,self)
# pushButton_4dailog.exec()
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setWindowIcon(QIcon('./image/w1.png'))
MainWindow = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
以上是源代码,求各位大神棒棒忙?或者指导一下= =!!!我查了好久的代码,一直搞不懂,写进去就出错。。。不知道是逻辑有问题,还是怎么样! 回答问题前,我想下提几点建议,提问时,需要说清楚的几点东西:
1、你想实现什么功能,比如我要做一个自动生成工牌图片的界面程序。
2、你遇到的问题的具体描述,比如你想问程序为什么运行不起来:如果是运行了不报错,但是没有达到你预期的效果(对应代码逻辑错误),你需要把你预期的结果,和程序运行的结果说清;如果是运行之后报错(对应程序运行错误,语法错误等),你需要提供ide报错的完整文本。
3、贴代码时,尽量做到两点:(1)添加必要的注释(2)使用代码框
本帖最后由 lightninng 于 2021-11-25 18:06 编辑
从你的代码里面,我看到的可能出问题的点,一个类定义中,第一个方法一定是__init__方法,借用https://www.runoob.com/python/python-object.html这个文档的内容,__init__方法被称为类的构造函数或初始化方法,当创建了这个类的实例时就会调用该方法,当你实例化一个类时,会首先自动执行__init__方法中的内容。
另外,建议你所有界面处理的部分,都放在setupUi方法中,如以这句代码(这也是使用代码框的示例):
app.setWindowIcon(QIcon('./image/w1.png'))
然后再__init__方法中调用setUi方法来生成界面,而不是把他放在主程序里面:
最后贴一段代码(使用PySide2编写,但是逻辑几乎完全一样)
from PySide2 import QtCore, QtGui
from PySide2.QtGui import QFont
from PySide2.QtWidgets import QApplication, QWidget, QMessageBox, QFileDialog,QGridLayout,QCheckBox,QPushButton,QLabel,QSizePolicy,QTableWidget,QTableWidgetItem,QHeaderView
def __init__(self):
#初始化方法
super(MainWindow, self).__init__()
#调用setupUi方法生成界面
self.setupUi(self)
#处理其他逻辑
self.pushButton_2.setText("开始随机")
self.pushButton_1.clicked.connect(self.import_file)
self.pushButton_2.clicked.connect(lambda:self.click_random(self.pushButton_2.text()))
self.pushButton_3.clicked.connect(self.output_file)
self.pushButton_4.clicked.connect(self.scroll_play)
self.pushButton_5.clicked.connect(self.show_position_window)
self.tables = []
self.data=None
self.positions=[]
self.icon = QtGui.QIcon()
self.icon.addPixmap(QtGui.QPixmap(":main.ico"), QtGui.QIcon.Normal,QtGui.QIcon.Off)
self.setWindowIcon(self.icon)
self.random_thread = myThread()
self.stop_singnal.connect(self.random_thread.stop)
self.random_thread.shuffle_signal.connect(self.shuffle)
self.random_thread.button_text_signal.connect(self.button_text_build)
self.spinBox_1.valueChanged.connect(lambda:(self.init_tables(),self.click_random(self.pushButton_2.text(),True)))
self.spinBox_2.valueChanged.connect(lambda:(self.init_tables(),self.click_random(self.pushButton_2.text(),True)))
self.spinBox_3.valueChanged.connect(lambda:(self.init_tables(),self.click_random(self.pushButton_2.text(),True)))
self.checkBox_1.clicked.connect(lambda:(self.init_tables(),self.click_random(self.pushButton_2.text(),True)))
self.checkBox_2.clicked.connect(self.position_display_changed)
self.init_tables()
def setupUi(self, Form):
# setupUi 方法用来创建和界面构成相关的代码
if not Form.objectName():
Form.setObjectName(u"Form")
Form.resize(742, 489)
self.horizontalLayout = QHBoxLayout(Form)
self.horizontalLayout.setObjectName(u"horizontalLayout")
self.verticalLayout = QVBoxLayout()
self.verticalLayout.setObjectName(u"verticalLayout")
self.groupBox = QGroupBox(Form)
self.groupBox.setObjectName(u"groupBox")
self.verticalLayout_2 = QVBoxLayout(self.groupBox)
self.verticalLayout_2.setObjectName(u"verticalLayout_2")
self.horizontalLayout_7 = QHBoxLayout()
self.horizontalLayout_7.setObjectName(u"horizontalLayout_7")
self.label_5 = QLabel(self.groupBox)
self.label_5.setObjectName(u"label_5")
self.horizontalLayout_7.addWidget(self.label_5)
self.spinBox_5 = QSpinBox(self.groupBox)
self.spinBox_5.setObjectName(u"spinBox_5")
self.spinBox_5.setMinimum(15)
self.spinBox_5.setMaximum(30)
self.spinBox_5.setValue(25)
self.horizontalLayout_7.addWidget(self.spinBox_5)
self.verticalLayout_2.addLayout(self.horizontalLayout_7)
self.verticalLayout.addWidget(self.groupBox)
self.pushButton_1 = QPushButton(Form)
self.pushButton_1.setObjectName(u"pushButton_1")
self.verticalLayout.addWidget(self.pushButton_1)
self.pushButton_5 = QPushButton(Form)
self.pushButton_5.setObjectName(u"pushButton_5")
self.verticalLayout.addWidget(self.pushButton_5)
self.pushButton_2 = QPushButton(Form)
self.pushButton_2.setObjectName(u"pushButton_2")
self.pushButton_2.setMinimumSize(QSize(250, 0))
self.verticalLayout.addWidget(self.pushButton_2)
self.pushButton_3 = QPushButton(Form)
self.pushButton_3.setObjectName(u"pushButton_3")
self.verticalLayout.addWidget(self.pushButton_3)
self.pushButton_4 = QPushButton(Form)
self.pushButton_4.setObjectName(u"pushButton_4")
self.verticalLayout.addWidget(self.pushButton_4)
self.groupBox_2 = QGroupBox(Form)
self.groupBox_2.setObjectName(u"groupBox_2")
self.verticalLayout_3 = QVBoxLayout(self.groupBox_2)
self.verticalLayout_3.setObjectName(u"verticalLayout_3")
self.horizontalLayout_2 = QHBoxLayout()
self.horizontalLayout_2.setObjectName(u"horizontalLayout_2")
self.label = QLabel(self.groupBox_2)
self.label.setObjectName(u"label")
sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label.sizePolicy().hasHeightForWidth())
self.label.setSizePolicy(sizePolicy)
self.label.setAlignment(Qt.AlignRight|Qt.AlignTrailing|Qt.AlignVCenter)
self.horizontalLayout_2.addWidget(self.label)
self.spinBox_1 = QSpinBox(self.groupBox_2)
self.spinBox_1.setObjectName(u"spinBox_1")
self.spinBox_1.setMaximum(1000)
self.spinBox_1.setValue(90)
self.horizontalLayout_2.addWidget(self.spinBox_1)
self.verticalLayout_3.addLayout(self.horizontalLayout_2)
self.horizontalLayout_4 = QHBoxLayout()
self.horizontalLayout_4.setObjectName(u"horizontalLayout_4")
self.label_3 = QLabel(self.groupBox_2)
self.label_3.setObjectName(u"label_3")
sizePolicy.setHeightForWidth(self.label_3.sizePolicy().hasHeightForWidth())
self.label_3.setSizePolicy(sizePolicy)
self.label_3.setAlignment(Qt.AlignRight|Qt.AlignTrailing|Qt.AlignVCenter)
self.horizontalLayout_4.addWidget(self.label_3)
self.spinBox_2 = QSpinBox(self.groupBox_2)
self.spinBox_2.setObjectName(u"spinBox_2")
self.spinBox_2.setValue(2)
self.horizontalLayout_4.addWidget(self.spinBox_2)
self.verticalLayout_3.addLayout(self.horizontalLayout_4)
self.horizontalLayout_3 = QHBoxLayout()
self.horizontalLayout_3.setObjectName(u"horizontalLayout_3")
self.label_2 = QLabel(self.groupBox_2)
self.label_2.setObjectName(u"label_2")
sizePolicy.setHeightForWidth(self.label_2.sizePolicy().hasHeightForWidth())
self.label_2.setSizePolicy(sizePolicy)
self.label_2.setAlignment(Qt.AlignRight|Qt.AlignTrailing|Qt.AlignVCenter)
self.horizontalLayout_3.addWidget(self.label_2)
self.spinBox_3 = QSpinBox(self.groupBox_2)
self.spinBox_3.setObjectName(u"spinBox_3")
self.spinBox_3.setValue(10)
self.horizontalLayout_3.addWidget(self.spinBox_3)
self.verticalLayout_3.addLayout(self.horizontalLayout_3)
self.horizontalLayout_8 = QHBoxLayout()
self.horizontalLayout_8.setObjectName(u"horizontalLayout_8")
self.label_6 = QLabel(self.groupBox_2)
self.label_6.setObjectName(u"label_6")
sizePolicy.setHeightForWidth(self.label_6.sizePolicy().hasHeightForWidth())
self.label_6.setSizePolicy(sizePolicy)
self.label_6.setAlignment(Qt.AlignRight|Qt.AlignTrailing|Qt.AlignVCenter)
self.horizontalLayout_8.addWidget(self.label_6)
self.checkBox_1 = QCheckBox(self.groupBox_2)
self.checkBox_1.setObjectName(u"checkBox_1")
self.horizontalLayout_8.addWidget(self.checkBox_1)
self.verticalLayout_3.addLayout(self.horizontalLayout_8)
self.horizontalLayout_9 = QHBoxLayout()
self.horizontalLayout_9.setObjectName(u"horizontalLayout_9")
self.label_7 = QLabel(self.groupBox_2)
self.label_7.setObjectName(u"label_7")
sizePolicy.setHeightForWidth(self.label_7.sizePolicy().hasHeightForWidth())
self.label_7.setSizePolicy(sizePolicy)
self.label_7.setAlignment(Qt.AlignRight|Qt.AlignTrailing|Qt.AlignVCenter)
self.horizontalLayout_9.addWidget(self.label_7)
self.checkBox_2 = QCheckBox(self.groupBox_2)
self.checkBox_2.setObjectName(u"checkBox_2")
self.checkBox_2.setEnabled(False)
self.horizontalLayout_9.addWidget(self.checkBox_2)
self.verticalLayout_3.addLayout(self.horizontalLayout_9)
self.horizontalLayout_6 = QHBoxLayout()
self.horizontalLayout_6.setObjectName(u"horizontalLayout_6")
self.label_8 = QLabel(self.groupBox_2)
self.label_8.setObjectName(u"label_8")
sizePolicy1 = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
sizePolicy1.setHorizontalStretch(0)
sizePolicy1.setVerticalStretch(0)
sizePolicy1.setHeightForWidth(self.label_8.sizePolicy().hasHeightForWidth())
self.label_8.setSizePolicy(sizePolicy1)
self.label_8.setMaximumSize(QSize(16777215, 16777215))
self.label_8.setAlignment(Qt.AlignRight|Qt.AlignTop|Qt.AlignTrailing)
self.label_8.setWordWrap(False)
self.label_8.setMargin(0)
self.horizontalLayout_6.addWidget(self.label_8)
self.label_4 = QLabel(self.groupBox_2)
self.label_4.setObjectName(u"label_4")
self.label_4.setWordWrap(True)
self.label_4.setMargin(0)
self.horizontalLayout_6.addWidget(self.label_4)
self.verticalLayout_3.addLayout(self.horizontalLayout_6)
self.verticalLayout.addWidget(self.groupBox_2)
self.verticalSpacer = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding)
self.verticalLayout.addItem(self.verticalSpacer)
self.horizontalLayout.addLayout(self.verticalLayout)
self.horizontalLayout_5 = QHBoxLayout()
self.horizontalLayout_5.setObjectName(u"horizontalLayout_5")
self.tableWidget_1 = QTableWidget(Form)
self.tableWidget_1.setObjectName(u"tableWidget_1")
self.horizontalLayout_5.addWidget(self.tableWidget_1)
self.tableWidget_2 = QTableWidget(Form)
self.tableWidget_2.setObjectName(u"tableWidget_2")
self.horizontalLayout_5.addWidget(self.tableWidget_2)
self.tableWidget_3 = QTableWidget(Form)
self.tableWidget_3.setObjectName(u"tableWidget_3")
self.horizontalLayout_5.addWidget(self.tableWidget_3)
self.tableWidget_4 = QTableWidget(Form)
self.tableWidget_4.setObjectName(u"tableWidget_4")
self.horizontalLayout_5.addWidget(self.tableWidget_4)
self.horizontalLayout.addLayout(self.horizontalLayout_5)
self.retranslateUi(Form)
QMetaObject.connectSlotsByName(Form)
lightninng 发表于 2021-11-25 17:52
回答问题前,我想下提几点建议,提问时,需要说清楚的几点东西:
1、你想实现什么功能,比如我要做一个自 ...
因为图片发不出来,只能口述了,我想实现绑定pushbutton_3和pushbutton_4绑定到打印机本地打印机的功能。
pushbutton和pushbutton_2显示图片到label adds52339 发表于 2021-11-25 18:05
因为图片发不出来,只能口述了,我想实现绑定pushbutton_3和pushbutton_4绑定到打印机本地打印机的功能。 ...
你能仔细看一下我说的内容嘛
你的功能说清了,问题呢,问题的具体表现说清了么,是程序报错,还是程序没有按照你想的方式运行?
如果是程序报错,那完整的报错文本呢?
如果程序没有报错,但是没有按照你想的方式运行,那程序运行是什么样的,你想的是什么样的? lightninng 发表于 2021-11-25 18:08
你能仔细看一下我说的内容嘛
你的功能说清了,问题呢,问题的具体表现说清了么,是程序报错,还是程序没 ...
是我没说清楚= =!!!
页:
[1]