pyqt5在GridView小部件下方添加第二个布局
再会!我希望有人可以告诉我以下简单代码在哪里出错。我正在在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
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您将
scroll
小部件设置为中央小部件。scroll
widget仅包含wrapper_widget
,其中包含gridlayout
,而不是vbox
。尝试创建
vbox
,然后添加scroll
widget和hbox
vbox
,并使用qwidget设置中央小部件/代码>具有
vbox
布局。我修改了您的一些代码,添加
master_widget
具有vbox
包含scroll
和hbox
布局的布局。检查一下!It's because you set
scroll
widget as central widget.scroll
widget contains onlywrapper_widget
, which containsgridlayout
, notvbox
.Try creating
vbox
and addscroll
widget andhbox
tovbox
, and set central widget withQWidget
which hasvbox
layout.I modified some of your code, adding
master_widget
which hasvbox
layout that containsscroll
andhbox
layout. Check this!