【PyQt5-Qt Designer】按鈕系列
復選框(QCheckBox)
效果如下:
參考:
https://zhuanlan.zhihu.com/p/30509947
完整代碼:

from PyQt5.QtWidgets import (QApplication,QWidget,QPushButton,QGridLayout,QCheckBox,QMessageBox) from PyQt5.QtCore import Qt from PyQt5.QtGui import (QIcon,QFont,QPixmap) import sys class Example(QWidget): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): self.setGeometry(300,300,200,200) self.setWindowTitle("復選框") gridLayout = QGridLayout() self.btn = QPushButton("提交") self.cb1 = QCheckBox("全選") self.cb2 = QCheckBox("你是") self.cb3 = QCheckBox("我的") self.cb4 = QCheckBox("寶貝") gridLayout.addWidget(self.cb1,0,0,1,1) gridLayout.addWidget(self.cb2,1,0,1,1) gridLayout.addWidget(self.cb3,2,0,1,1) gridLayout.addWidget(self.cb4,3,0,1,1) gridLayout.addWidget(self.btn,4,0,1,1) self.setLayout(gridLayout) self.cb1.stateChanged.connect(self.changecb1) self.cb2.stateChanged.connect(self.changecb2) self.cb3.stateChanged.connect(self.changecb2) self.cb4.stateChanged.connect(self.changecb2) self.btn.clicked.connect(self.btnConfirm) def changecb1(self): if self.cb1.checkState() == Qt.Checked: self.cb2.setChecked(1) self.cb3.setChecked(1) self.cb4.setChecked(1) elif self.cb1.checkState() == Qt.Unchecked: self.cb2.setChecked(0) self.cb3.setChecked(0) self.cb4.setChecked(0) def changecb2(self): if self.cb2.checkState() and self.cb3.checkState() and self.cb4.checkState(): self.cb1.setCheckState(1) elif self.cb2.checkState() or self.cb3.checkState() or self.cb4.checkState(): # self.cb1.setTristate() # self.cb1.setCheckState(Qt.PartiallyChecked) self.cb1.setCheckState(0) else: self.cb1.setTristate(0) self.cb1.setCheckState(Qt.Unchecked) def btnConfirm(self): if self.cb1.checkState(): info = "你是我的寶貝" elif self.cb2.checkState() and self.cb3.checkState(): info = "你是我的" elif self.cb2.checkState() and self.cb4.checkState(): info = "你是寶貝" elif self.cb3.checkState() and self.cb4.checkState(): info = "我的寶貝" elif self.cb2.checkState(): info = "你是" elif self.cb3.checkState(): info = "我的" elif self.cb4.checkState(): info = "寶貝" else: info = "什么都沒有選中啊。。。" reply = QMessageBox.information(self,"消息框",info) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() ex.show() sys.exit(app.exec_())
單選按鈕(QRadioButton)
效果如下:
參考:
https://zhuanlan.zhihu.com/p/30708796
完整代碼:

from PyQt5.QtWidgets import (QApplication,QWidget,QPushButton,QGridLayout,QRadioButton,QMessageBox,QButtonGroup) from PyQt5.QtCore import Qt from PyQt5.QtGui import (QIcon,QFont,QPixmap) import sys class Example(QWidget): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): self.setGeometry(300,300,200,200) self.setWindowTitle("單選框") gridLayout = QGridLayout() self.btn = QPushButton("提交") self.rb11 = QRadioButton("你是") self.rb12 = QRadioButton("大美女") self.rb21 = QRadioButton("我是") self.rb22 = QRadioButton("大帥哥") self.rb31 = QRadioButton("他是") self.rb32 = QRadioButton("小屁孩") gridLayout.addWidget(self.rb11,0,0,1,1) gridLayout.addWidget(self.rb12,0,1,1,1) gridLayout.addWidget(self.rb21,1,0,1,1) gridLayout.addWidget(self.rb22,1,1,1,1) gridLayout.addWidget(self.rb31,2,0,1,1) gridLayout.addWidget(self.rb32,2,1,1,1) gridLayout.addWidget(self.btn,3,0,1,2) self.setLayout(gridLayout) #建立了兩個單選按鈕分組,其中每個分組單選按鈕是互斥的,在每個分組中分別選擇一個單選按鈕,然后提交就得到相應的信息了 #新建6個單選按鈕。如果不增加分組,這個6個單選按鈕是互斥的,因為單選按鈕默認為autoExclusive(自動互斥) #將單選按鈕添加到分組中,同時分配一個id號。函數默認的id是-1 自動分配的ID保證為負數,從-2開始。 如果您正在分配自己的ID,請使用正值來避免沖突。 self.bg1 = QButtonGroup() self.bg1.addButton(self.rb11,11) self.bg1.addButton(self.rb21,21) self.bg1.addButton(self.rb31,31) self.bg2 = QButtonGroup() self.bg2.addButton(self.rb12, 12) self.bg2.addButton(self.rb22, 22) self.bg2.addButton(self.rb32, 32) self.info1 = "" self.info2 = "" #在分組中點擊給定按鈕的時候會發出buttonClicked()信號,同時我們連接到rbclicked這個槽函數上 self.bg1.buttonClicked.connect(self.rb_clicked) self.bg2.buttonClicked.connect(self.rb_clicked) self.btn.clicked.connect(self.btn_submit) def rb_clicked(self): sender = self.sender() if sender == self.bg1: if self.bg1.checkedId() == 11: self.info1 = "你是" elif self.bg1.checkedId() == 21: self.info1 = "我是" elif self.bg1.checkedId() == 31: self.info1 = "他是" else: self.info1 = "" elif sender == self.bg2: if self.bg2.checkedId() == 12: self.info2 = "大美女" elif self.bg2.checkedId() == 22: self.info2 = "大帥哥" elif self.bg2.checkedId() == 32: self.info2 = "小屁孩" else: self.info2 = "" def btn_submit(self): if self.info1 == "" or self.info2 == "": QMessageBox.information(self,"radioButton標題","你什么都沒有選啊!") else: QMessageBox.information(self,"radioButton標題",self.info1+self.info2) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() ex.show() sys.exit(app.exec_())
普通按鈕(QPushButton) 按鈕下拉選擇
效果如下:
參考:
https://zhuanlan.zhihu.com/p/32239438
完整代碼:

from PyQt5.QtWidgets import (QApplication,QWidget,QPushButton,QGridLayout,QMenu,QMessageBox,QButtonGroup) from PyQt5.QtCore import Qt,QTimer from PyQt5.QtGui import (QIcon,QFont,QPixmap) import sys,time class Example(QWidget): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): self.setGeometry(300,300,300,200) self.setWindowTitle("下拉選擇按鈕+計時器") gridLayout = QGridLayout() self.btn1 = QPushButton("這是什么?") self.btn2 = QPushButton("發送驗證碼") menu = QMenu() menu.addAction("我是") menu.addSeparator() menu.addAction("世界上") menu.addSeparator() menu.addAction("最帥的人") menu.addSeparator() self.btn1.setMenu(menu) gridLayout.addWidget(self.btn1,0,0,1,1) gridLayout.addWidget(self.btn2,0,1,1,1) self.setLayout(gridLayout) self.count = 10 self.btn2.clicked.connect(self.Verification_code) self.time = QTimer() self.time.setInterval(1000) #設置時間間隔為1000ms self.time.timeout.connect(self.Refresh) def Verification_code(self): if self.btn2.isEnabled(): self.time.start() #開始計時 self.btn2.setEnabled(0) #設置按鈕為未激活狀態 def Refresh(self): if self.count >0: self.btn2.setText(str(self.count)+"秒后重發") self.count -= 1 else: self.time.stop() self.bt2.setEnabled(True) ##設置按鈕為使能狀態 self.bt2.setText('發送驗證碼') self.count = 10 if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() ex.show() sys.exit(app.exec_())
補充 QTimer()計時器:
效果如下:
完整代碼:

import time,sys from PyQt5.QtCore import Qt,QTimer from PyQt5.QtWidgets import (QApplication,QWidget,QPushButton,QGridLayout,QMenu,QMessageBox,QButtonGroup,QLabel) class Example(QWidget): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): self.setGeometry(300, 300, 300, 200) self.setWindowTitle("Qtimer計時器案例") gridLayout = QGridLayout() self.btn1 = QPushButton("開始計時") self.lb = QLabel("10s計時器,發送驗證碼") gridLayout.addWidget(self.lb,0,0,1,1) gridLayout.addWidget(self.btn1,1,0,1,1) self.setLayout(gridLayout) self.btn1.clicked.connect(self.start_timer) self.count = 10 self.timer = QTimer() self.timer.setInterval(1000) self.timer.timeout.connect(self.time_count) def start_timer(self): if self.btn1.isEnabled(): self.timer.start() self.btn1.setEnabled(0) def time_count(self): if self.count>0: self.lb.setText(str(self.count) + "秒后重發") self.count-=1 else: self.timer.stop() self.btn1.setText('已發送驗證碼') self.lb.setText('') self.btn1.setEnabled(True) self.count = 10 if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() ex.show() sys.exit(app.exec_())
工具按鈕(QToolButton)
效果如下:
參考:
https://zhuanlan.zhihu.com/p/32407887
https://doc.qt.io/qt-5/qtoolbutton.html
完整代碼:

from PyQt5.QtWidgets import (QApplication,QWidget,QMenu,QToolButton,QAction) from PyQt5.QtCore import (Qt,QUrl) from PyQt5.QtGui import (QIcon,QDesktopServices) import sys class Example(QWidget): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): self.setGeometry(300,300,300,200) self.setWindowTitle("工具按鈕") self.tb = QToolButton(self) self.tb.move(100,100) self.tb.setToolButtonStyle(Qt.ToolButtonTextBesideIcon) #設置工具按鈕類型:圖標在文字旁邊 # self.tb.setArrowType(Qt.DownArrow) #設置箭頭圖標 self.tb.setToolTip("選擇支付方式!") #設置提示 self.tb.setText("支付方式") self.tb.setIcon(QIcon(r'Qt設計師/china.ico')) self.tb.setAutoRaise(True) #此屬性保持是否啟用自動升起,默認是禁用的(即False) # self.tb.setPopupMode(QToolButton.MenuButtonPopup) #按下箭頭顯示下拉菜單 # self.tb.setPopupMode(QToolButton.DelayedPopup) #一直按箭頭 一段時間后顯示下拉菜單(默認) self.tb.setPopupMode(QToolButton.InstantPopup) # 無延時,按下圖標即可顯示下拉菜單 menu = QMenu() self.alipayAct = QAction(QIcon('Qt設計師/icon/alipay.ico'),"支付寶") self.wechatAct = QAction(QIcon('Qt設計師/icon/wechat.ico'),"微信") self.master_cardAct = QAction(QIcon('Qt設計師/icon/master_card.ico'),"萬事達") self.visaAct = QAction(QIcon('Qt設計師/icon/visa.ico'),"Visa") menu.addAction(self.alipayAct) menu.addAction(self.wechatAct) menu.addAction(self.master_cardAct) menu.addAction(self.visaAct) self.tb.setMenu(menu) self.alipayAct.triggered.connect(self.on_click) self.wechatAct.triggered.connect(self.on_click) self.master_cardAct.triggered.connect(self.on_click) self.visaAct.triggered.connect(self.on_click) def on_click(self): if self.sender() == self.alipayAct: QDesktopServices.openUrl(QUrl("https://www.alipay.com/")) elif self.sender() == self.wechatAct: QDesktopServices.openUrl(QUrl("https://pay.weixin.qq.com/index.php")) elif self.sender() == self.visaAct: QDesktopServices.openUrl(QUrl("https://www.visa.com.cn/")) elif self.sender() == self.master_cardAct: QDesktopServices.openUrl(QUrl("https://www.mastercard.com.cn/zh-cn.html")) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() ex.show() sys.exit(app.exec_())
補充:用QT設計師完成

from PyQt5.QtWidgets import QMenu,QAction from PyQt5.QtGui import QIcon,QDesktopServices from PyQt5.QtCore import QUrl from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Dialog(object): def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(571, 434) font = QtGui.QFont() font.setFamily("微軟雅黑") font.setPointSize(12) Dialog.setFont(font) Dialog.setSizeGripEnabled(True) self.toolButton = QtWidgets.QToolButton(Dialog) self.toolButton.setGeometry(QtCore.QRect(120, 100, 121, 81)) icon = QtGui.QIcon() icon.addPixmap(QtGui.QPixmap("china.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off) self.toolButton.setIcon(icon) self.toolButton.setIconSize(QtCore.QSize(50, 50)) # self.toolButton.setPopupMode(QtWidgets.QToolButton.MenuButtonPopup) #按下箭頭顯示下拉菜單 # self.toolButton.setPopupMode(QtWidgets.QToolButton.DelayedPopup) #一直按下箭頭一段時間后顯示下拉菜單 self.toolButton.setPopupMode(QtWidgets.QToolButton.InstantPopup) #無延時,按下圖標即可顯示下拉菜單 self.toolButton.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon) self.toolButton.setAutoRaise(False) self.toolButton.setArrowType(QtCore.Qt.NoArrow) self.toolButton.setObjectName("toolButton") menu = QMenu(self) self.alipayAct = QAction(QIcon('icon/alipay.ico'), '支付寶支付', self) self.wechatAct = QAction(QIcon('icon/wechat.ico'), '微信支付', self) self.visaAct = QAction(QIcon('icon/visa.ico'), 'Visa卡支付', self) self.master_cardAct = QAction(QIcon('icon/master_card.ico'), '萬事達卡支付', self) menu.addAction(self.alipayAct) menu.addAction(self.wechatAct) menu.addSeparator() menu.addAction(self.visaAct) menu.addAction(self.master_cardAct) self.toolButton.setMenu(menu) self.alipayAct.triggered.connect(self.on_click) self.wechatAct.triggered.connect(self.on_click) self.visaAct.triggered.connect(self.on_click) self.master_cardAct.triggered.connect(self.on_click) self.retranslateUi(Dialog) # QtCore.QMetaObject.connectSlotsByName(Dialog) def on_click(self): if self.sender() == self.alipayAct: QDesktopServices.openUrl(QUrl('https://www.alipay.com/')) elif self.sender() == self.wechatAct: QDesktopServices.openUrl(QUrl('https://pay.weixin.qq.com/index.php')) elif self.sender() == self.visaAct: QDesktopServices.openUrl(QUrl('https://www.visa.com.cn/')) else: QDesktopServices.openUrl(QUrl('https://www.mastercard.com.cn/zh-cn.html')) def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "Dialog")) self.toolButton.setToolTip(_translate("Dialog", "選擇你的支付方式")) self.toolButton.setText(_translate("Dialog", "支付方式"))

# -*- coding: utf-8 -*- from PyQt5.QtCore import pyqtSlot from PyQt5.QtWidgets import QDialog, QApplication from Ui_toolbtn import Ui_Dialog class Dialog(QDialog, Ui_Dialog): def __init__(self, parent=None): super(Dialog, self).__init__(parent) self.setupUi(self) if __name__ == "__main__": import sys app = QApplication(sys.argv) ui = Dialog() ui.show() sys.exit(app.exec_())
抽象按鈕(QAbstractButton)
效果如下:
案例1
案例2:
案例3:
參考:
https://zhuanlan.zhihu.com/p/32818176
完整代碼:

from PyQt5.QtWidgets import (QApplication,QWidget,QPushButton,QMessageBox,QGridLayout,QLabel) import sys class Example(QWidget): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): self.setGeometry(300,300,200,300) self.setWindowTitle("抽象按鈕1") gridLayout = QGridLayout() self.lb1 = QLabel("密碼輸入區:") self.lb2 = QLabel("正確密碼:麻") self.lb3 = QLabel("你輸入的密碼:") self.btn1 = QPushButton("芝") self.btn1.setMinimumSize(40,40) self.btn2 = QPushButton("麻") self.btn2.setMinimumSize(40, 40) self.btn3 = QPushButton("開") self.btn3.setMinimumSize(40, 40) self.btn4 = QPushButton("門") self.btn4.setMinimumSize(40, 40) gridLayout.addWidget(self.lb1,0,0,1,1) gridLayout.addWidget(self.lb2,3,0,1,1) gridLayout.addWidget(self.lb3,4,0,1,1) gridLayout.addWidget(self.btn1,1,1,1,1) gridLayout.addWidget(self.btn2,1,2,1,1) gridLayout.addWidget(self.btn3,2,1,1,1) gridLayout.addWidget(self.btn4,2,2,1,1) self.setLayout(gridLayout) self.btn1.setCheckable(True) self.btn2.setCheckable(True) self.btn3.setCheckable(True) self.btn4.setCheckable(True) self.btn1.setAutoExclusive(True) self.btn2.setAutoExclusive(True) self.btn3.setAutoExclusive(True) self.btn4.setAutoExclusive(True) self.btn1.clicked.connect(self.setPassword) self.btn2.clicked.connect(self.setPassword) self.btn3.clicked.connect(self.setPassword) self.btn4.clicked.connect(self.setPassword) def setPassword(self): word = self.sender().text() self.lb3.setText("你輸入的密碼:"+word) if word == "麻": QMessageBox.information(self,"信息提示框","恭喜你,密碼輸入正確!") if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() ex.show() sys.exit(app.exec_())

1 from PyQt5.QtWidgets import (QApplication,QWidget,QPushButton,QMessageBox,QGridLayout,QLabel) 2 import sys 3 4 class Example(QWidget): 5 def __init__(self): 6 super(Example, self).__init__() 7 self.initUI() 8 9 def initUI(self): 10 self.setGeometry(300,300,200,300) 11 self.setWindowTitle("抽象按鈕2") 12 gridLayout = QGridLayout() 13 self.lb1 = QLabel("密碼輸入區:") 14 self.lb2 = QLabel("正確密碼:芝麻開門") 15 self.lb3 = QLabel("你輸入的密碼:") 16 self.btn1 = QPushButton("芝") 17 self.btn1.setMinimumSize(40,40) 18 self.btn2 = QPushButton("麻") 19 self.btn2.setMinimumSize(40, 40) 20 self.btn3 = QPushButton("開") 21 self.btn3.setMinimumSize(40, 40) 22 self.btn4 = QPushButton("門") 23 self.btn4.setMinimumSize(40, 40) 24 gridLayout.addWidget(self.lb1,0,0,1,1) 25 gridLayout.addWidget(self.lb2,3,0,1,1) 26 gridLayout.addWidget(self.lb3,4,0,1,1) 27 gridLayout.addWidget(self.btn1,1,1,1,1) 28 gridLayout.addWidget(self.btn2,1,2,1,1) 29 gridLayout.addWidget(self.btn3,2,1,1,1) 30 gridLayout.addWidget(self.btn4,2,2,1,1) 31 self.setLayout(gridLayout) 32 33 self.btn1.setCheckable(True) 34 self.btn2.setCheckable(True) 35 self.btn3.setCheckable(True) 36 self.btn4.setCheckable(True) 37 self.btn1.setAutoExclusive(True) 38 self.btn2.setAutoExclusive(True) 39 self.btn3.setAutoExclusive(True) 40 self.btn4.setAutoExclusive(True) 41 self.password = "" 42 43 self.btn1.clicked.connect(self.setPassword) 44 self.btn2.clicked.connect(self.setPassword) 45 self.btn3.clicked.connect(self.setPassword) 46 self.btn4.clicked.connect(self.setPassword) 47 48 49 def setPassword(self,pressed): 50 word = self.sender().text() 51 if pressed: 52 if len(self.password) < 4: 53 self.password += word 54 else: 55 self.password = self.password.replace(word, '') 56 57 self.lb3.setText("你輸入的密碼:"+self.password) 58 if len(self.password) == 4 and self.password == '芝麻開門': 59 QMessageBox.information(self, '提示', '恭喜,密碼正確,可以進入!') 60 61 62 if __name__ == '__main__': 63 app = QApplication(sys.argv) 64 ex = Example() 65 ex.show() 66 sys.exit(app.exec_())

1 from PyQt5.QtWidgets import (QApplication,QWidget,QPushButton,QMessageBox,QGridLayout,QLabel,QButtonGroup) 2 import sys 3 4 class Example(QWidget): 5 def __init__(self): 6 super(Example, self).__init__() 7 self.initUI() 8 9 def initUI(self): 10 self.setGeometry(300,300,200,300) 11 self.setWindowTitle("抽象按鈕3") 12 gridLayout = QGridLayout() 13 self.lb1 = QLabel("密碼輸入區:") 14 self.lb2 = QLabel("正確密碼:321") 15 self.lb3 = QLabel("你輸入的密碼:") 16 self.btn11 = QPushButton("1") 17 self.btn11.setMinimumSize(40,40) 18 self.btn12 = QPushButton("2") 19 self.btn12.setMinimumSize(40, 40) 20 self.btn13 = QPushButton("3") 21 self.btn13.setMinimumSize(40, 40) 22 self.btn21 = QPushButton("1") 23 self.btn21.setMinimumSize(40, 40) 24 self.btn22 = QPushButton("2") 25 self.btn22.setMinimumSize(40, 40) 26 self.btn23 = QPushButton("3") 27 self.btn23.setMinimumSize(40, 40) 28 self.btn31 = QPushButton("1") 29 self.btn31.setMinimumSize(40, 40) 30 self.btn32 = QPushButton("2") 31 self.btn32.setMinimumSize(40, 40) 32 self.btn33 = QPushButton("3") 33 self.btn33.setMinimumSize(40, 40) 34 gridLayout.addWidget(self.lb1,0,0,1,1) 35 gridLayout.addWidget(self.lb2,4,0,1,1) 36 gridLayout.addWidget(self.lb3,5,0,1,1) 37 gridLayout.addWidget(self.btn11,1,1,1,1) 38 gridLayout.addWidget(self.btn12,1,2,1,1) 39 gridLayout.addWidget(self.btn13,1,3,1,1) 40 gridLayout.addWidget(self.btn21,2,1,1,1) 41 gridLayout.addWidget(self.btn22,2,2,1,1) 42 gridLayout.addWidget(self.btn23,2,3,1,1) 43 gridLayout.addWidget(self.btn31,3,1,1,1) 44 gridLayout.addWidget(self.btn32,3,2,1,1) 45 gridLayout.addWidget(self.btn33,3,3,1,1) 46 self.setLayout(gridLayout) 47 48 self.btn11.setCheckable(True) 49 self.btn12.setCheckable(True) 50 self.btn13.setCheckable(True) 51 self.btn21.setCheckable(True) 52 self.btn22.setCheckable(True) 53 self.btn23.setCheckable(True) 54 self.btn31.setCheckable(True) 55 self.btn32.setCheckable(True) 56 self.btn33.setCheckable(True) 57 58 self.btg1 = QButtonGroup(self) 59 self.btg2 = QButtonGroup(self) 60 self.btg3 = QButtonGroup(self) 61 62 self.btg1.addButton(self.btn11,1) 63 self.btg1.addButton(self.btn12,2) 64 self.btg1.addButton(self.btn13,3) 65 self.btg2.addButton(self.btn21,1) 66 self.btg2.addButton(self.btn22,2) 67 self.btg2.addButton(self.btn23,3) 68 self.btg3.addButton(self.btn31,1) 69 self.btg3.addButton(self.btn32,2) 70 self.btg3.addButton(self.btn33,3) 71 72 self.pwd1 , self.pwd2 ,self.pwd3 = "","","" 73 74 self.btg1.buttonClicked.connect(self.setPassWord) 75 self.btg2.buttonClicked.connect(self.setPassWord) 76 self.btg3.buttonClicked.connect(self.setPassWord) 77 78 def setPassWord(self): 79 sender = self.sender() 80 if sender == self.btg1: 81 self.pwd1 = str(self.btg1.checkedId()) 82 elif sender == self.btg2: 83 self.pwd2 = str(self.btg2.checkedId()) 84 elif sender == self.btg3: 85 self.pwd3 = str(self.btg3.checkedId()) 86 87 you_password = self.pwd1+self.pwd2+self.pwd3 88 self.lb3.setText(you_password) 89 if you_password == "321": 90 QMessageBox.information(self,"提示框","恭喜你密碼正確!",QMessageBox.Ok|QMessageBox.Cancel|QMessageBox.Close,QMessageBox.Ok) 91 elif len(you_password) == 3 and you_password != "321": 92 QMessageBox.warning(self,"警告框","密碼輸入錯誤,請重新輸入!") 93 94 95 if __name__ == '__main__': 96 app = QApplication(sys.argv) 97 ex = Example() 98 ex.show() 99 sys.exit(app.exec_())