pyqt5在GridView小部件下方添加第二个布局

发布于 2025-02-09 23:14:04 字数 2357 浏览 1 评论 0原文

再会!我希望有人可以告诉我以下简单代码在哪里出错。我正在在GridView中显示图像(如下所示,如下所示,如下所示)使用了通常的行和列在编队中显示。我的问题是在GridView下,我希望添加两个独立的按钮(确定/取消)。这些我不想成为网格的一部分,而是自己的下方。

从周围的许多小时开始,我认为我需要将网格视图添加到垂直盒中,即顶部插槽,然后在底部添加到底部的按钮,但我不确定要实现这一目标的正确方法。我对此代码的尝试不佳。要提到的一件事是,完整的GridView被传递到可滚动区域。我不知道那是为什么我看不到按钮出现在我的示例中的原因。谢谢您

希望它看起来像 ”在此处输入图像描述

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = "Lost with widgets"
        self.main_window()

    def main_window(self):
        self.setWindowTitle(self.title)
        self.setGeometry(0, 0, 600, 600)
        # Make Grid
        self.grid = QGridLayout()
        self.grid.setSpacing(100)
        # Make buttons
        folder_button = QPushButton('Top Buttons 1', self)
        self.grid.addWidget(folder_button, 0, 1, alignment=Qt.AlignCenter)  # Add to grid
        folder_button1 = QPushButton('Top Buttons 2', self)
        self.grid.addWidget(folder_button1, 0, 2, alignment=Qt.AlignCenter)  # Add to grid
        # Complete layout of Grid
        self.setLayout(self.grid)
        #########################################################
        # Create Ok and Cancel bottom buttons#
        ########################################################
        okButton = QPushButton("OK")
        cancelButton = QPushButton("Cancel")
        # Horizonal
        hbox = QHBoxLayout()
        hbox.addStretch(1)
        hbox.addWidget(okButton)
        hbox.addWidget(cancelButton)
        # Vertical
        vbox = QVBoxLayout()
        vbox.addStretch(1)
        vbox.addLayout(hbox)
        # Set layout
        self.setLayout(hbox)
        # add widget and set its layout
        wrapper_widget = QWidget()
        wrapper_widget.setLayout(self.grid)
        # Scroll
        scroll = QScrollArea()
        scroll.setWidget(wrapper_widget)
        scroll.setSizeAdjustPolicy(scroll.AdjustToContents)
        scroll.setWidgetResizable(False) # Spaces out
        self.setCentralWidget(scroll)
        # Show
        self.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    sys.exit(app.exec())

Good day! I hope someone can tell me where I am going wrong with the following simple code. I am displaying images in a gridview (shown as buttons for simplicity below) These use the usual row and columns to display in formation. My issue is under the gridview I wish to add two independent buttons (ok/cancel). These I don't want to be part of grid but on their own underneath.

From many hours of messing around I thought I would need to add my grid view to a vertical box i.e top slot and then the buttons i've made to the bottom one but i'm not sure of the correct way to achieve this. My poor attempt at this code is below. One thing to mention is the completed gridview is passed into a scrollable area. I don't know if that's why I don't see the buttons appear in my example. Thank you

Hoping for it to look like this enter image description here

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = "Lost with widgets"
        self.main_window()

    def main_window(self):
        self.setWindowTitle(self.title)
        self.setGeometry(0, 0, 600, 600)
        # Make Grid
        self.grid = QGridLayout()
        self.grid.setSpacing(100)
        # Make buttons
        folder_button = QPushButton('Top Buttons 1', self)
        self.grid.addWidget(folder_button, 0, 1, alignment=Qt.AlignCenter)  # Add to grid
        folder_button1 = QPushButton('Top Buttons 2', self)
        self.grid.addWidget(folder_button1, 0, 2, alignment=Qt.AlignCenter)  # Add to grid
        # Complete layout of Grid
        self.setLayout(self.grid)
        #########################################################
        # Create Ok and Cancel bottom buttons#
        ########################################################
        okButton = QPushButton("OK")
        cancelButton = QPushButton("Cancel")
        # Horizonal
        hbox = QHBoxLayout()
        hbox.addStretch(1)
        hbox.addWidget(okButton)
        hbox.addWidget(cancelButton)
        # Vertical
        vbox = QVBoxLayout()
        vbox.addStretch(1)
        vbox.addLayout(hbox)
        # Set layout
        self.setLayout(hbox)
        # add widget and set its layout
        wrapper_widget = QWidget()
        wrapper_widget.setLayout(self.grid)
        # Scroll
        scroll = QScrollArea()
        scroll.setWidget(wrapper_widget)
        scroll.setSizeAdjustPolicy(scroll.AdjustToContents)
        scroll.setWidgetResizable(False) # Spaces out
        self.setCentralWidget(scroll)
        # Show
        self.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    sys.exit(app.exec())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

擦肩而过的背影 2025-02-16 23:14:04

这是因为您将scroll小部件设置为中央小部件。 scroll widget仅包含wrapper_widget,其中包含gridlayout,而不是vbox

尝试创建vbox,然后添加scroll widget和hbox vbox,并使用qwidget设置中央小部件/代码>具有vbox布局。

我修改了您的一些代码,添加master_widget具有vbox包含scrollhbox布局的布局。检查一下!

class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = "Lost with widgets"
        self.main_window()

    def main_window(self):
        self.setWindowTitle(self.title)
        self.setGeometry(0, 0, 600, 600)
        # Make Grid
        self.grid = QGridLayout()
        self.grid.setSpacing(100)
        # Make buttons
        folder_button = QPushButton('Top Buttons 1', self)
        self.grid.addWidget(folder_button, 0, 1, alignment=Qt.AlignCenter)  # Add to grid
        folder_button1 = QPushButton('Top Buttons 2', self)
        self.grid.addWidget(folder_button1, 0, 2, alignment=Qt.AlignCenter)  # Add to grid
        # Complete layout of Grid
        self.setLayout(self.grid)
        #########################################################
        # Create Ok and Cancel bottom buttons#
        ########################################################
        okButton = QPushButton("OK")
        cancelButton = QPushButton("Cancel")
        # Horizonal
        hbox = QHBoxLayout()
        hbox.addStretch(1)
        hbox.addWidget(okButton)
        hbox.addWidget(cancelButton)
        # Vertical
        vbox = QVBoxLayout()
        vbox.addStretch(1)
        # vbox.addLayout(hbox)
        # Set layout
        # self.setLayout(vbox)
        # add widget and set its layout
        wrapper_widget = QWidget()
        wrapper_widget.setLayout(self.grid)
        # Scroll
        scroll = QScrollArea()
        scroll.setWidget(wrapper_widget)
        # scroll.setSizeAdjustPolicy(scroll.AdjustToContents)
        scroll.setWidgetResizable(False) # Spaces out
        master_widget = QWidget()
        master_widget.setLayout(vbox)
        vbox.addWidget(scroll)
        vbox.addLayout(hbox)
        self.setCentralWidget(master_widget)
        # Show
        self.show()

It's because you set scroll widget as central widget. scroll widget contains only wrapper_widget, which contains gridlayout, not vbox.

Try creating vbox and add scroll widget and hbox to vbox, and set central widget with QWidget which has vbox layout.

I modified some of your code, adding master_widget which has vbox layout that contains scroll and hbox layout. Check this!

class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = "Lost with widgets"
        self.main_window()

    def main_window(self):
        self.setWindowTitle(self.title)
        self.setGeometry(0, 0, 600, 600)
        # Make Grid
        self.grid = QGridLayout()
        self.grid.setSpacing(100)
        # Make buttons
        folder_button = QPushButton('Top Buttons 1', self)
        self.grid.addWidget(folder_button, 0, 1, alignment=Qt.AlignCenter)  # Add to grid
        folder_button1 = QPushButton('Top Buttons 2', self)
        self.grid.addWidget(folder_button1, 0, 2, alignment=Qt.AlignCenter)  # Add to grid
        # Complete layout of Grid
        self.setLayout(self.grid)
        #########################################################
        # Create Ok and Cancel bottom buttons#
        ########################################################
        okButton = QPushButton("OK")
        cancelButton = QPushButton("Cancel")
        # Horizonal
        hbox = QHBoxLayout()
        hbox.addStretch(1)
        hbox.addWidget(okButton)
        hbox.addWidget(cancelButton)
        # Vertical
        vbox = QVBoxLayout()
        vbox.addStretch(1)
        # vbox.addLayout(hbox)
        # Set layout
        # self.setLayout(vbox)
        # add widget and set its layout
        wrapper_widget = QWidget()
        wrapper_widget.setLayout(self.grid)
        # Scroll
        scroll = QScrollArea()
        scroll.setWidget(wrapper_widget)
        # scroll.setSizeAdjustPolicy(scroll.AdjustToContents)
        scroll.setWidgetResizable(False) # Spaces out
        master_widget = QWidget()
        master_widget.setLayout(vbox)
        vbox.addWidget(scroll)
        vbox.addLayout(hbox)
        self.setCentralWidget(master_widget)
        # Show
        self.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文