如何在最大化窗口时防止QPushButtons改变其位置

时间:2015-08-25 11:36:06

标签: python pyqt4

我正在制作一种图像查看器,在其中我们可以看到加载图像的直方图和hsv。代码按预期运行,除非我最大化窗口,否则所有QPushButtons都会放错位置。

from PyQt4 import QtGui, QtCore
import cv2
import numpy as np
from matplotlib import pyplot as plt

 class Example(QtGui.QWidget):

    def __init__(self):
     super(Example, self).__init__()

     hbox = QtGui.QHBoxLayout(self)
     top = QtGui.QFrame(self)
     top.setFrameShape(QtGui.QFrame.StyledPanel)
     bottomleft = QtGui.QFrame(self)
     bottomleft.setFrameShape(QtGui.QFrame.StyledPanel)
     bottomright = QtGui.QFrame(self)
     bottomright.setFrameShape(QtGui.QFrame.StyledPanel)
     splitter1 = QtGui.QSplitter(QtCore.Qt.Vertical)
     splitter1.addWidget(top)
     splitter2 = QtGui.QSplitter(QtCore.Qt.Horizontal)
     splitter2.addWidget(bottomleft)
     splitter2.addWidget(bottomright)
     splitter1.addWidget(splitter2)
     hbox.addWidget(splitter1)
     splitter1.setSizes([190,220])
     splitter2.setSizes([400,360])
     self.setLayout(hbox)
     QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Cleanlooks'))
     self.setGeometry(600, 120, 990, 850)
     self.setWindowTitle('PIMAD')
     self.setWindowIcon(QtGui.QIcon('imag.jpg')) 
     self.show()
     browseButton = QtGui.QPushButton("Browse")
     browseButton.clicked.connect(self.loadFromFile)
     browseButton.setToolTip('click to <b>Browse</b>')
     histButton = QtGui.QPushButton("")
     histButton.setToolTip('For image <b>Histogram</b> click here')
     histButton.setIcon(QtGui.QIcon('download.jpg'))
     histButton.setIconSize(QtCore.QSize(55,35))
     histButton.clicked.connect(self.loadFromHist)
     hsvButton = QtGui.QPushButton("")
     hsvButton.clicked.connect(self.loadFromHsv)
     hsvButton.setToolTip('For <b>Image HSV </b> click here')
     hsvButton.setIcon(QtGui.QIcon('hsv.jpg'))
     hsvButton.setIconSize(QtCore.QSize(50,35))

     self.lbl= QtGui.QLabel()
     self.lbl.setScaledContents(True)
     bottomleftLayout = QtGui.QHBoxLayout()
     self.lbl.setFixedSize(470, 480)
     self.lbl2 = QtGui.QLabel()
     self.lbl2.setScaledContents(True)
     bottomrightLayout = QtGui.QHBoxLayout()
     self.lbl3 = QtGui.QLabel()
     self.lbl3.setScaledContents(True)
     self.lbl3.setFixedSize(300,250)
     self.lbl3.move(650,05)
     self.lbl3.setParent(top)
     self.image = "C:\New folder (2)\logo.jpeg"
     self.pix = QtGui.QPixmap(self.image)
     self.lbl3.setPixmap(self.pix)    
     self.lbl3.show()
     topLayout = QtGui.QVBoxLayout()

     self.fileName = "\Users\Public\Pictures\Sample Pictures\lord.jpg"
     self.pixmap = QtGui.QPixmap(self.fileName)
     self.lbl.setPixmap(self.pixmap)

     bottomleftLayout.addWidget(self.lbl)
     bottomleft.setLayout(bottomleftLayout)
     bottomrightLayout.addWidget(self.lbl2)
     bottomright.setLayout(bottomrightLayout)
     topLayout.addWidget(self.lbl3)

     topLayout.addStretch(1)

     top.setLayout(topLayout)
     topLayout.addStretch(1)

     browseButton.setParent(top)
     histButton.setParent(top)
     hsvButton.setParent(top)
     browseButton.move(720,260)
     histButton.move(790,260)
     hsvButton.move(860,260)
     browseButton.resize(60,40)
     histButton.resize(60,40)
     hsvButton.resize(60,40)
     browseButton.show()
     histButton.show()
     hsvButton.show()

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('Example')
    main = Example()
    main.show()    
    app.exec_()

1 个答案:

答案 0 :(得分:0)

您正在使用布局和绝对定位(使用方法move),这有点奇怪。您也不需要使用show()那么多。它应该只在主窗口上调用。

如果要调整窗口大小,绝对定位通常不会很好。这是固定的。小部件将保持在相对于窗口左上角的相同位置。如果窗口变小,则会隐藏一些小部件,如果窗口变大,则不会使用新空间。

布局意味着灵活。您可以在垂直或水平线(QHBoxLayoutQVBoxLayout)或网格(QGridLayout)上排列小部件。布局将填充所有可用空间:它们适用于任何窗口大小。

我建议您按照教程重新开始(一个好的是ZetCode Layout management in PyQt4)。尝试开始简单并逐步在布局中添加更多元素。在每个步骤测试以调整窗口大小以查看它是否按预期工作。