Marc B. Hankin
Marc B. Hankin

Reputation: 771

How to combine a pyside frame with a button layout

I came across a pyside script demonstrating a frame, and pyside script demonstrating buttons and box layout.

I made a primitive attempt to combine the two scripts, but I can't get both of the following functions to work at the same time: run_button_app(True) and run_frame_app(True)

One of the functions uses this statement: app_frame = QApplication([]) and the other uses this statement: app1 = QtGui.QApplication(sys.argv) The tutorials I used did not include material explaining these things. I've ordered a book on Pyside and PyQt programming, but it hasn't arrived yet.

I'm trying to figure out how to use the frame (e.g., to put a couple of frames in window, and put my buttons in one of the frames), but I haven't been able to figure that out yet, nor where to get info (example? tutorial?) about how to use the frame.

I am a pyside and python newbie. Any tips (e.g., links to relevant tutorials) would be much appreciated.

Thanks, Marc

    """
    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)

Upvotes: 0

Views: 3726

Answers (1)

reclosedev
reclosedev

Reputation: 9522

You can't run two QAplication instances at once, here some information in this tutorial.

I dont fully understand what are you trying to do, but if you want to add Example widget (with buttons) to FrameTester, you need to create some layout in FrameTester and add Example instance to this layout (there are many tutorials on it).

If you want Example widget to have frame, you need to subclass QFrame:

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

Tip for the future, when you defining some Widget, provide parent argument to __init__ function, so it can be used in other places.

To learn more try zetcode tutorial, PySide documentation

Upvotes: 1

Related Questions