Qt Style QRadioButton 标签?
Qt Style QRadioButton label?
有没有办法设置 QRadioButton 标签的样式?具体来说,我想将标签移到顶部,从这里开始:
对此:
后者已在 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_())
有没有办法设置 QRadioButton 标签的样式?具体来说,我想将标签移到顶部,从这里开始:
对此:
后者已在 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_())