2011-12-19 57 views
0

我遇到了展示框架的pyside腳本,演示了按鈕和框佈局的pyside腳本。如何將pyside框架與按鈕佈局相結合

我做了一個原始的企圖兩個腳本合併,但我不能得到以下兩個功能同時運行:run_button_app(真)和run_frame_app(真)

功能之一使用此語句:app_frame = QApplication([]) 另一個使用此語句:app1 = QtGui.QApplication(sys.argv) 我使用的教程不包含解釋這些內容的材料。我已經訂購了一本關於Pyside和PyQt編程的書,但尚未推出。我試圖弄清楚如何使用框架(例如,把幾個框架放在窗口中,並把我的按鈕放在其中一個框架中),但我一直沒能弄清楚這一點但是,也不知道如何獲取有關如何使用框架的信息(example?tutorial?)。

我是一個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) 

回答

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,提供parent參數__init__功能,因此它可以用在其他地方。

要了解更多信息,請嘗試zetcode tutorial,PySide documentation