使用PyQt5和Qt Designer创建Python GUI应用程序(六)
。我们将使用组合框来允许用户选择两个二进制输入(0或1),然后显示应用于它们的XOR函数的结果。
self.comboX.addItem("Linux公社 www.linuxidc.com")
index = self.comboX.findText("1", QtCore.QtMatchFixedString) # 查找所需项目的索引
self.comboX.setCurrentIndex(index) # 设置索引(您也可以只为变量索引放入一个整数)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
...
self.submit.clicked.connect(self.pressed)
def pressed(self):
pass
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_())
GUI应用程序 见 https://www.linuxidc.com/search.aspx?where=nkey&keyword=65658
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx
本文永久更新链接地址:https://www.linuxidc.com/Linux/2020-05/163186.htm
微信赞赏支付宝赞赏