Python 如何将pyside框架与按钮布局相结合

Python 如何将pyside框架与按钮布局相结合,python,layout,button,pyqt,pyside,Python,Layout,Button,Pyqt,Pyside,我遇到一个pyside脚本演示一个框架,pyside脚本演示按钮和框布局 我尝试将这两个脚本结合起来,但无法同时使用以下两个函数:run_button_app(True)和run_frame_app(True) 其中一个函数使用以下语句:app_frame=QApplication([]) 另一个使用以下语句:app1=QtGui.QApplication(sys.argv) 我使用的教程没有包括解释这些内容的材料。我订了一本关于Pyside和PyQt编程的书,但还没有到 我试图弄清楚如何使用框

我遇到一个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)

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

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

如果希望
Example
小部件具有框架,则需要将
QFrame
子类化:

class Example(QFrame):
    def __init__(self, parent=None):
        super(Example, self).__init__(parent)
        self.setLineWidth(3)
        self.setFrameStyle(QFrame.Box|QFrame.Sunken)  
# ...    
将来的提示:当您定义一些
小部件
时,请为
\uuuuu init\uuuu
函数提供
父项
参数,以便在其他地方使用

要了解更多,请尝试