如何使用PyQt添加移动窗口大小时移动的TextEdit,Labels和Buttons?

时间:2016-06-07 21:41:03

标签: pyqt pyqt4 pyqt5

我是PyQt的新手,但在过去的几天里,我试图创建一个GUI,它有标签,TextEdit和按钮,当窗口大小移动(最小化或放大)时移动,我尝试这样做,但按钮卡在左上角,而标签和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() 

1 个答案:

答案 0 :(得分:0)

嘿,我修复了你的代码问题。在我的修复程序中,我将QGridLayout添加到QHBoxLayout。如果您希望网格位于其他位置,则可能需要将这两种布局嵌套在第3种布局中。

请注意,当您只能设置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