我想创建一个在滚动区域显示按钮的程序。 然而,即使我放了很多按钮,滚动条也不会在滚动区域中创建,按钮只会被切断。
import sys
import os
import pandas as pd
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5 import QtCore, QtGui
class UI_MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setupUi()
def setupUi(self):
self.setWindowTitle("")
self.setGeometry(100, 100, 1024, 768)
icon_x = 10
icon_y = 60
icon_button_width = 60
icon_button_height = 60
icon_label_width = 60
icon_label_height = 20
tier_x = 0
tier_y = 40
tier_button_width = 50
tier_button_height = 50
icons_folder = '.\\c_icons\\champ_icons'
icon_files = os.listdir(icons_folder)
icon_path = []
for icon_file in icon_files:
icon_path.append(os.path.join(icons_folder, icon_file))
icon_path.sort()
tier_icon_folder = '.\\c_icons\\tier_icons'
tier_icon_files = os.listdir(tier_icon_folder)
tier_icon_path = []
for tier_icon_file in tier_icon_files:
tier_icon_path.append(os.path.join(tier_icon_folder, tier_icon_file))
tier_folder = '.\\c_prop'
tier_files = os.listdir(tier_folder)
tier_path = []
for tier_file in tier_files:
tier_path.append(os.path.join(tier_folder, tier_file))
tier_f = pd.read_excel(str(tier_path)[2:-2], header=None, index_col=None)
scroll_area_1 = QScrollArea(self)
scroll_area_1.setGeometry(120, 100, 300, 600)
scroll_area_1.setWidgetResizable(True)
scroll_content_1 = QWidget()
scroll_area_1.setWidget(scroll_content_1)
scroll_area_1.setStyleSheet("background-color: #201F22;"
"border-width: 0px;"
"border-radius: 15px;")
self.lineEdit = QLineEdit(scroll_content_1)
self.lineEdit.setGeometry(10, 10, 280, 40)
self.lineEdit.setStyleSheet("background-color: #2B2B30;"
"border-width: 0px;"
"border-radius: 3px;")
self.lineEdit.textChanged.connect(self.filterButtons)
self.lineEdit.setMaximumHeight(40)
self.icon_buttons = []
self.icon_labels = []
for j in range(icon_path.__len__()):
button = QPushButton(scroll_content_1)
button.setIcon(QIcon(icon_path[j]))
button.setIconSize(QtCore.QSize(icon_button_width, icon_button_height))
button.setGeometry(icon_x, icon_y, icon_button_width, icon_button_height)
button.setObjectName(f"Icon_{str(icon_files[j][:-4])}")
button.setText("") # 버튼 텍스트 제거
self.icon_buttons.append(button)
label = QLabel(f"Label {j + 1}", scroll_content_1)
label.setText(icon_files[j][:-4])
label.setObjectName(f"Label {j + 1}")
if len(icon_files[j][:-4]) > 3:
label.setText((str(icon_files[j][:3]) + "..."))
label.setGeometry(icon_x, icon_y + 60, icon_label_width, icon_label_height)
label.setStyleSheet("color: #9E9EAF;")
self.icon_labels.append(label)
if j % 4 == 3:
icon_x = 10
icon_y += 100
else:
icon_x += 70
def filterButtons(self, text):
text = text.replace(" ", "")
x = 10
y = 50
i = 0
for button, label in zip(self.icon_buttons, self.icon_labels):
if text not in button.objectName():
button.hide()
label.hide()
button.setDisabled(True)
label.setDisabled(True)
else:
button.show()
label.show()
button.setEnabled(True)
label.setEnabled(True)
button.setGeometry(x, y, 60, 60)
label.setGeometry(x, y + 60, 60, 20)
if (i + 1) % 4 == 0:
y += 80
x = 10
else:
x += 70
i += 1
if __name__ == "__main__":
app = QApplication(sys.argv)
window = UI_MainWindow()
window.show()
sys.exit(app.exec_())
在这段代码中,即使我用setHorizontalScrollBarPolicy函数设置了Alwayson,滚动条仍然可见,但无法操作。 另外,当使用网格布局时,我无法将每个对象的大小调整为所需的像素,所以我不想这样做。 有没有办法在不使用 setHorizontalScrollBarPolicy 函数和 GridLayout 的情况下创建滚动条?
确保您的程序在添加按钮时动态调整滚动区域的内容大小。 PyQt 要求您显式指定内容的大小或使用自动为您管理大小的布局。对于您的情况,请将按钮放入 QWidget 中,然后将该 QWidget 设置为滚动区域的小部件。使用 QVBoxLayout 或 QHBoxLayout 来管理该小部件中的按钮。这可以确保您的滚动区域知道需要显示多少空间,并在内容超出可见区域时相应地激活滚动条。