在PYQT5中创建一个完整的屏幕按钮

问题描述 投票:0回答:1

开始申请

绘制图表

全屏

我在主窗口上有一个带有4 Box的应用程序,我希望在窗口中有一个全屏按钮,可以绘制一些图形,就像在顶部的图片上一样。

我首先尝试一种方法在我的代码中创建一个fullScreen函数,链接到按钮,但它没有用。

这是我的尝试:

class mainApplication(QWidget):

    def __init__(self, parent=None):
        super(mainApplication, self).__init__(parent)

        self.layoutMap = {}
        self.buttonMap = {}

        # Figure Bottom Right
        self.figure = plt.figure(figsize=(15,5))
        self.figure.set_facecolor('0.915')
        self.canvas = FigureCanvas(self.figure) 

        # Main Figure
        self.setGeometry(600, 300, 1000, 600)

        self.topLeft()
        self.topRight()
        self.bottomLeft()
        self.bottomRight()

        mainLayout = QGridLayout()
        mainLayout.addWidget(self.topLeftBox, 1, 0)
        mainLayout.addWidget(self.topRightBox, 1, 1)
        mainLayout.addWidget(self.bottomLeftBox, 2, 0)
        mainLayout.addWidget(self.bottomRightBox, 2, 1)
        mainLayout.setRowStretch(1, 1)
        mainLayout.setRowStretch(2, 1)
        mainLayout.setColumnStretch(0, 1)
        mainLayout.setColumnStretch(1, 1)
        self.saveLayout(mainLayout, "main")

        self.setLayout(mainLayout)

        self.setWindowTitle("Title")
        QApplication.setStyle("Fusion")
        self.show()

    def bottomRight(self):

        self.bottomRightBox = QGroupBox("Bottom Right")

        # Create Select Button
        chooseButton = QPushButton("Select")
        chooseButton.setMaximumWidth(100)
        chooseButton.setMaximumHeight(20)
        self.saveButton(chooseButton)
        chooseButton.clicked.connect(self.selectFunction)

        # Create Full Screen Button
        fullScreenButton = QPushButton("Full")
        fullScreenButton.setMaximumWidth(100)
        fullScreenButton.setMaximumHeight(20)
        self.saveButton(fullScreenButton)
        fullScreenButton.clicked.connect(self.swichFullScreen)

        # Create Layout
        layout = QVBoxLayout()
        layout.addWidget(self.canvas)
        layout.addWidget(chooseButton)
        layout.addWidget(fullScreenButton)
        layout.addStretch(1)

        self.saveLayout(layout, "full")


        # Add Layout to GroupBox
        self.bottomRightBox.setLayout(layout)   


    def selectFunction(self):

        # Select Data
        filePath, _ = QtWidgets.QFileDialog.getOpenFileName(self, 'Open file', '/Data/')
        df = pd.read_csv(str(filePath))
        x = df.x.tolist()
        y = df.y.tolist()

        # Create Figure
        self.figure.clf()
        ax = self.figure.add_subplot(111)
        ax.plot(x, y)
        ax.set_facecolor('0.915')
        ax.set_title('Graphique')

        # Draw Graph
        self.canvas.draw()

    def saveLayout(self,obj, text):
         self.layoutMap[text] = obj

    def findLayout(self,text):
         return self.layoutMap[text]

    def saveButton(self,obj):
         self.buttonMap[obj.text()] = obj

    def findButton(self,text):
         return self.buttonMap[text]


    def swichFullScreen(self):
        self.setLayout(self.findLayout("full"))
        self.show()





if __name__ == '__main__':

    app = QApplication(sys.argv)
    mainWindow = mainApplication()
    sys.exit(app.exec_())

你有个主意吗?因为例如,如果在我的初始化中我不这样做:

self.setLayout(mainLayout)

但是:

swichFullScreen()

我有我想要的结果,所以为什么在创建主布局后调用这个函数不起作用?

此外,我尝试了另一种适配器:PyQt: Change GUI Layout after button is clicked

但它仍然无效,因为当我点击“完整”按钮时,它切换得非常好,但正常的Window对象已被删除,因此按钮选择停止工作。

如果你有我的第一个想法的解决方案,我更喜欢,因为它避免了其他类的创建,但如果不可能并且你找到第二个解决方案的解决方案以避免对象的破坏,我也接受它。

这是我的第二个解决方案的代码:

class fullScreenApplication(QWidget):
    def __init__(self, parent=None):
        super(fullScreenApplication, self).__init__(parent)
        self.setGeometry(600, 300, 1000, 600)


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setGeometry(600, 300, 1000, 600)
        self.normalWindows()

    def normalWindows(self):
        self.normalBox = mainApplication(self)
        self.setCentralWidget(self.normalBox)
        self.normalBox.findButton("Full").clicked.connect(self.fullScreenWindow)
        self.show()

    def fullScreenWindow(self):
        self.FullBox = fullScreenApplication(self)
        self.FullBox.setLayout(self.normalBox.findLayout("full"))
        self.normalBox.findButton("Full").clicked.connect(self.normalWindows)
        self.normalBox.findButton("Select").clicked.connect(self.normalBox.selectFunction)
        self.setCentralWidget(self.FullBox)
        self.show()





if __name__ == '__main__':

    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    sys.exit(app.exec_()) 

谢谢

python pyqt pyqt5 fullscreen
1个回答
0
投票

试试吧:

import sys
import pandas as pd
import matplotlib.pyplot  as plt
from PyQt5.QtCore    import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui     import *
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure


class mainApplication(QWidget):

    def __init__(self, parent=None):
        super(mainApplication, self).__init__(parent)

        self.layoutMap = {}
        self.buttonMap = {}

        # Figure Bottom Right
        self.figure = plt.figure(figsize=(15,5))
        self.figure.set_facecolor('0.915')
        self.canvas = FigureCanvas(self.figure) 

        # Main Figure
#        self.setGeometry(600, 300, 1000, 600)

        self.topLeftBox    = self.topLeft()
        self.topRightBox   = self.topRight()
        self.bottomLeftBox = self.bottomLeft()
        self.bottomRight()

        self.mainLayout = QGridLayout()
        self.mainLayout.addWidget(self.topLeftBox, 1, 0)
        self.mainLayout.addWidget(self.topRightBox, 1, 1)
        self.mainLayout.addWidget(self.bottomLeftBox, 2, 0)
        self.mainLayout.addWidget(self.bottomRightBox, 2, 1)
        self.mainLayout.setRowStretch(1, 1)
        self.mainLayout.setRowStretch(2, 1)
        self.mainLayout.setColumnStretch(0, 1)
        self.mainLayout.setColumnStretch(1, 1)
        self.saveLayout(self.mainLayout, "main")

        self.setLayout(self.mainLayout)

        self.setWindowTitle("Title")
        QApplication.setStyle("Fusion")
#        self.show()

    def bottomRight(self):

        self.bottomRightBox = QGroupBox("Bottom Right")

        # Create Select Button
        chooseButton = QPushButton("Select")
        chooseButton.setMaximumWidth(100)
        chooseButton.setMaximumHeight(20)
        self.saveButton(chooseButton)
        chooseButton.clicked.connect(self.selectFunction)

        # Create Full Screen Button
        self.fullScreenButton = QPushButton("Full")
        self.fullScreenButton.setMaximumWidth(100)
        self.fullScreenButton.setMaximumHeight(20)
        self.saveButton(self.fullScreenButton)
        self.fullScreenButton.clicked.connect(self.swichFullScreen)

        # Create Layout
        layout = QVBoxLayout()
        layout.addWidget(self.canvas)
        layout.addWidget(chooseButton)
        layout.addWidget(self.fullScreenButton)
        layout.addStretch(1)

        self.saveLayout(layout, "full")


        # Add Layout to GroupBox
        self.bottomRightBox.setLayout(layout)   


    def selectFunction(self):

        # Select Data
        filePath, _ = QFileDialog.getOpenFileName(self, 'Open file', '/Data/')
        df = pd.read_csv(str(filePath))
        x = df.x.tolist()
        y = df.y.tolist()

        # Create Figure
        self.figure.clf()
        ax = self.figure.add_subplot(111)
        ax.plot(x, y)
        ax.set_facecolor('0.915')
        ax.set_title('Graphique')

        # Draw Graph
        self.canvas.draw()

    def saveLayout(self,obj, text):
         self.layoutMap[text] = obj

    def findLayout(self,text):
         return self.layoutMap[text]

    def saveButton(self,obj):
         self.buttonMap[obj.text()] = obj

    def findButton(self,text):
         return self.buttonMap[text]


    def swichFullScreen(self):
#        self.setLayout(self.findLayout("full"))           # ---
#        self.show()                                       # ---
# +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
        if self.sender().text()== "Full":
            self.topLeftBox.hide()
            self.topRightBox.hide()
            self.bottomLeftBox.hide()
            self.bottomRightBox.hide()
            self.mainLayout.addWidget(self.bottomRightBox, 0, 0, 1, 2)
            self.bottomRightBox.show()
            self.fullScreenButton.setText("NoFull")

        else:
            self.bottomRightBox.hide()
            self.topLeftBox.show()
            self.topRightBox.show()
            self.bottomLeftBox.show()
            self.mainLayout.addWidget(self.bottomRightBox, 2, 1)
            self.bottomRightBox.show()
            self.fullScreenButton.setText("Full")            

    def topLeft(self):
        textEdit = QTextEdit()
        return textEdit
    def topRight(self):
        textEdit = QTextEdit()
        return textEdit
    def bottomLeft(self):
        textEdit = QTextEdit()
        return textEdit
# +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^       

if __name__ == '__main__':

    app = QApplication(sys.argv)
    mainWindow = mainApplication()
    mainWindow.setGeometry(200, 100, 1000, 600)  
    mainWindow.show()
    sys.exit(app.exec_())

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.