PyQt5控制 窗口和显示窗口

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PyQt5控制 窗口和显示窗口相关的知识,希望对你有一定的参考价值。

我需要在多台显示器上同时运行两个PyQt窗口:一个用于显示屏幕,另一个用于输入。在下面的例子中,我试图让一个带有按钮的简单窗口和另一个带有标签的窗口。当我按下按钮时,其他窗口标签文本应该更改:

import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QWidget, QDialog, QInputDialog,QDesktopWidget,QVBoxLayout,QPushButton,QMainWindow,QAction,QFileDialog,QGroupBox,QGridLayout,QLabel


class Control_Pannel(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'Main Control Pannel'
        self.left = 10
        self.top = 10
        self.width = 640
        self.height = 400
        self.text = "Display text"
        self.InitializeUI()


    def InitializeUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left,self.top,self.width,self.height)
        self.inputBox = QGroupBox("Display")
        layout = QGridLayout()
        layout.setColumnStretch(1, 4)
        layout.setColumnStretch(2, 4)
        button = QPushButton('Push here to change text')
        layout.addWidget(button,0,0)
        button.clicked.connect(self.on_click)
        self.inputBox.setLayout(layout)
        windowLayout = QVBoxLayout()
        windowLayout.addWidget(self.inputBox)
        self.setLayout(windowLayout)
        self.show()

        app = QApplication(sys.argv)
        ex=InputWindows()
        sys.exit(app.exec_())


    @pyqtSlot()
    def on_click(self):
        InputWindows.text = "Modified text after button push"
        print(InputWindows.text)
        InputWindows.update()
        ##???? here i don't know how to make the changes be reflected in the other window, even if when i print the .text attribute it seems to have changed.



class InputWindows(QDialog):

    def __init__(self):
        super().__init__()
        self.title = 'Display Pannel'
        self.left = 5
        self.top = 5
        self.width = 300
        self.height = 300
        self.text = "Original Text"
        self.InitializeUI()

    def InitializeUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left,self.top,self.width,self.height)


        self.createDisplayGridLayout()
        windowLayout = QVBoxLayout()
        windowLayout.addWidget(self.horizontalGroupBox)
        self.setLayout(windowLayout)
        self.show()

    def createDisplayGridLayout(self):

        self.horizontalGroupBox = QGroupBox("Grid")
        layout = QGridLayout()
        layout.setColumnStretch(1, 4)
        layout.setColumnStretch(2, 4)
        self.label = QLabel(self.text)
        layout.addWidget(self.label,0,0) 



if __name__=='__main__':
    app=QApplication(sys.argv)
    ex=Control_Pannel()
    sys.exit(app.exec_())

随着InputWindows.update()我得到那个first argument of unbound method must have a type QWidget,如果我尝试InputWindows.label.update()我得到type object inputwindows has no attribute label

答案

InputWindows是一个类,它是一个抽象,所以你通常必须创建一个对象,我问你:如果你有n个Windows InputWindows,你会在哪个窗口更新文本?,我们不会知道,所以通过简单的逻辑我们看到这样做是错误的。另一方面,将每个类视为一个黑盒子,它由输入激发,并获得输出,在您的情况下,Control_Pannel必须有一个输出:新文本,但该输出是异步的,因为它更改,必须在改变,在Qt中是一个信号,所以我们必须创造它。另一方面,InputWindows必须接收信息以创建更新QLabel中文本的插槽,如下所示:

import sys
from PyQt5 import QtCore, QtWidgets


class Control_Pannel(QtWidgets.QWidget):
    sendSignal = QtCore.pyqtSignal(str)

    def __init__(self):
        super().__init__()
        self.title = 'Main Control Pannel'
        self.left = 10
        self.top = 10
        self.width = 640
        self.height = 400
        self.text = "Display text"
        self.InitializeUI()

    def InitializeUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left,self.top,self.width,self.height)
        self.inputBox = QtWidgets.QGroupBox("Display")
        layout = QtWidgets.QGridLayout()
        layout.setColumnStretch(1, 4)
        layout.setColumnStretch(2, 4)
        button = QtWidgets.QPushButton('Push here to change text')
        layout.addWidget(button,0,0)
        button.clicked.connect(self.on_click)
        self.inputBox.setLayout(layout)
        windowLayout = QtWidgets.QVBoxLayout(self)
        windowLayout.addWidget(self.inputBox)

    @QtCore.pyqtSlot()
    def on_click(self):
        text = "Modified text after button push"
        self.sendSignal.emit(text)


class InputWindows(QtWidgets.QDialog):
    def __init__(self):
        super().__init__()
        self.title = 'Display Pannel'
        self.left = 5
        self.top = 5
        self.width = 300
        self.height = 300
        self.InitializeUI()
        self.setText("Original Text")

    @QtCore.pyqtSlot(str)
    def setText(self, text):
        self.label.setText(text)

    def InitializeUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left,self.top,self.width,self.height)
        self.createDisplayGridLayout()
        windowLayout = QtWidgets.QVBoxLayout(self)
        windowLayout.addWidget(self.horizontalGroupBox)
        self.setLayout(windowLayout)

    def createDisplayGridLayout(self):
        self.horizontalGroupBox = QtWidgets.QGroupBox("Grid")
        layout = QtWidgets.QGridLayout()
        layout.setColumnStretch(1, 4)
        layout.setColumnStretch(2, 4)
        self.label = QtWidgets.QLabel()
        layout.addWidget(self.label, 0, 0) 
        self.horizontalGroupBox.setLayout(layout)



if __name__=='__main__':
    app = QtWidgets.QApplication(sys.argv)

    ex1 = Control_Pannel()
    ex1.show()
    ex2 = InputWindows()
    ex2.show()

    ex1.sendSignal.connect(ex2.setText)

    sys.exit(app.exec_())

以上是关于PyQt5控制 窗口和显示窗口的主要内容,如果未能解决你的问题,请参考以下文章

在pyqt5中设置显示窗口的代码是啥?

PyQt5对话框窗口打开时没有布局

如何在pyqt5中显示隐藏窗口?

PyQt5如何在一个窗口中显示不同的页面?

在 PyQt5 中显示和隐藏多个窗口

如何从 PyQT5 窗口访问 PyQGIS 地图画布