更新pyQT5中的重绘主窗口

时间:2017-07-21 09:34:32

标签: python-3.x qt pyqt5

我试图在动作后重新绘制屏幕。

基本上我正在显示要打印的标签列表,打印标签或单击刷新按钮时,应删除所有子窗口小部件,数据库查询将引入新的窗口小部件。

这是我的代码,我正在努力使用def刷新功能,我是qt和使用python3的新手

from functools import partial

import cups
from PyQt5 import QtCore, QtWidgets

import barcode
from barcode.writer import ImageWriter
import sql


class Ui_MainWindow(object):
    def __init__(self):
        self.centralwidget = QtWidgets.QWidget(MainWindow)

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1024, 600)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(910, 10, 90, 28))
        self.pushButton.setObjectName("pushButton")

        self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
        self.scrollArea.setGeometry(QtCore.QRect(0, 50, 1027, 541))
        self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.scrollArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.scrollArea.setWidgetResizable(False)
        self.scrollArea.setObjectName("scrollArea")
        self.scrollAreaWidgetContents = QtWidgets.QWidget()

        rows = len(sql.get_prepacks()) / 5
        self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 1027, rows * 280))
        self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")

        self.scrollArea.setWidget(self.scrollAreaWidgetContents)
        MainWindow.setCentralWidget(self.centralwidget)


        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Prepack Print Station"))
        self.pushButton.setText(_translate("MainWindow", "Reload"))
        self.pushButton.clicked.connect(self.on_click)

        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        self.show_cartons()

    def on_click(self):
        self.refresh()


    def show_cartons(self):
        i = 0
        z = 20
        y = 20

        pre_packs = sql.get_prepacks()
        for p in pre_packs:

            pz = 35
            py = 30

            # Box
            self.frame = QtWidgets.QFrame(self.scrollAreaWidgetContents)
            self.frame.setGeometry(QtCore.QRect(z, y, 150, 200))
            self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
            self.frame.setFrameShadow(QtWidgets.QFrame.Raised)

            # Prepack ID
            self.label = QtWidgets.QLabel(self.frame)
            self.label.setGeometry(QtCore.QRect(pz, py, 100, 20))
            self.label.setText(str(p[0]))

            py = py + 40

            # Altcode

            self.label_2 = QtWidgets.QLabel(self.frame)
            self.label_2.setGeometry(QtCore.QRect(pz, py, 100, 20))
            self.label_2.setText(str(p[1]))

            py = py + 40

            # Colour

            self.label_3 = QtWidgets.QLabel(self.frame)
            self.label_3.setGeometry(QtCore.QRect(pz, py, 100, 20))
            self.label_3.setText(str(p[2]))

            py = py + 40

            # Button

            self.p_button = QtWidgets.QPushButton(self.frame)
            self.p_button.setGeometry(QtCore.QRect(10, py, 130, 40))
            self.p_button.setText("Print " + str(p[3]) + " Labels")
            self.p_button.clicked.connect(partial(self.print_clicked, str(p[0])))

            z = z + 180
            i = i + 1

            if i == 5:
                z = 20
                i = 0
                y = y + 230

    def print_clicked(self, pp):
        labels = sql.get_labelsforprepack(pp)
        conn = cups.Connection()
        totallabels = len(labels)
        alloccode = labels[0][1]
        bcode = barcode.get('code39', str(alloccode), writer=ImageWriter())
        filename = bcode.save('bcode')
        conn.printFile("USB", filename, "Test", {"copies":  str(totallabels)})

        sql.insert_print_record(alloccode)

        for l in labels:
            sql.insert_carton(str(l[0]), str(l[1]))

        self.refresh()

    def refresh(self):
        MainWindow = QtWidgets.QMainWindow()
        ui = Ui_MainWindow()
        ui.setupUi(MainWindow)
        MainWindow.repaint()


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

更新 - >我更接近这个:

def refresh(self):

    for i in self.scrollAreaWidgetContents.children():
        sip.delete(i)

    self.show_cartons()
    self.scrollAreaWidgetContents.repaint()

现在删除了帧,但是调用show_cartons()没有读取它们,添加.repaint也无法正常工作

1 个答案:

答案 0 :(得分:0)

这就是我最终做到的方式

import os
from functools import partial

import cups

import sip

import time
from PyQt5 import QtCore, QtWidgets

import barcode
from barcode.writer import ImageWriter
import sql


class Ui_MainWindow(object):

    def setupBasics(self, MainWindow):

        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1024, 600)
        MainWindow.setWindowTitle("Prepack Print Station")

        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(910, 10, 90, 28))
        self.pushButton.setObjectName("pushButton")
        self.pushButton.setText("Reload")
        self.pushButton.clicked.connect(self.on_click)

        self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
        self.scrollArea.setGeometry(QtCore.QRect(0, 50, 1027, 541))
        self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.scrollArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.scrollArea.setWidgetResizable(False)
        self.scrollArea.setObjectName("scrollArea")

        MainWindow.setCentralWidget(self.centralwidget)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        self.setupDynamics()



    def setupDynamics(self):

        self.scrollAreaWidgetContents = QtWidgets.QWidget()

        rows = len(sql.get_prepacks()) / 5
        self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 1027, rows * 280))
        self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")



        self.show_cartons()

    def on_click(self):
        self.refresh()

    def show_cartons(self):
        i = 0
        z = 20
        y = 20

        pre_packs = sql.get_prepacks()
        for p in pre_packs:

            pz = 35
            py = 30

            # Box
            self.frame = QtWidgets.QFrame(self.scrollAreaWidgetContents)
            self.frame.setGeometry(QtCore.QRect(z, y, 150, 200))
            self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
            self.frame.setFrameShadow(QtWidgets.QFrame.Raised)

            # Prepack ID
            self.label = QtWidgets.QLabel(self.frame)
            self.label.setGeometry(QtCore.QRect(pz, py, 100, 20))
            self.label.setText(str(p[0]))

            py = py + 40

            # Altcode

            self.label_2 = QtWidgets.QLabel(self.frame)
            self.label_2.setGeometry(QtCore.QRect(pz, py, 100, 20))
            self.label_2.setText(str(p[1]))

            py = py + 40

            # Colour

            self.label_3 = QtWidgets.QLabel(self.frame)
            self.label_3.setGeometry(QtCore.QRect(pz, py, 100, 20))
            self.label_3.setText(str(p[2]))

            py = py + 40

            # Button

            self.p_button = QtWidgets.QPushButton(self.frame)
            self.p_button.setGeometry(QtCore.QRect(10, py, 130, 40))
            self.p_button.setText("Print " + str(p[3]) + " Labels")
            self.p_button.clicked.connect(partial(self.print_clicked, str(p[0])))

            z = z + 180
            i = i + 1

            if i == 5:
                z = 20
                i = 0
                y = y + 230

        self.scrollArea.setWidget(self.scrollAreaWidgetContents)
        self.scrollArea.repaint()

    def print_clicked(self, pp):
        labels = sql.get_labelsforprepack(pp)
        conn = cups.Connection()
        totallabels = len(labels)
        alloccode = labels[0][1]
        bcode = barcode.get('code39', str(alloccode), writer=ImageWriter())
        filename = bcode.save('bcode')
        conn.printFile("USB", filename, "Test", {"copies":  str(totallabels)})

        sql.insert_print_record(alloccode)

        for l in labels:
            sql.insert_carton(str(l[0]), str(l[1]))

        self.refresh()

    def refresh(self):

        d = self.scrollAreaWidgetContents.children()
        e = reversed(d)

        for g in e:
            g.deleteLater()

        self.scrollAreaWidgetContents.deleteLater()
        self.setupDynamics()


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupBasics(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())