Python Qt样式的QRadioButton标签?
有没有一种方法可以设置QRadioButton标签的样式?具体来说,我想将标签从以下位置移动到顶部: 为此:Python Qt样式的QRadioButton标签?,python,pyqt,qradiobutton,Python,Pyqt,Qradiobutton,有没有一种方法可以设置QRadioButton标签的样式?具体来说,我想将标签从以下位置移动到顶部: 为此: 后者已经在tkinter中完成。您可以通过在QHBoxLayout中使用QVBoxLayout和QLabel来实现这一点 from PyQt5.QtWidgets import ( QLabel, QRadioButton, QVBoxLayout, QHBoxLayout, QApplication, QWidget ) import sys class Wind
后者已经在tkinter中完成。您可以通过在
QHBoxLayout
中使用QVBoxLayout
和QLabel
来实现这一点
from PyQt5.QtWidgets import (
QLabel, QRadioButton, QVBoxLayout, QHBoxLayout,
QApplication, QWidget
)
import sys
class Window(QWidget):
def __init__(self):
QWidget.__init__(self)
self.layout = QHBoxLayout()
self.setLayout(self.layout)
self.vlayout1 = QVBoxLayout()
self.label1 = QLabel("HW")
self.radiobutton1 = QRadioButton()
self.radiobutton1.setChecked(True)
self.radiobutton1.toggled.connect(self.onClicked)
self.vlayout1.addWidget(self.label1)
self.vlayout1.addWidget(self.radiobutton1)
self.layout.addLayout(self.vlayout1)
self.vlayout2 = QVBoxLayout()
self.label2 = QLabel("SW")
self.radiobutton2 = QRadioButton()
self.radiobutton2.toggled.connect(self.onClicked)
self.vlayout2.addWidget(self.label2)
self.vlayout2.addWidget(self.radiobutton2)
self.layout.addLayout(self.vlayout2)
def onClicked(self):
radioButton = self.sender()
if radioButton.isChecked():
print("Radio button clicked")
if __name__ == '__main__':
app = QApplication(sys.argv)
screen = Window()
screen.show()
sys.exit(app.exec_())
请原谅我挑三拣四,但我最初并没有这样做,因为即使单选按钮上没有贴标签,您也可以看到它们仍然为单选按钮分配了一个空白,导致所有内容都偏离中心。完全重新定位此边距可以修复此问题,但也可以通过样式修复来消除此问题。IIRC一个负的右边距对此不起作用。没关系,@Laif。我会考虑使用和/或。这会有什么帮助?@ Laif给按钮一个固定的宽度,这样它不会水平扩展:<代码>按钮。StEngIdEdStand(按钮。siZHeTin()。Head())< /Cal>。@ LAIF对我来说很好。当我对上面显示的示例代码进行建议的更改时,它看起来与问题中显示的第二幅图像完全相同。