标签:button qpixmap 占位符 import .sh text box qlabel .text
1 import sys 2 from PyQt5.QtGui import QPixmap 3 from PyQt5.QtWidgets import QApplication, QWidget, QRadioButton, QLabel, QHBoxLayout, QVBoxLayout 4 5 6 class Demo(QWidget): 7 def __init__(self): 8 super(Demo, self).__init__() 9 self.off_button = QRadioButton(‘off‘, self) # 实例化单选按钮,参数1:显示的文本 10 self.on_button = QRadioButton(‘开‘, self) 11 12 self.pic_label = QLabel(self) 13 14 self.button_h_layout = QHBoxLayout() 15 self.pic_h_layout = QHBoxLayout() 16 self.all_v_layout = QVBoxLayout() 17 18 self.layout_init() 19 self.radiobutton_init() 20 self.label_init() 21 self.off_button.setText(‘关‘) #设置按钮显示的文本 22 print(self.on_button.text()) #返回按钮显示的文本 23 24 def layout_init(self): 25 self.pic_h_layout.addStretch(1) # 在布局中添加1个占位符。参数:n个位置 26 self.pic_h_layout.addWidget(self.pic_label) 27 self.pic_h_layout.addStretch(1) 28 self.button_h_layout.addWidget(self.off_button) 29 self.button_h_layout.addWidget(self.on_button) 30 self.all_v_layout.addLayout(self.pic_h_layout) 31 self.all_v_layout.addLayout(self.button_h_layout) 32 33 self.setLayout(self.all_v_layout) 34 35 def radiobutton_init(self): 36 self.off_button.setChecked(True) # 设置为选中状态 37 self.off_button.toggled.connect(self.on_off_bulb_func) # 按钮状态发生变化时发出信号 38 # self.on_button.toggled.connect(self.on_off_bulb_func) 39 40 def label_init(self): 41 self.pic_label.setPixmap(QPixmap(‘off.png‘)) 42 43 def on_off_bulb_func(self): 44 if self.off_button.isChecked(): #返回按钮是否被选中,True选中 45 self.pic_label.setPixmap(QPixmap(‘off.png‘)) #在标签上添加一张图片 46 #需要 from PyQt5.QtGui import QPixmap 47 else: 48 self.pic_label.setPixmap(QPixmap(‘on.png‘)) 49 #图片下载地址: 50 #off.png: https://www.easyicon.net/download/png/538753/64/ 51 #on.png: https://www.easyicon.net/download/png/538754/64/ 52 53 54 if __name__ == ‘__main__‘: 55 app = QApplication(sys.argv) 56 demo = Demo() 57 demo.show() 58 sys.exit(app.exec_())
标签:button qpixmap 占位符 import .sh text box qlabel .text
原文地址:https://www.cnblogs.com/liming19680104/p/10357642.html