手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

使用PyQt5和Qt Designer创建Python GUI应用程序(六)

Combobox组合框

[日期:2020-05-13] 来源:Linux公社  作者:醉落红尘 [字体: ]

这篇PyQt5组合框教程将展示如何创建,使用和操作Combobox组合框。Combobox可以使您可以从下拉选项框列表中选择一个数据。

创建GUI

在本教程中,我们将创建一个可以模拟XOR功能的GUI 。我们将使用组合框来允许用户选择两个二进制输入(0或1),然后显示应用于它们的XOR函数的结果。

注意:这是XOR函数的真值表。它需要两个二进制输入,我们将从组合框获得这些二进制输入,并为我们提供0或1的输出。

这是我们将要构建的GUI:

添加组合框

要将Combobox组合框添加到GUI中,只需将其从侧栏拖入即可。将其移至所需位置后,对其进行调整大小并命名,即可为其添加一些项目。

要将项目添加到组合框,请双击它,然后单击绿色的“ +”按钮,

 使用PyQt5和Qt Designer创建Python GUI应用程序(六)

我将把其余的GUI留给您,因为我确信到目前为止,您已经知道如何构建它。

新增项目

在某些情况下,您可能希望将代码从代码添加到Combobox。您可以在所需的Combobox上使用.addItem()方法执行此操作。在此示例中,我的Combobox名为comboX。

self.comboX.addItem("Linux公社 www.linuxidc.com")

更改默认项目

您可能想要做的另一件事是设置默认情况下在combobox中显示的项目。 为此,我们可以找到该项目的索引并设置combobox的当前索引。 索引的工作方式与Python列出的相同。 其中0是第一个项目,项目数量-1是最后一个项目。

index = self.comboX.findText("1", QtCore.QtMatchFixedString)  #  查找所需项目的索引
self.comboX.setCurrentIndex(index)  # 设置索引(您也可以只为变量索引放入一个整数)

获取comboBox Selection

我们可能要做的下一件事是获取用户在comboBox中Selection的项目。对于本例,我们将从每个combobox获取输入。对其应用XOR函数并更改标签以显示结果。这意味着我们需要做的第一件事是创建一个新方法并将其链接到提交按钮。

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
         ...
        self.submit.clicked.connect(self.pressed)

    def pressed(self):
        pass

现在我们可以从inside或pressed方法中获取comboBox文本。

        x = int(self.comboX.currentText())
        y =  int(self.comboY.currentText())

现在,要获得XOR函数的结果,我们将使用运算符的巧妙组合。

        xor = (x and not y) or (not x and y)

最后,我们可以设置标签文本以显示此结果。

        self.label.setText("X XOR Y = " + str(xor))

完整代码:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.comboX = QtWidgets.QComboBox(self.centralwidget)
        self.comboX.setGeometry(QtCore.QRect(50, 120, 231, 121))
        font = QtGui.QFont()
        font.setPointSize(28)
        self.comboX.setFont(font)
        self.comboX.setObjectName("comboX")
        self.comboX.addItem("")
        self.comboX.addItem("")
        self.comboY = QtWidgets.QComboBox(self.centralwidget)
        self.comboY.setGeometry(QtCore.QRect(470, 120, 231, 121))
        font = QtGui.QFont()
        font.setPointSize(28)
        self.comboY.setFont(font)
        self.comboY.setObjectName("comboY")
        self.comboY.addItem("")
        self.comboY.addItem("")
        self.submit = QtWidgets.QPushButton(self.centralwidget)
        self.submit.setGeometry(QtCore.QRect(290, 420, 221, 91))
        font = QtGui.QFont()
        font.setPointSize(22)
        self.submit.setFont(font)
        self.submit.setObjectName("submit")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(280, 290, 221, 81))
        font = QtGui.QFont()
        font.setPointSize(20)
        self.label.setFont(font)
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.submit.clicked.connect(self.pressed)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Linux公社 www.linuxidc.com"))
        self.comboX.setItemText(0, _translate("MainWindow", "0"))
        self.comboX.setItemText(1, _translate("MainWindow", "1"))
        self.comboY.setItemText(0, _translate("MainWindow", "0"))
        self.comboY.setItemText(1, _translate("MainWindow", "1"))
        self.submit.setText(_translate("MainWindow", "提交"))
        self.label.setText(_translate("MainWindow", "X XOR Y ="))

    def pressed(self):
        x = int(self.comboX.currentText())
        y = int(self.comboY.currentText())
        xor = (x and not y) or (not x and y)
        if xor == True:
            xor = 1
        else:
            xor = 0

        self.label.setText("X XOR Y =  " + str(xor))

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

 运行代码:

使用PyQt5和Qt Designer创建Python GUI应用程序(六)

 本文暂时先这样,有空再补充,请继续关注本文。

使用PyQt5和Qt Designer创建Python GUI应用程序https://www.linuxidc.com/search.aspx?where=nkey&keyword=65658

更多Python相关信息见Python 专题页面 https://www.linuxidc.com/topicnews.aspx?tid=17

Linux公社的RSS地址https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址https://www.linuxidc.com/Linux/2020-05/163186.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款