Создать полноэкранную кнопку в PYQT5 - PullRequest
0 голосов
/ 17 февраля 2019

Запуск приложения

Построение графика

Полноэкранный режим

У меня есть приложение с 4 полями в главном окне, и я хочу иметь полноэкранную кнопку в окнах, на которых изображен какой-то график, как на рисунках вверху.

Сначала я пытаюсь создать в своем коде функцию 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: Изменить макет GUI после нажатия кнопки

Но этовсе еще не работает, потому что, когда я нажимаю кнопку «полный», он переключается очень хорошо, но объект 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 голосов
/ 17 февраля 2019

Попробуйте:

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

...