【问题标题】:PyQt: Put scrollbars in thisPyQt:在此放置滚动条
【发布时间】:2012-12-19 00:30:27
【问题描述】:

这是我的课:

class ChildFoundDlg(QDialog):


    #__init__ function:
    def __init__(self, parent=None):
       QtGui.QDialog.__init__(self, parent)
       self.resize(500, 270)
       self.setMaximumSize(500, 540)
       self.GridLayout = QtGui.QGridLayout(self)        

    def buttons(self, a, b, c):    #a = 0, b = 5 c = 7 
       self.font = QtGui.QFont("Sans Serif", 10, QFont.Normal)

       self.label = QtGui.QLabel(self)
       self.label.setText(QtGui.QApplication.translate("Form", "Μήπως εννοείτε την/τον:", None, QtGui.QApplication.UnicodeUTF8))
       self.label.setFont(self.font)
       self.GridLayout.addWidget(self.label, a, 0, 1, 1) 

       self.label2 = QtGui.QLabel(self)
       self.GridLayout.addWidget(self.label2, a, 1, 1, 1) 
       self.label2.setFont(self.font)

       self.pushButton = QtGui.QPushButton(self)
       self.GridLayout.addWidget(self.pushButton, b, 0, 2, 1) 
       self.pushButton.setText(QtGui.QApplication.translate("Form", "Υπότροπος", None, QtGui.QApplication.UnicodeUTF8))
       self.pushButton.setSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Expanding) 

       self.pushButton2 = QtGui.QPushButton(self)
       self.GridLayout.addWidget(self.pushButton2, b, 1, 2, 1) 
       self.pushButton2.setText(QtGui.QApplication.translate("Form", "Αναβολή", None, QtGui.QApplication.UnicodeUTF8))
       self.pushButton2.setSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Expanding) 

       self.pushButton3 = QtGui.QPushButton(self)
       self.GridLayout.addWidget(self.pushButton3, c, 1, 2, 1) 
       self.pushButton3.setText(QtGui.QApplication.translate("Form", "Ακύρωση", None, QtGui.QApplication.UnicodeUTF8))
       self.pushButton3.setSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Expanding) 
       self.pushButton3.clicked.connect(self.reject)

我想要实现的是,当我用不同的值多次调用 self.buttons 时, 当达到对话框的最大尺寸时,停止放大,而是放置滚动条,如果你明白我在说什么......我该怎么做?

【问题讨论】:

    标签: python layout dialog pyqt scrollbar


    【解决方案1】:

    这是一个使用QScrollArea的示例:

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    
    from PyQt4 import QtCore, QtGui
    
    class myDialog(QtGui.QDialog):
        _buttons = 0
    
        def __init__(self, parent=None):
            super(myDialog, self).__init__(parent)
    
            self.pushButton = QtGui.QPushButton(self)
            self.pushButton.setText(QtGui.QApplication.translate("self", "Add Button!", None, QtGui.QApplication.UnicodeUTF8))
            self.pushButton.clicked.connect(self.on_pushButton_clicked)
    
            self.scrollArea = QtGui.QScrollArea(self)
            self.scrollArea.setWidgetResizable(True)
            self.scrollAreaWidgetContents = QtGui.QWidget(self.scrollArea)
            self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 380, 247))
            self.scrollArea.setWidget(self.scrollAreaWidgetContents)
    
            self.verticalLayout = QtGui.QVBoxLayout(self)
            self.verticalLayout.addWidget(self.pushButton)
            self.verticalLayout.addWidget(self.scrollArea)
    
            self.verticalLayoutScroll = QtGui.QVBoxLayout(self.scrollAreaWidgetContents)
    
        @QtCore.pyqtSlot()
        def on_pushButton_clicked(self):
            self._buttons  += 1
            pustButtonName = u"Button {0}".format(self._buttons)
    
            pushButton = QtGui.QPushButton(self.scrollAreaWidgetContents)
            pushButton.setText(pustButtonName)
    
            self.verticalLayoutScroll.addWidget(pushButton)
    
    
    if __name__ == "__main__":
        import sys
    
        app = QtGui.QApplication(sys.argv)
        app.setApplicationName('myDialog')
    
        main = myDialog()
        main.show()
    
        sys.exit(app.exec_())
    

    【讨论】:

    • 谢谢,还有一件事:如何在小部件之间放置分隔符?
    • @Antoni4040 加入这个chat,我会帮你的
    • 我该如何处理这个聊天室(抱歉,菜鸟问题...XD)?
    • @Antoni4040 登录,我给你留言了
    • 谢谢你——我发现的第一个真正有效的例子!
    猜你喜欢
    • 2021-03-29
    • 2011-07-30
    • 2011-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    相关资源
    最近更新 更多