下面详细讲解一下如何制作Python PyQt5的胶囊状单选按钮。
胶囊状单选按钮
首先要明确的是,胶囊状单选按钮其实是指单选钮拥有圆角而非直角的样式。在PyQt5中,我们可以通过使用QSS样式表来实现此效果。
步骤
- 创建单选钮,使用QSS样式来制作圆角:
radio_button_1 = QRadioButton("Option 1", self)
radio_button_1.setStyleSheet("border-radius: 10px;")
- 创建单选按钮组:
radio_button_group = QButtonGroup(self)
- 将单选钮添加到单选按钮组中,并设置唯一的ID:
radio_button_1 = QRadioButton("Option 1", self)
radio_button_group.addButton(radio_button_1, 1)
- 创建更多的单选钮,重复步骤3。
radio_button_2 = QRadioButton("Option 2", self)
radio_button_group.addButton(radio_button_2, 2)
radio_button_3 = QRadioButton("Option 3", self)
radio_button_group.addButton(radio_button_3, 3)
- 在单选钮组中设置默认选项:
radio_button_group.button(1).setChecked(True)
这将使第一个单选钮默认选中。
- 使用布局来放置单选钮:
layout = QVBoxLayout()
layout.addWidget(radio_button_1)
layout.addWidget(radio_button_2)
layout.addWidget(radio_button_3)
self.setLayout(layout)
示例1:列表中的胶囊状单选按钮
在这个例子中,我们将创建一个包含许多单选钮的列表,并为每个单选钮设置胶囊状样式。
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QRadioButton
from PyQt5.QtCore import Qt
import sys
class AppDemo(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
self.setWindowTitle('PyQt5 Capsule Radio Buttons')
self.setFixedSize(300, 300)
radio_button_group = QButtonGroup(self)
options = ['Option 1', 'Option 2', 'Option 3', 'Option 4', 'Option 5', 'Option 6', 'Option 7', 'Option 8']
layout = QVBoxLayout()
for option in options:
radio_button = QRadioButton(option, self)
radio_button.setStyleSheet("QRadioButton{border-radius:10px;}")
radio_button_group.addButton(radio_button)
layout.addWidget(radio_button)
radio_button_group.button(0).setChecked(True)
self.setLayout(layout)
self.show()
def main():
app = QApplication(sys.argv)
demo = AppDemo()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
示例2:胶囊状单选按钮的自定义控制台
在这个例子中,我们将创建一个具有胶囊状单选钮的自定义控制台以启动Python脚本。
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QRadioButton, QLineEdit, QPushButton
from PyQt5.QtCore import Qt
import os
import sys
class AppDemo(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
# 设置窗口标题和大小
self.setWindowTitle('Custom Console')
self.setFixedSize(400, 100)
# 创建单选钮
python_option = QRadioButton('Python', self)
python_option.setStyleSheet('QRadioButton{border-radius:5px;}')
python_option.setChecked(True)
# 添加单选钮到单选钮组
radio_button_group = QButtonGroup(self)
radio_button_group.addButton(python_option, 1)
# 创建文本框
command_line = QLineEdit(self)
# 创建按钮
run_button = QPushButton('Run', self)
run_button.clicked.connect(lambda: self.run_script(command_line.text(), radio_button_group))
# 添加控件到布局中
layout = QVBoxLayout()
horizontal_layout = QHBoxLayout()
horizontal_layout.addWidget(python_option)
horizontal_layout.addWidget(command_line)
layout.addLayout(horizontal_layout)
layout.addWidget(run_button)
self.setLayout(layout)
# 居中显示
self.move(QApplication.desktop().screen().rect().center() - self.rect().center())
self.show()
def run_script(self, command_line, radio_button_group):
if radio_button_group.checkedButton() == 1:
os.system('python {}'.format(command_line))
else:
print('Invalid option selected!')
def main():
app = QApplication(sys.argv)
demo = AppDemo()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
以上就是关于PyQt5制作胶囊状单选钮的完整攻略。希望能对你有所帮助。