2016-05-17 18 views
1

我在Python中使用PyQt4庫爲我的GUI構建了一個應用程序。我的GUI繪製了一個圖形和上面的一些按鈕。圖表生活在frame_B,圖表上方的按鈕(和LED)住在frame_A在PyQt中擴展QWidgets

enter image description here

我在MainWindow類中創建frame_A(也frame_B):

class CustomMainWindow(QtGui.QMainWindow): 

    def __init__(self): 
     super(CustomMainWindow, self).__init__() 
     # Do some initialization stuff.. 
    '''''' 

    def initLayout(self): 
     # Create frame_A and its corresponding layout 
     frame_A = QtGui.QFrame(self) 
     setCustomSize(frame_A, 380,800) 
     self.layout_A = QtGui.QGridLayout() 
     frame_A.setLayout(self.layout_A) 

     # Create frame_B, and so forth.. 

    '''''' 

    def setButtons(self): 
     # Create btn01.. 
     # Create btn02.. 
     # Create btn03.. 
     # Create btn04.. 
     # Create btn05.. 
     # Create led01.. 
     # Add all buttons to the layout of frame_A: 
     self.layout_A.addWidget(self.btn01, *(0,0)) 
     self.layout_A.addWidget(self.btn02, *(0,1)) 
     self.layout_A.addWidget(self.btn03, *(0,2)) 
     self.layout_A.addWidget(self.btn04, *(0,3)) 
     self.layout_A.addWidget(self.btn05, *(0,4)) 
     self.layout_A.addWidget(self.led01, *(0,5)) 

    '''''' 

'''''' 

的結果非常令人滿意,如上圖所示。當我放大我的窗口(通過簡單的點擊並拖動鼠標),frame_Aframe_B也放大。這很好。我不想要任何其他方式。

但它困擾我frame_A按鈕得到均勻分佈,以填補所有新的空間。我更喜歡這樣的行爲: enter image description here

任何人都有一個聰明的想法來實現這一目標?

回答

1

如果你只想要水平添加小工具,我會建議使用QHBoxLayout而不是QGridLayout。有了這個,你可以使用layout_A.setAlignment(QtCore.Qt.AlignLeft)。不知道這是否也適用於Gridlayout。

+0

工程就像魅力!謝謝先生:-) –

2

add self.layout_A.addStretch()最後一個按鈕後

+0

它不起作用。我得到的錯誤:'AttributeError:'QGridLayout'對象沒有屬性'addStretch()'' –