我的代码遇到麻烦,无法将 entry2 和 btn2 移到 entry1 和 btn1 < / em>,尝试时会在我的代码中造成混乱。 我使用PyQt5进行编程。如果您引导我完成此任务,我将不胜感激。
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Window(QtWidgets.QMainWindow):
def __init__(self):
super(Window, self).__init__()
V = QtWidgets.QApplication.desktop().screenGeometry()
h, w, x, y = V.height(), V.width(), 1000, 600
self.setGeometry(h/4, w/20, x, y)
self.setFixedSize(x, y)
self.setWindowTitle('Main Window')
centralWidget = QtWidgets.QWidget()
self.setCentralWidget(centralWidget)
self.grid = QtWidgets.QGridLayout(centralWidget)
OpenFile = QtWidgets.QAction('Open', self)
OpenFile.setShortcut('Ctrl+O')
OpenFile.setStatusTip('Open Restore File...')
self.statusBar()
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('File')
fileMenu.addAction(OpenFile)
self.home()
def home(self):
self.tools_in_home()
self.show()
def tools_in_home(self):
self.groupBox0 = QtWidgets.QGroupBox('Test 1')
self.groupBox0.setFixedSize(400, 200)
hBoxLayout1 = QtWidgets.QHBoxLayout()
entry1 = QtWidgets.QLineEdit('Enter Value1...')
entry2 = QtWidgets.QLineEdit('Enter Valu2...')
hBoxLayout1.addWidget(entry1)
hBoxLayout1.addWidget(entry2)
btn1 = QtWidgets.QPushButton('Submit1', self)
btn2 = QtWidgets.QPushButton('Submit2', self)
hBoxLayout1.addWidget(btn1)
hBoxLayout1.addWidget(btn2)
self.groupBox0.setLayout(hBoxLayout1)
self.groupBox1 = QtWidgets.QGroupBox('Test 2')
self.groupBox1.setFixedSize(400, 200)
self.groupBox2 = QtWidgets.QGroupBox('Test 3')
self.groupBox2.setFixedSize(400, 200)
self.groupBox3 = QtWidgets.QGroupBox('Test 4')
self.groupBox3.setFixedSize(400, 200)
self.grid.addWidget(self.groupBox0, 0, 0)
self.grid.addWidget(self.groupBox1, 2, 0)
self.grid.addWidget(self.groupBox2, 0, 2)
self.grid.addWidget(self.groupBox3, 2, 2)
def run():
app = QtWidgets.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())
if __name__ == '__main__':
run()
答案 0 :(得分:0)
使用addWidget()
时,您必须按顺序进行操作,即先输入entry1,然后输入btn1,再输入entry2,最后输入btn2。
def tools_in_home(self):
self.groupBox0 = QtWidgets.QGroupBox('Test 1')
self.groupBox0.setFixedSize(400, 200)
hBoxLayout1 = QtWidgets.QHBoxLayout()
entry1 = QtWidgets.QLineEdit('Enter Value1...')
entry2 = QtWidgets.QLineEdit('Enter Valu2...')
btn1 = QtWidgets.QPushButton('Submit1')
btn2 = QtWidgets.QPushButton('Submit2')
hBoxLayout1.addWidget(entry1)
hBoxLayout1.addWidget(btn1)
hBoxLayout1.addWidget(entry2)
hBoxLayout1.addWidget(btn2)
self.groupBox0.setLayout(hBoxLayout1)
# ...
更新:
如果要使用表单结构,请使用QFormLayout。
def tools_in_home(self):
self.groupBox0 = QtWidgets.QGroupBox('Test 1')
self.groupBox0.setFixedSize(400, 200)
lay = QtWidgets.QFormLayout()
entry1 = QtWidgets.QLineEdit('Enter Value1...')
entry2 = QtWidgets.QLineEdit('Enter Valu2...')
btn1 = QtWidgets.QPushButton('Submit1')
btn2 = QtWidgets.QPushButton('Submit2')
lay.addRow(entry1, btn1)
lay.addRow(entry2, btn2)
self.groupBox0.setLayout(lay)
# ...
或QGridLayout:
def tools_in_home(self):
self.groupBox0 = QtWidgets.QGroupBox('Test 1')
self.groupBox0.setFixedSize(400, 200)
lay = QtWidgets.QGridLayout()
entry1 = QtWidgets.QLineEdit('Enter Value1...')
entry2 = QtWidgets.QLineEdit('Enter Valu2...')
btn1 = QtWidgets.QPushButton('Submit1')
btn2 = QtWidgets.QPushButton('Submit2')
lay.addWidget(entry1, 0, 0)
lay.addWidget(btn1, 0, 1)
lay.addWidget(entry2, 1, 0)
lay.addWidget(btn2, 1, 1)
self.groupBox0.setLayout(lay)