Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyQt4中标签的自动调整大小_Python_Python 3.x_Python 2.7_Pyqt_Pyqt4 - Fatal编程技术网

Python PyQt4中标签的自动调整大小

Python PyQt4中标签的自动调整大小,python,python-3.x,python-2.7,pyqt,pyqt4,Python,Python 3.x,Python 2.7,Pyqt,Pyqt4,我在PyQt4中添加了一个包含图像的标签。此图像在未最大化时完全适合窗口。我的目标是,标签应该自动调整大小,并完全适合窗口,即使最大化。我的代码如下: class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName(_fromUtf8("MainWindow")) MainWindow.resize(1047, 600) self.centra

我在PyQt4中添加了一个包含图像的标签。此图像在未最大化时完全适合窗口。我的目标是,标签应该自动调整大小,并完全适合窗口,即使最大化。我的代码如下:

class Ui_MainWindow(object):
   def setupUi(self, MainWindow):
       MainWindow.setObjectName(_fromUtf8("MainWindow"))
       MainWindow.resize(1047, 600)
       self.centralwidget = QtGui.QWidget(MainWindow)
       self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
       self.label = QtGui.QLabel(self.centralwidget)
       self.label.setGeometry(QtCore.QRect(0, 0, 1081, 551))
       self.label.setText(_fromUtf8(""))
       self.label.setPixmap(QtGui.QPixmap(_fromUtf8("Capture.PNG")))
       self.label.setObjectName(_fromUtf8("label"))
   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_())

请告诉我如何修改代码?

您必须执行以下操作:

  • centralwidget
    设置为
    QMainWindow
    centralwidget
  • 使用布局将
    QLabel
    放置在
    centralwidget
  • 启用
    QLabel
    ScaledContents
    属性

class Ui\u主窗口(对象):
def设置UI(自我,主窗口):
self.centralwidget=QtGui.QWidget(主窗口)

main window.setCentralWidget(self.centralwidget)#非常感谢。我在标签上有一些按钮,与标签上的某些点有关。现在,当标签展开时,这些按钮保留了它们的初始坐标,并且没有随标签变化。我们是否有可能确保这些按钮也随着标签的变化而变化@MehulBhatia你希望你的职位如何改变?提供一个新问题,我可以帮你。非常肯定@Eylansec先生。我将创建一个新问题!!
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        self.centralwidget = QtGui.QWidget(MainWindow)
        MainWindow.setCentralWidget(self.centralwidget) # <--
        lay = QtGui.QVBoxLayout(self.centralwidget)
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setPixmap(QtGui.QPixmap("Capture.PNG"))
        self.label.setScaledContents(True) # <--
        lay.addWidget(self.label) # <--