2011-12-19 20 views
0

フレームと、ボタンとボックスのレイアウトを示すpysideスクリプトを示すpysideスクリプトが出ました。ボタンのレイアウトとフレームの組み合わせ方法

私は2つのスクリプトを組み合わせるための原始的な試みを試みたが、私は以下の機能の両方を同時に動作させることはできません。run_button_app(真)とrun_frame_app(真)機能の

ワンapp1 = QtGui.QApplication(sys.argv) 私が使ったチュートリアルでは、これらのことを説明する資料は含まれていませんでした。私はPysideとPyQtプログラミングに関する本を注文しましたが、まだ到着していません。

私はフレームを使用する方法を理解しようとしています(例えば、いくつかのフレームをウィンドウに置き、フレームの1つにボタンを配置するなど)。しかし、私はそれを理解することができませんでしたまだ、どのようにフレームを使用するかについての情報(例?チュートリアル?)を入手する場所もありません。

私はpysideとpython newbieです。 ヒント(例:関連するチュートリアルへのリンク)は非常に高く評価されます。

おかげで、 マルク

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

あなたはこのtutorialここで、一度にいくつかの情報を を2つのQAplicationのインスタンスを実行することはできません。

私は完全に(多くのチュートリアルがあり、あなたがやろうとしているものを理解していますが、 FrameTesterExampleウィジェット(ボタン付き)を追加したい場合は、あなたが 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

関連する問題