鱼C论坛

 找回密码
 立即注册
查看: 53|回复: 5

求助,写好了界面,不懂怎么事先功能,求帮帮忙!

[复制链接]
发表于 7 天前 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
本帖最后由 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_())

以上是源代码,求各位大神棒棒忙?或者指导一下= =!!!我查了好久的代码,一直搞不懂,写进去就出错。。。不知道是逻辑有问题,还是怎么样!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 7 天前 | 显示全部楼层
回答问题前,我想下提几点建议,提问时,需要说清楚的几点东西:
1、你想实现什么功能,比如我要做一个自动生成工牌图片的界面程序。
2、你遇到的问题的具体描述,比如你想问程序为什么运行不起来:如果是运行了不报错,但是没有达到你预期的效果(对应代码逻辑错误),你需要把你预期的结果,和程序运行的结果说清;如果是运行之后报错(对应程序运行错误,语法错误等),你需要提供ide报错的完整文本。
3、贴代码时,尽量做到两点:(1)添加必要的注释(2)使用代码框
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 7 天前 | 显示全部楼层
本帖最后由 lightninng 于 2021-11-25 18:06 编辑

从你的代码里面,我看到的可能出问题的点,一个类定义中,第一个方法一定是__init__方法,借用https://www.runoob.com/python/python-object.html这个文档的内容,__init__方法被称为类的构造函数或初始化方法,当创建了这个类的实例时就会调用该方法,当你实例化一个类时,会首先自动执行__init__方法中的内容。
另外,建议你所有界面处理的部分,都放在setupUi方法中,如以这句代码(这也是使用代码框的示例):
  1. app.setWindowIcon(QIcon('./image/w1.png'))
复制代码

然后再__init__方法中调用setUi方法来生成界面,而不是把他放在主程序里面:

最后贴一段代码(使用PySide2编写,但是逻辑几乎完全一样)
  1. from PySide2 import QtCore, QtGui
  2. from PySide2.QtGui import QFont
  3. from PySide2.QtWidgets import QApplication, QWidget, QMessageBox, QFileDialog,QGridLayout,QCheckBox,QPushButton,QLabel,QSizePolicy,QTableWidget,QTableWidgetItem,QHeaderView
  4.     def __init__(self):
  5.         #初始化方法
  6.         super(MainWindow, self).__init__()

  7.         #调用setupUi方法生成界面
  8.         self.setupUi(self)

  9.         #处理其他逻辑
  10.         self.pushButton_2.setText("开始随机")
  11.               
  12.         self.pushButton_1.clicked.connect(self.import_file)
  13.         self.pushButton_2.clicked.connect(lambda:self.click_random(self.pushButton_2.text()))
  14.         self.pushButton_3.clicked.connect(self.output_file)
  15.         self.pushButton_4.clicked.connect(self.scroll_play)
  16.         self.pushButton_5.clicked.connect(self.show_position_window)
  17.   
  18.         self.tables = []
  19.         self.data=None
  20.         self.positions=[]

  21.         self.icon = QtGui.QIcon()
  22.         self.icon.addPixmap(QtGui.QPixmap(":main.ico"), QtGui.QIcon.Normal,QtGui.QIcon.Off)
  23.         self.setWindowIcon(self.icon)
  24.         
  25.         self.random_thread = myThread()
  26.         self.stop_singnal.connect(self.random_thread.stop)
  27.         self.random_thread.shuffle_signal.connect(self.shuffle)
  28.         self.random_thread.button_text_signal.connect(self.button_text_build)

  29.         self.spinBox_1.valueChanged.connect(lambda:(self.init_tables(),self.click_random(self.pushButton_2.text(),True)))
  30.         self.spinBox_2.valueChanged.connect(lambda:(self.init_tables(),self.click_random(self.pushButton_2.text(),True)))
  31.         self.spinBox_3.valueChanged.connect(lambda:(self.init_tables(),self.click_random(self.pushButton_2.text(),True)))
  32.         self.checkBox_1.clicked.connect(lambda:(self.init_tables(),self.click_random(self.pushButton_2.text(),True)))
  33.         self.checkBox_2.clicked.connect(self.position_display_changed)
  34.                
  35.         self.init_tables()

  36. def setupUi(self, Form):
  37.         # setupUi 方法用来创建和界面构成相关的代码
  38.         if not Form.objectName():
  39.             Form.setObjectName(u"Form")
  40.         Form.resize(742, 489)
  41.         self.horizontalLayout = QHBoxLayout(Form)
  42.         self.horizontalLayout.setObjectName(u"horizontalLayout")
  43.         self.verticalLayout = QVBoxLayout()
  44.         self.verticalLayout.setObjectName(u"verticalLayout")
  45.         self.groupBox = QGroupBox(Form)
  46.         self.groupBox.setObjectName(u"groupBox")
  47.         self.verticalLayout_2 = QVBoxLayout(self.groupBox)
  48.         self.verticalLayout_2.setObjectName(u"verticalLayout_2")
  49.         self.horizontalLayout_7 = QHBoxLayout()
  50.         self.horizontalLayout_7.setObjectName(u"horizontalLayout_7")
  51.         self.label_5 = QLabel(self.groupBox)
  52.         self.label_5.setObjectName(u"label_5")

  53.         self.horizontalLayout_7.addWidget(self.label_5)

  54.         self.spinBox_5 = QSpinBox(self.groupBox)
  55.         self.spinBox_5.setObjectName(u"spinBox_5")
  56.         self.spinBox_5.setMinimum(15)
  57.         self.spinBox_5.setMaximum(30)
  58.         self.spinBox_5.setValue(25)

  59.         self.horizontalLayout_7.addWidget(self.spinBox_5)


  60.         self.verticalLayout_2.addLayout(self.horizontalLayout_7)


  61.         self.verticalLayout.addWidget(self.groupBox)

  62.         self.pushButton_1 = QPushButton(Form)
  63.         self.pushButton_1.setObjectName(u"pushButton_1")

  64.         self.verticalLayout.addWidget(self.pushButton_1)

  65.         self.pushButton_5 = QPushButton(Form)
  66.         self.pushButton_5.setObjectName(u"pushButton_5")

  67.         self.verticalLayout.addWidget(self.pushButton_5)

  68.         self.pushButton_2 = QPushButton(Form)
  69.         self.pushButton_2.setObjectName(u"pushButton_2")
  70.         self.pushButton_2.setMinimumSize(QSize(250, 0))

  71.         self.verticalLayout.addWidget(self.pushButton_2)

  72.         self.pushButton_3 = QPushButton(Form)
  73.         self.pushButton_3.setObjectName(u"pushButton_3")

  74.         self.verticalLayout.addWidget(self.pushButton_3)

  75.         self.pushButton_4 = QPushButton(Form)
  76.         self.pushButton_4.setObjectName(u"pushButton_4")

  77.         self.verticalLayout.addWidget(self.pushButton_4)

  78.         self.groupBox_2 = QGroupBox(Form)
  79.         self.groupBox_2.setObjectName(u"groupBox_2")
  80.         self.verticalLayout_3 = QVBoxLayout(self.groupBox_2)
  81.         self.verticalLayout_3.setObjectName(u"verticalLayout_3")
  82.         self.horizontalLayout_2 = QHBoxLayout()
  83.         self.horizontalLayout_2.setObjectName(u"horizontalLayout_2")
  84.         self.label = QLabel(self.groupBox_2)
  85.         self.label.setObjectName(u"label")
  86.         sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Preferred)
  87.         sizePolicy.setHorizontalStretch(0)
  88.         sizePolicy.setVerticalStretch(0)
  89.         sizePolicy.setHeightForWidth(self.label.sizePolicy().hasHeightForWidth())
  90.         self.label.setSizePolicy(sizePolicy)
  91.         self.label.setAlignment(Qt.AlignRight|Qt.AlignTrailing|Qt.AlignVCenter)

  92.         self.horizontalLayout_2.addWidget(self.label)

  93.         self.spinBox_1 = QSpinBox(self.groupBox_2)
  94.         self.spinBox_1.setObjectName(u"spinBox_1")
  95.         self.spinBox_1.setMaximum(1000)
  96.         self.spinBox_1.setValue(90)

  97.         self.horizontalLayout_2.addWidget(self.spinBox_1)


  98.         self.verticalLayout_3.addLayout(self.horizontalLayout_2)

  99.         self.horizontalLayout_4 = QHBoxLayout()
  100.         self.horizontalLayout_4.setObjectName(u"horizontalLayout_4")
  101.         self.label_3 = QLabel(self.groupBox_2)
  102.         self.label_3.setObjectName(u"label_3")
  103.         sizePolicy.setHeightForWidth(self.label_3.sizePolicy().hasHeightForWidth())
  104.         self.label_3.setSizePolicy(sizePolicy)
  105.         self.label_3.setAlignment(Qt.AlignRight|Qt.AlignTrailing|Qt.AlignVCenter)

  106.         self.horizontalLayout_4.addWidget(self.label_3)

  107.         self.spinBox_2 = QSpinBox(self.groupBox_2)
  108.         self.spinBox_2.setObjectName(u"spinBox_2")
  109.         self.spinBox_2.setValue(2)

  110.         self.horizontalLayout_4.addWidget(self.spinBox_2)


  111.         self.verticalLayout_3.addLayout(self.horizontalLayout_4)

  112.         self.horizontalLayout_3 = QHBoxLayout()
  113.         self.horizontalLayout_3.setObjectName(u"horizontalLayout_3")
  114.         self.label_2 = QLabel(self.groupBox_2)
  115.         self.label_2.setObjectName(u"label_2")
  116.         sizePolicy.setHeightForWidth(self.label_2.sizePolicy().hasHeightForWidth())
  117.         self.label_2.setSizePolicy(sizePolicy)
  118.         self.label_2.setAlignment(Qt.AlignRight|Qt.AlignTrailing|Qt.AlignVCenter)

  119.         self.horizontalLayout_3.addWidget(self.label_2)

  120.         self.spinBox_3 = QSpinBox(self.groupBox_2)
  121.         self.spinBox_3.setObjectName(u"spinBox_3")
  122.         self.spinBox_3.setValue(10)

  123.         self.horizontalLayout_3.addWidget(self.spinBox_3)


  124.         self.verticalLayout_3.addLayout(self.horizontalLayout_3)

  125.         self.horizontalLayout_8 = QHBoxLayout()
  126.         self.horizontalLayout_8.setObjectName(u"horizontalLayout_8")
  127.         self.label_6 = QLabel(self.groupBox_2)
  128.         self.label_6.setObjectName(u"label_6")
  129.         sizePolicy.setHeightForWidth(self.label_6.sizePolicy().hasHeightForWidth())
  130.         self.label_6.setSizePolicy(sizePolicy)
  131.         self.label_6.setAlignment(Qt.AlignRight|Qt.AlignTrailing|Qt.AlignVCenter)

  132.         self.horizontalLayout_8.addWidget(self.label_6)

  133.         self.checkBox_1 = QCheckBox(self.groupBox_2)
  134.         self.checkBox_1.setObjectName(u"checkBox_1")

  135.         self.horizontalLayout_8.addWidget(self.checkBox_1)


  136.         self.verticalLayout_3.addLayout(self.horizontalLayout_8)

  137.         self.horizontalLayout_9 = QHBoxLayout()
  138.         self.horizontalLayout_9.setObjectName(u"horizontalLayout_9")
  139.         self.label_7 = QLabel(self.groupBox_2)
  140.         self.label_7.setObjectName(u"label_7")
  141.         sizePolicy.setHeightForWidth(self.label_7.sizePolicy().hasHeightForWidth())
  142.         self.label_7.setSizePolicy(sizePolicy)
  143.         self.label_7.setAlignment(Qt.AlignRight|Qt.AlignTrailing|Qt.AlignVCenter)

  144.         self.horizontalLayout_9.addWidget(self.label_7)

  145.         self.checkBox_2 = QCheckBox(self.groupBox_2)
  146.         self.checkBox_2.setObjectName(u"checkBox_2")
  147.         self.checkBox_2.setEnabled(False)

  148.         self.horizontalLayout_9.addWidget(self.checkBox_2)


  149.         self.verticalLayout_3.addLayout(self.horizontalLayout_9)

  150.         self.horizontalLayout_6 = QHBoxLayout()
  151.         self.horizontalLayout_6.setObjectName(u"horizontalLayout_6")
  152.         self.label_8 = QLabel(self.groupBox_2)
  153.         self.label_8.setObjectName(u"label_8")
  154.         sizePolicy1 = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
  155.         sizePolicy1.setHorizontalStretch(0)
  156.         sizePolicy1.setVerticalStretch(0)
  157.         sizePolicy1.setHeightForWidth(self.label_8.sizePolicy().hasHeightForWidth())
  158.         self.label_8.setSizePolicy(sizePolicy1)
  159.         self.label_8.setMaximumSize(QSize(16777215, 16777215))
  160.         self.label_8.setAlignment(Qt.AlignRight|Qt.AlignTop|Qt.AlignTrailing)
  161.         self.label_8.setWordWrap(False)
  162.         self.label_8.setMargin(0)

  163.         self.horizontalLayout_6.addWidget(self.label_8)

  164.         self.label_4 = QLabel(self.groupBox_2)
  165.         self.label_4.setObjectName(u"label_4")
  166.         self.label_4.setWordWrap(True)
  167.         self.label_4.setMargin(0)

  168.         self.horizontalLayout_6.addWidget(self.label_4)


  169.         self.verticalLayout_3.addLayout(self.horizontalLayout_6)


  170.         self.verticalLayout.addWidget(self.groupBox_2)

  171.         self.verticalSpacer = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding)

  172.         self.verticalLayout.addItem(self.verticalSpacer)


  173.         self.horizontalLayout.addLayout(self.verticalLayout)

  174.         self.horizontalLayout_5 = QHBoxLayout()
  175.         self.horizontalLayout_5.setObjectName(u"horizontalLayout_5")
  176.         self.tableWidget_1 = QTableWidget(Form)
  177.         self.tableWidget_1.setObjectName(u"tableWidget_1")

  178.         self.horizontalLayout_5.addWidget(self.tableWidget_1)

  179.         self.tableWidget_2 = QTableWidget(Form)
  180.         self.tableWidget_2.setObjectName(u"tableWidget_2")

  181.         self.horizontalLayout_5.addWidget(self.tableWidget_2)

  182.         self.tableWidget_3 = QTableWidget(Form)
  183.         self.tableWidget_3.setObjectName(u"tableWidget_3")

  184.         self.horizontalLayout_5.addWidget(self.tableWidget_3)

  185.         self.tableWidget_4 = QTableWidget(Form)
  186.         self.tableWidget_4.setObjectName(u"tableWidget_4")

  187.         self.horizontalLayout_5.addWidget(self.tableWidget_4)


  188.         self.horizontalLayout.addLayout(self.horizontalLayout_5)


  189.         self.retranslateUi(Form)

  190.         QMetaObject.connectSlotsByName(Form)
复制代码


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 7 天前 | 显示全部楼层
lightninng 发表于 2021-11-25 17:52
回答问题前,我想下提几点建议,提问时,需要说清楚的几点东西:
1、你想实现什么功能,比如我要做一个自 ...

因为图片发不出来,只能口述了,我想实现绑定pushbutton_3和pushbutton_4绑定到打印机本地打印机的功能。
    pushbutton和pushbutton_2显示图片到label
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 7 天前 | 显示全部楼层
adds52339 发表于 2021-11-25 18:05
因为图片发不出来,只能口述了,我想实现绑定pushbutton_3和pushbutton_4绑定到打印机本地打印机的功能。 ...

你能仔细看一下我说的内容嘛
你的功能说清了,问题呢,问题的具体表现说清了么,是程序报错,还是程序没有按照你想的方式运行?
如果是程序报错,那完整的报错文本呢?
如果程序没有报错,但是没有按照你想的方式运行,那程序运行是什么样的,你想的是什么样的?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 6 天前 | 显示全部楼层
lightninng 发表于 2021-11-25 18:08
你能仔细看一下我说的内容嘛
你的功能说清了,问题呢,问题的具体表现说清了么,是程序报错,还是程序没 ...

是我没说清楚= =!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1

GMT+8, 2021-12-2 00:11

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表