如何添加使用 PyQt 移动 Window 大小时移动的 TextEdit、标签和按钮?

How to add TextEdit, Labels and Buttons that move when Window Size is moved using PyQt?

我是 PyQt 的新手,但在过去的几天里,我试图创建一个 GUI,它有标签、TextEdit 和按钮,当 Window 大小移动(最小化或放大)时,它们会移动,我试过这样做但按钮卡在左上角,而标签和 TextEdit 完全没有显示在表单上,​​请帮忙。这是我的代码片段

import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow, QtGui.QWidget):

`   def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 500, 300)
        self.setWindowTitle("PTE")
        self.setWindowIcon(QtGui.QIcon('.png'))
        self.center()

        # Adding Menu to the GUI
        quitAction = QtGui.QAction(QtGui.QIcon('exit.png'), "&Quit", self)
        quitAction.setShortcut("Ctrl+Q")  
        quitAction.setStatusTip('Exit Application')
        quitAction.triggered.connect(self.close_application)

        undoAction = QtGui.QAction(QtGui.QIcon('undo.png'), "&Undo", self)
        undoAction.setShortcut("Ctrl+Z")        
        undoAction.triggered.connect(self.close_application)

        aboutAction = QtGui.QAction("&About PTE...", self)


        self.statusBar()

        #Actual Main Menu with options
        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu('&File')        
        fileMenu.addAction(quitAction)

        fileMenu = mainMenu.addMenu('&Edit')
        fileMenu.addAction(undoAction)

        fileMenu = mainMenu.addMenu('&Help')
        fileMenu.addAction(aboutAction)

        self.home()


    #Centering Window on the screen
    def center(self):
        gui = self.frameGeometry()
        cp = QtGui.QDesktopWidget().availableGeometry().center()
        gui.moveCenter(cp)
        self.move(gui.topLeft())


     def home(self):
         #Buttons
        qbtn = QtGui.QPushButton('Quit', self)
        qbtn.clicked.connect(self.close_application)

        rbtn = QtGui.QPushButton("Run", self)
        rbtn.clicked.connect(self.close_application)

        hbox = QtGui.QHBoxLayout()
        hbox.addStretch(1)
        hbox.addWidget(qbtn)
        hbox.addWidget(rbtn)

        vbox = QtGui.QVBoxLayout()
        vbox.addStretch(1)
        vbox.addLayout(hbox)

        self.setLayout(vbox) 


        self.show()

        #Labels and TextBox 
        Intervals = QtGui.QLabel('Number of intervals (0<=20) :')
        Timesteps = QtGui.QLabel('Number of time steps (<=1000) : ')

        IntervalsEdit = QtGui.QLineEdit()
        TimestepsEdit = QtGui.QLineEdit()


        grid = QtGui.QGridLayout()
        grid.setSpacing(2)

        grid.addWidget(Intervals, 1, 0)
        grid.addWidget(IntervalsEdit, 1, 1)

        grid.addWidget(Timesteps, 2, 0)
        grid.addWidget(TimestepsEdit, 2, 1)


        self.setLayout(grid)

        self.show()

    #What to display when the app is closed    
     def close_application(self):
         #Popup message before closing the application in Binary
         choice = QtGui.QMessageBox.question(self, 'Message',"Are you sure you want to exit?", 
                                        QtGui.QMessageBox.Yes | QtGui.QMessageBox.No )

         if choice == QtGui.QMessageBox.Yes:
              print(" Until Next Time")
              sys.exit()
         else:
             pass

def run():        
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())

    run() 

嘿,我解决了您的代码中的问题。在我的修复中,我将 QGridLayout 添加到 QHBoxLayout。如果你想在另一个地方使用网格,你可能需要将两个布局嵌套在第三个布局中。

请注意,当您只能设置 1 种布局时,您将 setLayout 用于 grid 用于 vbox。您还调用了 show() 两次,这是您不需要的。您还将 QVBoxLayout 添加到 hbox 但不添加任何小部件。事实上,这是没有用的。如果您希望 hboxgrid 垂直对齐,则需要 vbox.addLayout(grid)self.setLayout(vbox)

import sys

from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow, QtGui.QWidget):

`   def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 500, 300)
        self.setWindowTitle("PTE")
        self.setWindowIcon(QtGui.QIcon('.png'))
        self.center()

        # Adding Menu to the GUI
        quitAction = QtGui.QAction(QtGui.QIcon('exit.png'), "&Quit", self)
        quitAction.setShortcut("Ctrl+Q")  
        quitAction.setStatusTip('Exit Application')
        quitAction.triggered.connect(self.close_application)

        undoAction = QtGui.QAction(QtGui.QIcon('undo.png'), "&Undo", self)
        undoAction.setShortcut("Ctrl+Z")        
        undoAction.triggered.connect(self.close_application)

        aboutAction = QtGui.QAction("&About PTE...", self)


        self.statusBar()

        #Actual Main Menu with options
        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu('&File')        
        fileMenu.addAction(quitAction)

        fileMenu = mainMenu.addMenu('&Edit')
        fileMenu.addAction(undoAction)

        fileMenu = mainMenu.addMenu('&Help')
        fileMenu.addAction(aboutAction)

        self.home()


    #Centering Window on the screen
    def center(self):
        gui = self.frameGeometry()
        cp = QtGui.QDesktopWidget().availableGeometry().center()
        gui.moveCenter(cp)
        self.move(gui.topLeft())


     def home(self):
         #Buttons
        qbtn = QtGui.QPushButton('Quit', self)
        qbtn.clicked.connect(self.close_application)

        rbtn = QtGui.QPushButton("Run", self)
        rbtn.clicked.connect(self.close_application)

        hbox = QtGui.QHBoxLayout()
        hbox.addStretch(1)
        hbox.addWidget(qbtn)
        hbox.addWidget(rbtn)

        vbox = QtGui.QVBoxLayout()
        vbox.addStretch(1)
        vbox.addLayout(hbox)



        #self.show() #this only needs to be called once

        #Labels and TextBox 
        Intervals = QtGui.QLabel('Number of intervals (0<=20) :')
        Timesteps = QtGui.QLabel('Number of time steps (<=1000) : ')

        IntervalsEdit = QtGui.QLineEdit()
        TimestepsEdit = QtGui.QLineEdit()


        grid = QtGui.QGridLayout()
        grid.setSpacing(2)

        grid.addWidget(Intervals, 1, 0)
        grid.addWidget(IntervalsEdit, 1, 1)

        grid.addWidget(Timesteps, 2, 0)
        grid.addWidget(TimestepsEdit, 2, 1)

        hbox.addLayout(grid) #this will add the grid to the horizontal layout.
        #if you want it to be vertcally aligned change this to vbox.addLayout(grid)...

        #... and this to self.setLayout(vbox)
        self.setLayout(hbox)

        self.show()

    #What to display when the app is closed    
     def close_application(self):
         #Popup message before closing the application in Binary
         choice = QtGui.QMessageBox.question(self, 'Message',"Are you sure you want to exit?", 
                                        QtGui.QMessageBox.Yes | QtGui.QMessageBox.No )

         if choice == QtGui.QMessageBox.Yes:
              print(" Until Next Time")
              sys.exit()
         else:
             pass