Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
515 views
in Technique[技术] by (71.8m points)

python - How to keep Push Buttons constant in relative to change of Label Size in PyQt4

I have an image as my label in a PyQt4 Program. This program also contains some push buttons. Now, when I maximize my window, though the label gets expanded but the push button retain their place. I want that the push buttons should maintain their relative position with respect to the label, even when maximized.

My code is as follows:

class Ui_MainWindow(object):
   def setupUi(self, MainWindow):

       self.centralwidget = QtGui.QWidget(MainWindow)
       MainWindow.setCentralWidget(self.centralwidget)
       lay = QtGui.QVBoxLayout(self.centralwidget)
       self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
       self.label = QtGui.QLabel(self.centralwidget)
       self.label.setText(_fromUtf8(""))
       self.label.setPixmap(QtGui.QPixmap(_fromUtf8("Capture.PNG")))
       self.label.setObjectName(_fromUtf8("label"))
       self.label.setScaledContents(True)
       lay.addWidget(self.label)
       self.Langdon = QtGui.QPushButton(self.centralwidget)
       self.Langdon.setGeometry(QtCore.QRect(290, 90, 75, 23))
       self.Langdon.setObjectName(_fromUtf8("Langdon"))     
       self.ChainLakes = QtGui.QPushButton(self.centralwidget)
       self.ChainLakes.setGeometry(QtCore.QRect(50, 290, 85, 23))
       self.ChainLakes.setObjectName(_fromUtf8("ChainLakes"))

   if __name__ == "__main__":
      import sys
      app = QtGui.QApplication(sys.argv)
      MainWindow = QtGui.QMainWindow()
      ui = Ui_MainWindow()
      ui.setupUi(MainWindow)
      MainWindow.show()
      sys.exit(app.exec_())

How shall I modify my code??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're using absolute positioning for the buttons, but relative for the label (because it is in the layout). Since the buttons are positioned absolutely, you're responsible for moving them around yourself whenever the container size (your main window or central widget) changes. OTOH if you position them using a layout, the layout then moves them around as needed (as with the label).

I highly recommend you find a layout-based solution which would suit your needs (they're very flexible but can sometimes take a bit of fiddling). Barring that, you'll need to subclass the container (your centralWidget QWidget) and re-implement the QWidget::resizeEvent() handler. Inside that handler you would move the absolutely positioned elements (the buttons) to a new position based on current size of the container and current size/position of the label.

I'm not handy enough with PyQt to give a concrete example (sorry), but I did provide a similar answer to a C++ question which may be useful: QPushButton alignment on top another widget


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...