将PyQt5 GUI与其他python程序集成

时间:2018-07-22 21:54:07

标签: python multithreading qt user-interface pyqt

我已经用PyQt5构建了一个GUI,现在我想将其与另一个python程序集成在一起。我以前通过将用TKinter编写的GUI代码放在单独的线程中来完成此操作。我想知道如何以最佳方式将此PyQt GUI集成到其他代码中。

我的另一个程序有2个监视旋转编码器的线程,主线程在RFID读取器上工作+有一些数据库通信。

PyQt GUI是否必须是程序的主线程?

这是PyQt gui:

from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QTableWidget, QTableWidgetItem, QVBoxLayout, QLabel 
from PyQt5.QtGui import QPixmap
import sys

class Reminder(): 

    def __init__(self):
        self.activity = 'activity' # instance variable unique to each instance
        self.time = 0000
        self.day = 'day'
        self.multiple_days = False


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.titel = "GUI for Andreas"
        self.top = 150
        self.left = 150 
        self.width = 500
        self.height = 500
        self.setWindowIcon(QtGui.QIcon("calender.png"))
        self.InitWindow()

    def InitWindow(self):
        self.setWindowTitle(self.titel)
        self.setGeometry(self.top,self.left,self.width,self.height)
        self.creatingTables(test_list)
        self.vBoxLayout = QVBoxLayout()
        self.vBoxLayout.addWidget(self.tableWidget)
        self.setLayout(self.vBoxLayout)
        self.textWidgets()
        self.vBoxLayout.addWidget(self.timelabel)
        self.vBoxLayout.addWidget(self.daylabel)
        self.show()

    def creatingTables(self, planned_activities):
        self.tableWidget = QTableWidget()
        self.tableWidget.setRowCount(len(planned_activities))
        self.tableWidget.setColumnCount(4)
        count = 0

        for i in planned_activities: 
            index = 0 + count
            self.label = QLabel(self)
            self.label.setPixmap(QPixmap('%s.png' %i.activity))
            self.tableWidget.setCellWidget(index,0, self.label)
            self.tableWidget.setItem(index,1, QTableWidgetItem(i.activity))
            self.tableWidget.setItem(index,2, QTableWidgetItem(i.day))
            self.tableWidget.setItem(index,3, QTableWidgetItem(str(i.time)))
            count +=1

    def textWidgets(self):
        self.timelabel = QLabel(self)
        self.daylabel = QLabel(self)
        self.timelabel.setText("Time:%s" %glo_dict['Time'])
        self.daylabel.setText("Day:%s" %glo_dict['Day'])

glo_dict = {'Time': 0000, 'Day': 'None'} #This dict will be updated by other threads in main code

#Simple test object, these objects will be made by the main thread in the other program. 
test_list=[]
a = Reminder() 
a.time = 1123
a.day = 'Monday'
a.activity = 'rolling'
test_list.append(a)


App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())

0 个答案:

没有答案