在 PYQT5 中创建全屏按钮

Posted

技术标签:

【中文标题】在 PYQT5 中创建全屏按钮【英文标题】:Create a full Screen Button in PYQT5 【发布时间】:2019-02-17 14:31:53 【问题描述】:

应用程序的开始

绘制图表

全屏

我在主窗口上有一个带有 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

但它仍然不起作用,因为当我单击“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】:

试试看:

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

【讨论】:

@kilag 如果我的示例对您有所帮助,请记住将其标记为正确。

以上是关于在 PYQT5 中创建全屏按钮的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 TailwindCSS 在 div 中创建全高元素 [重复]

如何在 PyQT5 中创建导航栏

如何在 pyqt5 webbrowser 中为 youtube 视频创建全屏模式

pyqt5窗口全屏不显示边框

在 PyQt5 中打开多个窗口时遇到问题

Python PyQt5 - 向表中添加行