pyqt5gui与其他python程序的集成

2024-06-02 07:25:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经用PyQt5构建了一个GUI,现在我想将它与另一个python程序集成。我以前在单独的线程中放置了用TKinter编写的GUI代码。我想知道如何以最佳方式将这个PyQt GUI集成到我的其他代码中。你知道吗

我的另一个程序有两个线程监测旋转编码器和主线程上的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())

Tags: testimportself程序indexdefguiactivity