I am trying to create a GUI using PyQt5 that will simulate some data and display some results, but since this is the first time I build a GUI with PyQt5, I find difficulty to align horizontally two radio buttons a the top left corner of the GUI.
I tried the following code, but it gave me the following error:
Traceback (most recent call last):
File "example6.py", line 30, in <module>
screen = GroupBox()
File "example6.py", line 19, in __init__
vbox.setGeometry(10,10,100,100)
TypeError: setGeometry(self, QRect): argument 1 has unexpected type 'int'
This is the code I tried:
from PyQt5.QtWidgets import *
import sys
class GroupBox(QWidget):
def __init__(self):
QWidget.__init__(self)
self.setGeometry(20,20,900,700)
self.setWindowTitle("Elevator Group Control System Simulator")
layout = QGridLayout()
self.setLayout(layout)
groupbox = QGroupBox("Algorithm to use")
groupbox.setCheckable(False)
layout.addWidget(groupbox)
hbox = QHBoxLayout()
hbox.setGeometry(10,10,100,100)
groupbox.setLayout(hbox)
radiobutton = QRadioButton("Good Picker")
hbox.addWidget(radiobutton)
radiobutton = QRadioButton("Naive Picker")
hbox.addWidget(radiobutton)
app = QApplication(sys.argv)
screen = GroupBox()
screen.show()
sys.exit(app.exec_())
EDIT: Even after applying the suggestions in the first answer, it still gives me a group of radio buttons which covers the whole GUI:
Resulting GUI
EDIT 2: After trying the suggestions in the last answer, I obtained this:
Second result
setGeometry()
expects a QRect
as a parameter. Replace hbox.setGeometry(10,10,100,100)
with:
hbox.setGeometry(QRect(10,10,100,100))
Note that this will require adding from PyQt5.QtCore import *
to your imports.
As indicated in a previous answer, the first error is because setGeometry() expects a QRect.
If you want to align the widgets to the topleft you have to set a stretch on the right side to be compressed to the left side, and then point to the alignment of the widgets in the layout as Qt::AlignTop.
from PyQt5 import QtCore, QtWidgets
class GroupBox(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setGeometry(QtCore.QRect(20, 20, 900, 700))
self.setWindowTitle("Elevator Group Control System Simulator")
layout = QtWidgets.QGridLayout(self)
groupbox = QtWidgets.QGroupBox("Algorithm to use", checkable=False)
layout.addWidget(groupbox)
hbox = QtWidgets.QHBoxLayout()
groupbox.setLayout(hbox)
good_radiobutton = QtWidgets.QRadioButton("Good Picker")
naive_radiobutton = QtWidgets.QRadioButton("Naive Picker")
hbox.addWidget(good_radiobutton, alignment=QtCore.Qt.AlignTop)
hbox.addWidget(naive_radiobutton, alignment=QtCore.Qt.AlignTop)
hbox.addStretch()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
screen = GroupBox()
screen.show()
sys.exit(app.exec_())
Update:
from PyQt5 import QtCore, QtWidgets
class GroupBox(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setGeometry(QtCore.QRect(20, 20, 900, 700))
self.setWindowTitle("Elevator Group Control System Simulator")
layout = QtWidgets.QGridLayout(self)
groupbox = QtWidgets.QGroupBox("Algorithm to use", checkable=False)
layout.addWidget(groupbox)
hbox = QtWidgets.QHBoxLayout()
groupbox.setLayout(hbox)
good_radiobutton = QtWidgets.QRadioButton("Good Picker")
naive_radiobutton = QtWidgets.QRadioButton("Naive Picker")
hbox.addWidget(good_radiobutton, alignment=QtCore.Qt.AlignTop)
hbox.addWidget(naive_radiobutton, alignment=QtCore.Qt.AlignTop)
hbox.addStretch()
layout.setColumnStretch(1, 1)
layout.setRowStretch(1, 1)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
screen = GroupBox()
screen.show()
sys.exit(app.exec_())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.