我正在尝试使用GUI - 我希望有滚动条和标签。我用滚动条作为最外面的"" widget,内置QTabWidget,反之亦然:QTabWidget有一个QWidget选项卡,QScrollArea的父选项卡是选项卡。简而言之,我无法扩展滚动区域以填充可用空间。下面有一些评论说明了我尝试过的所有内容。没有调整sizePolicies似乎有帮助。 我查看了其他人发布的类似问题,特别是Issue with scrollarea in pyqt?
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 17 07:07:08 2017
@author: Erik
"""
import sys
from PyQt5.QtWidgets import QLayout, QVBoxLayout ,QTabWidget, QMainWindow,QApplication, QWidget, QSizePolicy, QScrollArea
from PyQt5.QtGui import QColor, QPalette
from PyQt5.QtCore import QSize
class Color2(QWidget):
def __init__(self, color, *args, **kwargs):
super(Color2, self).__init__(*args, **kwargs)
self.setAutoFillBackground(True)
palette = self.palette()
palette.setColor(QPalette.Window, QColor(color))
self.setPalette(palette)
self.setSizePolicy(QSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum))
def sizeHint(self):
return QSize(250,150)
class MyTableWidget(QWidget):
def __init__(self, parent):
super(QWidget, self).__init__(parent)
self.layout = QVBoxLayout(self)
# Initialize tab screen
self.tabs = QTabWidget(self)
self.layout.addWidget(self.tabs)
#self.setSizePolicy(QSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored))
#self.tabs.setSizePolicy(QSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored))
def addTab(self, widget, text):
self.tabs.addTab(widget, text)
widget.setAutoFillBackground(True)
my_yellow = QColor.fromHsl(35, 255, 153, 128 )
dark_blue = QColor.fromHsl(211, 196, 38, 255)
widget.palette().setColor(QPalette.Window, dark_blue)
widget.palette().setColor(QPalette.WindowText, my_yellow)
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.statusBar().showMessage('Ready')
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Statusbar')
self.table_widget = MyTableWidget(self)
self.tab1 = QWidget()
self.tab2 = QWidget()
#self.tab1.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding))
self.table_widget.addTab(self.tab1, "Tab1")
self.table_widget.addTab(self.tab2, "Tab2")
self.scrollArea = QScrollArea(self.tab1)
#self.scrollArea = QScrollArea(self)
self.scrollArea.setWidgetResizable(True)
self.scrollAreaWidgetContents = QWidget(self.scrollArea)
#self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 380, 247))
self.scrollAreaWidgetContents.setMinimumSize(QSize(1100, 1300))
#self.scrollAreaWidgetContents.setSizePolicy(QSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored))
#self.scrollAreaWidgetContents.setWidgetResizable(True)
#self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.verticalLayout = QVBoxLayout(self.scrollAreaWidgetContents)
#self.verticalLayout.addWidget(self.scrollArea)
self.verticalLayout.addWidget(Color2('blue'))
self.verticalLayout.addWidget(Color2('red'))
self.verticalLayout.insertStretch(-1)
#self.verticalLayout.setSizeConstraint(1)#QLayout.SizeConstraint.setNoConstraint)
self.verticallayout2 = QVBoxLayout(self.tab2)
self.verticallayout2.addWidget(Color2('green'))
#self.scrollArea.setSizePolicy(QSizePolicy(QSizePolicy.Ignored, QSizePolicy.Expanding))
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.setCentralWidget(self.table_widget)
self.show()
print(self.table_widget.minimumSizeHint())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
我从http://zetcode.com/gui/pyqt5/学到了基础知识。
此外,上述代码行为的一个线索是QScrollArea
实际使用的区域与sizeHint
类的Color2
有某种关联。我有一个滚动条的例子,它们自己工作得很好......这只是他们在QTabWidget
的标签中将它们放在我不明白的行为中。有小费吗?非常感谢。
答案 0 :(得分:2)
问题是您没有使用布局在选项卡内设置QScrollArea。当您确定一个窗口小部件是另一个窗口小部件的父节点时,您只是指示子节点的坐标系相对于父节点,而不是它将占用父节点的大小,因此您必须使用布局,因为它处理大小
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 17 07:07:08 2017
@author: Erik
"""
import sys
from PyQt5.QtWidgets import QLayout, QVBoxLayout ,QTabWidget, QMainWindow,QApplication, QWidget, QSizePolicy, QScrollArea
from PyQt5.QtGui import QColor, QPalette
from PyQt5.QtCore import QSize
class Color2(QWidget):
def __init__(self, color, *args, **kwargs):
super(Color2, self).__init__(*args, **kwargs)
self.setAutoFillBackground(True)
palette = self.palette()
palette.setColor(QPalette.Window, QColor(color))
self.setPalette(palette)
self.setSizePolicy(QSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum))
def minimumSizeHint(self):
return QSize(250,150)
class MyTableWidget(QWidget):
def __init__(self, parent):
super(QWidget, self).__init__(parent)
self.layout = QVBoxLayout(self)
self.tabs = QTabWidget(self)
self.layout.addWidget(self.tabs)
def addTab(self, widget, text):
self.tabs.addTab(widget, text)
widget.setAutoFillBackground(True)
my_yellow = QColor.fromHsl(35, 255, 153, 128 )
dark_blue = QColor.fromHsl(211, 196, 38, 255)
widget.palette().setColor(QPalette.Window, dark_blue)
widget.palette().setColor(QPalette.WindowText, my_yellow)
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.statusBar().showMessage('Ready')
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Statusbar')
self.table_widget = MyTableWidget(self)
self.tab1 = QWidget()
self.tab2 = QWidget()
self.table_widget.addTab(self.tab1, "Tab1")
self.table_widget.addTab(self.tab2, "Tab2")
lay = QVBoxLayout(self.tab1)
self.scrollArea = QScrollArea()
lay.addWidget(self.scrollArea)
self.scrollArea.setWidgetResizable(True)
self.scrollAreaWidgetContents = QWidget()
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.verticalLayout = QVBoxLayout(self.scrollAreaWidgetContents)
self.verticalLayout.addWidget(Color2('blue'))
self.verticalLayout.addWidget(Color2('red'))
self.verticalLayout.insertStretch(-1)
self.verticallayout2 = QVBoxLayout(self.tab2)
self.verticallayout2.addWidget(Color2('green'))
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.setCentralWidget(self.table_widget)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())