PyQt5基本控件使用詳解:單選按鈕、復(fù)選框、下拉框
本文主要介紹PyQt5界面最基本使用的單選按鈕、復(fù)選框、下拉框三種控件的使用方法進(jìn)行介紹。
1、RadioButton單選按鈕/CheckBox復(fù)選框。需要知道如何判斷單選按鈕是否被選中。
2、ComboBox下拉框。需要知道如何對(duì)下拉框中的取值進(jìn)行設(shè)置以及代碼實(shí)現(xiàn)中如何獲取用戶選中的值。
帶著這些問(wèn)題下面開(kāi)始介紹這 RadioButton單選按鈕、CheckBox復(fù)選框、 ComboBox下拉框 三種基本控件的使用方法
QRadioButton單選按鈕
單選按鈕為用戶提供 多選一 的選擇,是一種開(kāi)關(guān)按鈕。QRadioButton單選按鈕是否選擇狀態(tài)通過(guò)isChecked()方法判斷。isChecked()方法返回值True表示選中,F(xiàn)alse表示未選中。
RadioButton示例完整代碼如下:
# -*- coding: utf-8 -*- import sys from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QRadioButton class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(309, 126) self.radioButton = QtWidgets.QRadioButton(Form) self.radioButton.setGeometry(QtCore.QRect(70, 40, 89, 16)) self.radioButton.setObjectName("radioButton") self.okButton = QtWidgets.QPushButton(Form) self.okButton.setGeometry(QtCore.QRect(70, 70, 75, 23)) self.okButton.setObjectName("okButton") self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "RadioButton單選按鈕例子")) self.radioButton.setText(_translate("Form", "單選按鈕")) self.okButton.setText(_translate("Form", "確定")) class MyMainForm(QMainWindow, Ui_Form): def __init__(self, parent=None): super(MyMainForm, self).__init__(parent) self.setupUi(self) self.okButton.clicked.connect(self.checkRadioButton) def checkRadioButton(self): if self.radioButton.isChecked(): QMessageBox.information(self,"消息框標(biāo)題","我RadioButton按鈕被選中啦!",QMessageBox.Yes | QMessageBox.No) if __name__ == "__main__": app = QApplication(sys.argv) myWin = MyMainForm() myWin.show() sys.exit(app.exec_())
運(yùn)行結(jié)果如下:
關(guān)鍵代碼介紹:
self.radioButton.isChecked() --> 用于判斷RadioButton控件是否被選中。返回值Trule表示按鈕被選中,F(xiàn)alse表示按鈕未選中。
QCheckBox復(fù)選框
復(fù)選框和單選按鈕一樣都是選項(xiàng)按鈕,區(qū)別是復(fù)選框?yàn)橛脩籼峁?多選多 的選擇。復(fù)選框按鈕同樣是使用isChecked()方法判斷是否被選中。
CheckBox例子完整代碼如下:
# -*- coding: utf-8 -*- import sys from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QCheckBox class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(380, 154) self.freshcheckBox = QtWidgets.QCheckBox(Form) self.freshcheckBox.setGeometry(QtCore.QRect(50, 40, 71, 31)) font = QtGui.QFont() font.setPointSize(14) self.freshcheckBox.setFont(font) self.freshcheckBox.setObjectName("freshcheckBox") self.bearcheckBox = QtWidgets.QCheckBox(Form) self.bearcheckBox.setGeometry(QtCore.QRect(140, 40, 71, 31)) font = QtGui.QFont() font.setPointSize(14) self.bearcheckBox.setFont(font) self.bearcheckBox.setObjectName("bearcheckBox") self.okButton = QtWidgets.QPushButton(Form) self.okButton.setGeometry(QtCore.QRect(230, 40, 71, 31)) font = QtGui.QFont() font.setPointSize(14) self.okButton.setFont(font) self.okButton.setObjectName("okButton") self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "CheckBox例子")) self.freshcheckBox.setText(_translate("Form", "魚(yú)")) self.bearcheckBox.setText(_translate("Form", "熊掌")) self.okButton.setText(_translate("Form", "確定")) class MyMainForm(QMainWindow, Ui_Form): def __init__(self, parent=None): super(MyMainForm, self).__init__(parent) self.setupUi(self) self.okButton.clicked.connect(self.checkCheckBox) def checkCheckBox(self): if self.freshcheckBox.isChecked() and self.bearcheckBox.isChecked(): QMessageBox.information(self,"消息框標(biāo)題","魚(yú)和熊掌我要兼得!",QMessageBox.Yes | QMessageBox.No) if __name__ == "__main__": app = QApplication(sys.argv) myWin = MyMainForm() myWin.show() sys.exit(app.exec_())
運(yùn)行結(jié)果如下:
關(guān)鍵代碼介紹:
self.freshcheckBox.isChecked() and self.bearcheckBox.isChecked() --> 同樣適用isChecked()函數(shù)判斷。
QComboBox下拉列表框
下拉列表框是一個(gè)集按鈕和下拉選項(xiàng)于一體的控件。通常用于固定的枚舉值供用戶選擇時(shí)使用。對(duì)于下拉列表框的使用最基本的是要知道如何添加下拉列表框中的值以及如何獲取下拉框中選擇的值。
(1) 如何添加下拉列表框中的值。
1、使用addItem() 添加一個(gè)下拉選項(xiàng)或者additems() 從列表中添加下拉選項(xiàng) 方法進(jìn)行添加。
2、如果使用Qt Designer畫(huà)圖實(shí)現(xiàn),可以將ComboBox控件添加到主界面后雙擊下拉列表框進(jìn)行打開(kāi)添加。如下:
(2)如何獲取下拉框中的取值
使用函數(shù)currentText() 返回選項(xiàng)中的文本進(jìn)行獲取
ComboBox示例完整代碼如下:
# -*- coding: utf-8 -*- import sys from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QComboBox class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(400, 130) self.comboBox = QtWidgets.QComboBox(Form) self.comboBox.setGeometry(QtCore.QRect(80, 50, 69, 22)) self.comboBox.setObjectName("comboBox") self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.addItem("") self.okButton = QtWidgets.QPushButton(Form) self.okButton.setGeometry(QtCore.QRect(190, 50, 75, 23)) self.okButton.setObjectName("okButton") self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "ComboBox下拉框例子")) self.comboBox.setItemText(0, _translate("Form", "Python")) self.comboBox.setItemText(1, _translate("Form", "C++")) self.comboBox.setItemText(2, _translate("Form", "Go")) self.comboBox.setItemText(3, _translate("Form", "Java")) self.okButton.setText(_translate("Form", "確定")) class MyMainForm(QMainWindow, Ui_Form): def __init__(self, parent=None): super(MyMainForm, self).__init__(parent) self.setupUi(self) self.okButton.clicked.connect(self.getComboxBoxValue) def getComboxBoxValue(self): select_value = self.comboBox.currentText() QMessageBox.information(self,"消息框標(biāo)題","你要學(xué)%s,為師給你說(shuō)道說(shuō)道!" % (select_value,),QMessageBox.Yes | QMessageBox.No) if __name__ == "__main__": app = QApplication(sys.argv) myWin = MyMainForm() myWin.show() sys.exit(app.exec_())
運(yùn)行結(jié)果如下:
關(guān)鍵代碼介紹:
select_value = self.comboBox.currentText() --> 使用currentText()函數(shù)獲取下拉框中選擇的值
小結(jié)
RadioButton單選按鈕、CheckBox復(fù)選框、 ComboBox下拉框三種基本控件 的使用方法介紹完了。本文中的內(nèi)容和實(shí)例也基本回答了開(kāi)篇提到的問(wèn)題。這三種基本控件的使用簡(jiǎn)單但也很頻繁??梢远鄤?dòng)手實(shí)踐一下。上文中的程序都可以直接運(yùn)行??梢赃\(yùn)行看看效果。
總結(jié)
以上所述是小編給大家介紹的PyQt5基本控件使用詳解:單選按鈕、復(fù)選框、下拉框,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
Python進(jìn)階學(xué)習(xí)之特殊方法實(shí)例詳析
一般說(shuō)來(lái),特殊的方法都被用來(lái)模仿某個(gè)行為。下面這篇文章主要給大家介紹了關(guān)于Python進(jìn)階學(xué)習(xí)之特殊方法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起看看吧。2017-12-12python標(biāo)準(zhǔn)庫(kù)模塊之json庫(kù)的基礎(chǔ)用法
json庫(kù)是處理JSON格式的Python標(biāo)準(zhǔn)庫(kù),json庫(kù)主要包括兩類函數(shù),操作函數(shù)和解析函數(shù),下面這篇文章主要給大家介紹了關(guān)于python標(biāo)準(zhǔn)庫(kù)模塊之json庫(kù)的基礎(chǔ)用法,需要的朋友可以參考下2022-06-06python3讀取autocad圖形文件.py實(shí)例
這篇文章主要介紹了python3讀取autocad圖形文件.py實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06Python基礎(chǔ)學(xué)習(xí)之時(shí)間轉(zhuǎn)換函數(shù)用法詳解
這篇文章主要介紹了Python基礎(chǔ)學(xué)習(xí)之時(shí)間轉(zhuǎn)換函數(shù)用法,結(jié)合實(shí)例形式分析了Python常見(jiàn)的日期時(shí)間獲取、轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下2019-06-06Python數(shù)字圖像處理之霍夫線變換實(shí)現(xiàn)詳解
這篇文章主要介紹了Python數(shù)字圖像處理之霍夫線變換實(shí)現(xiàn)詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01Python生成ubuntu apt鏡像地址實(shí)現(xiàn)
本文主要介紹了Python生成ubuntu apt鏡像地址實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05