QCheckBox控件


一個QCheckBox會有2種狀態:選中和為選中。它由一個選擇框和一個label組成,常常用來表示應用的某些特性是啟用或不啟用。

在下面的例子中,我們創建了一個選擇框,它的狀態變化會引起窗口標題的變化。

示例的執行效果如下:

 1 #!/usr/bin/python3
 2 # -*- coding: utf-8 -*-
 3 
 4 """
 5 ZetCode PyQt5 tutorial
 6 
 7 In this example, a QCheckBox widget
 8 is used to toggle the title of a window.
 9 
10 Author: Jan Bodnar
11 Website: zetcode.com
12 Last edited: August 2017
13 """
14 
15 from PyQt5.QtWidgets import QWidget, QCheckBox, QApplication
16 from PyQt5.QtCore import Qt
17 import sys
18 
19 
20 class Example(QWidget):
21 
22     def __init__(self):
23         super().__init__()
24 
25         self.initUI()
26 
27     def initUI(self):
28 
29         cb = QCheckBox('Show title', self)
30         cb.move(20, 20)
31         cb.toggle()     # 設置默認是選中狀態
32 
33         # 將自定義的槽函數changeTitle和信號stateChanged綁定起來,
34         # 槽函數changeTitle會改變窗口的標題
35         cb.stateChanged.connect(self.changeTitle)
36 
37         self.setGeometry(300, 300, 250, 150)
38         self.setWindowTitle('QCheckBox')
39         self.show()
40 
41     def changeTitle(self, state):
42         # 選中狀態下,窗口標題設置為”QCheckBox”,否則設置為空
43         if state == Qt.Checked:
44             self.setWindowTitle('QCheckBox')
45         else:
46             self.setWindowTitle(' ')
47 
48 
49 if __name__ == '__main__':
50 
51     app = QApplication(sys.argv)
52     ex = Example()
53     sys.exit(app.exec_())

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM