【问题标题】:How to combine a pyside frame with a button layout如何将 pyside 框架与按钮布局相结合
【发布时间】:2012-01-23 19:05:41
【问题描述】:

我遇到了一个演示框架的 pyside 脚本,以及演示按钮和框布局的 pyside 脚本。

我做了一个原始的尝试来组合这两个脚本,但我无法让以下两个函数同时工作:run_button_app(True) 和 run_frame_app(True)

其中一个函数使用这个语句:app_frame = QApplication([]) 另一个使用这个语句:app1 = QtGui.QApplication(sys.argv) 我使用的教程不包括解释这些事情的材料。我已经订购了一本关于 Pyside 和 PyQt 编程的书,但还没到。

我正在尝试弄清楚如何使用框架(例如,在窗口中放置几个​​框架,并将我的按钮放在其中一个框架中),但我还没有弄清楚这一点,也无法从哪里获得有关如何使用框架的信息(例如?教程?)。

我是 pyside 和 python 新手。 任何提示(例如,相关教程的链接)将不胜感激。

谢谢, 马克

    """
    Cannibalized by Marc from 
        http://zetcode.com/gui/pysidetutorial/
        ZetCode PySide tutorial  author: Jan Bodnar    website: zetcode.com 
            and    
        # explore QFrame()   #http://www.daniweb.com/software-development/python/threads/366177
    """

    import sys
    from PySide.QtCore import *
    from PySide.QtGui import *
    from PySide import QtGui

    initiate_app = False

    class Example(QtGui.QWidget):

        def __init__(self):
            super(Example, self).__init__()
            # The following 4 lines are from "Window_with_frame.py"
            # setGeometry(x_pos, y_pos, width, height)
            # self.setGeometry(100, 150, width, height)
            # self.setWindowTitle(title)
            # self.make_frame()

            self.initUI()

        def initUI(self):

            okButton = QtGui.QPushButton("OKh1_1")
            cancelButton = QtGui.QPushButton("Cancel1_2")
            ThirdButton = QtGui.QPushButton("Third1_3")
            hbox2_1Button = QtGui.QPushButton("hbox2_Btn1")
            hbox2_2Button = QtGui.QPushButton("hbox2_Btn2")
            hbox3_1Button = QtGui.QPushButton("hbox3_Btn1")
            hbox3_2Button = QtGui.QPushButton("hbox3_Btn2")
            Vbox1Button = QtGui.QPushButton("Vbox1Button")
            Vbox2Button = QtGui.QPushButton("Vbox2Button")
            NewQqPtA1Button = QtGui.QPushButton("NewQqPtA1")
            NewQqPtB2Button = QtGui.QPushButton("NewQqPtB2")

            hbox1 = QtGui.QHBoxLayout()
            hbox1.addStretch(1)
            hbox1.addWidget(okButton)
            hbox1.addWidget(cancelButton)
            hbox1.addWidget(ThirdButton)

            hbox2 = QtGui.QHBoxLayout()
            hbox2.addStretch(1)
            hbox2.addWidget(hbox2_1Button)
            hbox2.addWidget(hbox2_2Button)
            hbox1.addLayout(hbox2)

            hbox3 = QtGui.QHBoxLayout()
            hbox3.addStretch(1)        
            hbox3.addWidget(hbox3_1Button)
            hbox3.addWidget(hbox3_2Button)

            vbox1 = QtGui.QVBoxLayout()
            vbox1.addStretch(1)
            vbox1.addWidget(Vbox1Button)
            vbox1.addLayout(hbox1)
            #vbox1.addLayout(hbox2)
            vbox1.addLayout(hbox3)

            vbox2 = QtGui.QVBoxLayout()
            vbox2.addStretch(1)
            vbox2.addWidget(Vbox2Button)
            vbox2.addLayout(vbox1)

            self.setLayout(vbox2)    

            self.setGeometry(300, 300, 300, 150)
            self.setWindowTitle('Buttons')
            #self.make_frame()
            self.show()

    # The class is from "Window_with_frame.py"

    class FrameTester(QWidget):
        def __init__(self, title, width, height, parent=None):
            # create the window (this will be instance self)
            QWidget.__init__(self, parent)
            # setGeometry(x_pos, y_pos, width, height)
            self.setGeometry(100, 150, width, height)
            self.setWindowTitle(title)
            self.make_frame()

        def make_frame(self):
            frame = QFrame(self)
            frame.setLineWidth(3)
            frame.setFrameStyle(QFrame.Box|QFrame.Sunken)        
            # this will not have the effect you hope for
            #frame.setFrameRect(QRect(10, 10, 20, 20))       
            layout = QBoxLayout(QBoxLayout.LeftToRight)
            layout.addWidget(frame)        
            self.setLayout(layout)



    def run_frame_app(initiate_app = False):        
        # create the Qt Application
        if initiate_app == True:        
            app_frame = QApplication([])        
            title = "Window"
            width = 800
            height = 600
            tester = FrameTester(title, width, height)
            tester.show()        
            # run the main Qt event loop
            app_frame.exec_()


    def run_button_app(initiate_app = False):   
        # create the Qt Application
        if initiate_app == True:        
            app1 = QtGui.QApplication(sys.argv)
            ex = Example()
            run_frame_app()
            sys.exit(app1.exec_())    



    if __name__ == '__main__':
        #run_button_app(True)
        run_frame_app(True)

【问题讨论】:

    标签: python layout button pyqt pyside


    【解决方案1】:

    您不能同时运行两个 QAplication 实例, 这里有一些信息在这个tutorial

    我不完全理解您要做什么,但是如果您想将Example 小部件(带按钮)添加到FrameTester,您需要在FrameTester 中创建一些布局并将Example 实例添加到这个布局(上面有很多教程)。

    如果你想让Example 小部件有框架,你需要继承QFrame

    class Example(QFrame):
        def __init__(self, parent=None):
            super(Example, self).__init__(parent)
            self.setLineWidth(3)
            self.setFrameStyle(QFrame.Box|QFrame.Sunken)  
    # ...    
    

    提示以后,当你定义一些Widget时,给__init__函数提供parent参数,这样就可以在其他地方使用了。

    要了解更多信息,请尝试zetcode tutorialPySide documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-22
      相关资源
      最近更新 更多