[PyQt introductory tutorial] PyQt5 basic controls use: radio buttons, check boxes, drop-down box

   This paper describes the basic use PyQt5 interface radio buttons, and check boxes, drop-down box to use three kinds of controls are introduced.

   1, RadioButton radio buttons / CheckBox checkbox. You need to know how to determine whether the radio button is selected.

   2, ComboBox drop-down box. You need to know how to set and code implementation How to get the user to select a value for the value of the drop-down box.

   With these questions began to introduce this next RadioButton radio button, CheckBox checkbox, the ComboBox drop-down box to use three basic controls

QRadioButton radio button

  Radio buttons for the user to provide one of many choices, a switch button. QRadioButton method of determining whether to select the radio button status isChecked (). the isChecked () method returns the value True when selected, False means not selected.

RadioButton example of a complete code is as follows:

# -*- 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, " the message box title " , " I RadioButton button is selected it! " , QMessageBox.Yes | QMessageBox.No) 

IF  __name__ == " __main__ " : 
    App = QApplication (sys.argv) 
    mywin = MyMainForm ( ) 
    myWin.show () 
    sys.exit (app.exec_ ())

Results are as follows:

 The key code is introduced:

  self.radioButton.isChecked ()   -> RadioButton used to determine whether the control is selected. The return value Trule expressed button is selected, False means that the button is not selected.

QCheckBox box

   Check boxes and radio buttons are the same as option buttons, check boxes difference is to provide users with multiple-choice and more choice. Button again using the isChecked box () method of determining whether or not selected.

Examples CheckBox complete code is as follows:

# -*- 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", ""))
        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, " the message box title " , " fish and I want to eat it eat it! " , QMessageBox.Yes | QMessageBox.No) 

IF  __name__ == " __main__ " : 
    App = QApplication (sys.argv) 
    mywin = MyMainForm () 
    myWin.show () 
    sys.exit (app.exec_ ())

Results are as follows:

 The key code is introduced:

 self.freshcheckBox.isChecked () and self.bearcheckBox.isChecked () -> The same applies the isChecked () function to determine.

QComboBox下拉列表框

  下拉列表框是一个集按钮和下拉选项于一体的控件。通常用于固定的枚举值供用户选择时使用。对于下拉列表框的使用最基本的是要知道如何添加下拉列表框中的值以及如何获取下拉框中选择的值。

  (1)如何添加下拉列表框中的值。

  1、使用addItem() 添加一个下拉选项或者additems() 从列表中添加下拉选项 方法进行添加。

  2、如果使用Qt Designer画图实现,可以将ComboBox控件添加到主界面后双击下拉列表框进行打开添加。如下:

  (2)如何获取下拉框中的取值

  使用函数currentText() 返回选项中的文本进行获取

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,"消息框标题","你要学%s,为师给你说道说道!" % (select_value,),QMessageBox.Yes | QMessageBox.No)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    myWin = MyMainForm()
    myWin.show()
    sys.exit(app.exec_())

运行结果如下:

 关键代码介绍:

  select_value = self.comboBox.currentText() --> 使用currentText()函数获取下拉框中选择的值

小结

  RadioButton单选按钮、CheckBox复选框、ComboBox下拉框三种基本控件的使用方法介绍完了。本文中的内容和实例也基本回答了开篇提到的问题。这三种基本控件的使用简单但也很频繁。可以多动手实践一下。上文中的程序都可以直接运行。可以运行看看效果。

Guess you like

Origin www.cnblogs.com/linyfeng/p/11294173.html