#python #pyqt5 #qt-designer
#python #pyqt5 #qt-designer
Вопрос:
Я создал два файла .ui с помощью pyqt designer: один является главным окном, а другой — виджетом. Всякий раз, когда я пытаюсь добавить виджет в область прокрутки в моем главном окне, он не отображается на экране, и я не могу понять почему.
Извините, если это глупый вопрос, я все еще изучаю pyqt5 и pyqt designer. Буду признателен за любую помощь, спасибо.
код:
class Window(QMainWindow):
def __init__(self):
super(Window,self).__init__()
self.ui=Ui_Reddit()
self.ui.setupUi(self)
self.setStyle(QStyleFactory.create('Cleanlooks'))
#self.setGeometry(300, 300, 600, 600)
self.setWindowTitle("Reddit")
self.show()
self.mainWindow()
def mainWindow(self):
#to handle all the api calls using praw
self.x = RedditApi()
self.printSubmissions()
def printSubmissions(self):
#Gets the list of all submission titles to be displayed
#TO DO: Get and add other things like points and comments
self.submissions = self.x.showSubmissions()
for submission in self.submissions:
card = QWidget()
card_ui = Ui_Card()
card_ui.setupUi(card)
card_ui.title.setText(submission)
self.ui.verticalLayout_2.addWidget(card)
Ui_Reddit:
class Ui_Reddit(object):
def setupUi(self, Reddit):
Reddit.setObjectName("Reddit")
Reddit.resize(800, 600)
Reddit.setTabShape(QtWidgets.QTabWidget.Rounded)
self.centralwidget = QtWidgets.QWidget(Reddit)
self.centralwidget.setObjectName("centralwidget")
self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
self.scrollArea.setGeometry(QtCore.QRect(10, 60, 781, 531))
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 779,
529))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.verticalLayoutWidget_2 =
QtWidgets.QWidget(self.scrollAreaWidgetContents)
self.verticalLayoutWidget_2.setGeometry(QtCore.QRect(-1, -1, 811,
551))
self.verticalLayoutWidget_2.setObjectName("verticalLayoutWidget_2")
self.verticalLayout_2 =
QtWidgets.QVBoxLayout(self.verticalLayoutWidget_2)
self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.horizontalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(0, 0, 801, 51))
self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
self.horizontalLayout =
QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.horizontalLayoutWidget)
self.label.setIndent(5)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.comboBox = QtWidgets.QComboBox(self.horizontalLayoutWidget)
self.comboBox.setObjectName("comboBox")
self.horizontalLayout.addWidget(self.comboBox)
spacerItem = QtWidgets.QSpacerItem(40, 20,
QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.search_bar = QtWidgets.QLineEdit(self.horizontalLayoutWidget)
self.search_bar.setText("")
self.search_bar.setObjectName("search_bar")
self.horizontalLayout.addWidget(self.search_bar)
self.pushButton = QtWidgets.QPushButton(self.horizontalLayoutWidget)
self.pushButton.setFlat(False)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
spacerItem1 = QtWidgets.QSpacerItem(40, 20,
QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem1)
Reddit.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(Reddit)
self.statusbar.setObjectName("statusbar")
Reddit.setStatusBar(self.statusbar)
self.retranslateUi(Reddit)
QtCore.QMetaObject.connectSlotsByName(Reddit)
def retranslateUi(self, Reddit):
_translate = QtCore.QCoreApplication.translate
Reddit.setWindowTitle(_translate("Reddit", "MainWindow"))
self.label.setText(_translate("Reddit", "TextLabel"))
self.search_bar.setPlaceholderText(_translate("Reddit", "Search"))
self.pushButton.setText(_translate("Reddit", "PushButton"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Reddit = QtWidgets.QMainWindow()
ui = Ui_Reddit()
ui.setupUi(Reddit)
Reddit.show()
sys.exit(app.exec_())
Ui_Card:
class Ui_Card(object):
def setupUi(self, Card):
Card.setObjectName("Card")
Card.setWindowModality(QtCore.Qt.NonModal)
Card.setEnabled(True)
Card.resize(771, 134)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred,
QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(1)
sizePolicy.setHeightForWidth(Card.sizePolicy().hasHeightForWidth())
Card.setSizePolicy(sizePolicy)
self.title = QtWidgets.QLabel(Card)
self.title.setGeometry(QtCore.QRect(6, 10, 761, 21))
font = QtGui.QFont()
font.setPointSize(12)
self.title.setFont(font)
self.title.setObjectName("title")
self.user = QtWidgets.QLabel(Card)
self.user.setGeometry(QtCore.QRect(20, 40, 47, 13))
font = QtGui.QFont()
font.setPointSize(7)
self.user.setFont(font)
self.user.setObjectName("user")
self.label_3 = QtWidgets.QLabel(Card)
self.label_3.setGeometry(QtCore.QRect(70, 30, 20, 31))
font = QtGui.QFont()
font.setPointSize(16)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.subreddit = QtWidgets.QLabel(Card)
self.subreddit.setGeometry(QtCore.QRect(90, 40, 47, 13))
font = QtGui.QFont()
font.setPointSize(7)
self.subreddit.setFont(font)
self.subreddit.setObjectName("subreddit")
self.horizontalLayoutWidget = QtWidgets.QWidget(Card)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(10, 60, 771,
31))
self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
self.horizontalLayout =
QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
spacerItem = QtWidgets.QSpacerItem(40, 20,
QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.comments = QtWidgets.QLabel(self.horizontalLayoutWidget)
self.comments.setObjectName("comments")
self.horizontalLayout.addWidget(self.comments)
self.upvote = QtWidgets.QPushButton(self.horizontalLayoutWidget)
self.upvote.setText("")
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("upvoteArrow.jpg"), QtGui.QIcon.Normal,
QtGui.QIcon.Off)
self.upvote.setIcon(icon)
self.upvote.setFlat(True)
self.upvote.setObjectName("upvote")
self.horizontalLayout.addWidget(self.upvote)
self.downvote = QtWidgets.QPushButton(self.horizontalLayoutWidget)
self.downvote.setFlat(True)
self.downvote.setObjectName("downvote")
self.horizontalLayout.addWidget(self.downvote)
spacerItem1 = QtWidgets.QSpacerItem(40, 20,
QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem1)
self.line = QtWidgets.QFrame(Card)
self.line.setGeometry(QtCore.QRect(0, 100, 771, 21))
self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line.setLineWidth(3)
self.line.setFrameShape(QtWidgets.QFrame.HLine)
self.line.setObjectName("line")
self.retranslateUi(Card)
QtCore.QMetaObject.connectSlotsByName(Card)
def retranslateUi(self, Card):
_translate = QtCore.QCoreApplication.translate
Card.setWindowTitle(_translate("Card", "Form"))
self.title.setText(_translate("Card", "TextLabel"))
self.user.setText(_translate("Card", "TextLabel"))
self.label_3.setText(_translate("Card", "-"))
self.subreddit.setText(_translate("Card", "TextLabel"))
self.comments.setText(_translate("Card", "TextLabel"))
self.downvote.setText(_translate("Card", "downvote"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Card = QtWidgets.QWidget()
ui = Ui_Card()
ui.setupUi(Card)
Card.show()
sys.exit(app.exec_())
Комментарии:
1. поделиться
Ui_Reddit
иUi_Card
или .uis2. @eyllanesc Извините за задержку. Я обновил сообщение.