在PYQT5中创建全屏按钮

时间:2019-02-17 14:31:54

标签: python pyqt pyqt5 fullscreen

应用程序启动

绘制图形

全屏

我在主窗口上有一个带有4 Box的应用程序,我想在窗口中有一个全屏按钮来绘制一些图形,例如顶部的图片。

我首先尝试用一种方法在我的代码中创建一个全屏功能并链接到该按钮,但这是行不通的。

这是我的尝试:

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

但是它仍然不起作用,因为当我点击按钮“ full”时,它切换得很好,但是normalWindow对象已被删除,因此按钮选择停止工作。

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

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

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_()) 

谢谢

1 个答案:

答案 0 :(得分: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