python端代码 -> main.py
import sys, os
from PyQt5 import QtWidgets, uic
import random
BASE = os.path.dirname(os.path.abspath(__file__))
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
uic.loadUi(os.path.join(BASE, "ui-main.ui"), self)
self.pushButton.clicked.connect(self.on_click)
self.a, self.b, self.c = self.create_que()
self.label.setText(f'{self.a}+{self.b}=?')
self.pushButton.setShortcut('enter')
self.lineEdit.setFocus()
def create_que(self):
a, b = random.randint(1, 100), random.randint(1, 100)
c = a + b
return a, b, c
def on_click(self):
if f'{self.c}' == self.lineEdit.text():
print('正确')
self.a, self.b, self.c = self.create_que()
self.label.setText(f'{self.a}+{self.b}=?')
self.lineEdit.setFocus()
#
self.lineEdit.clear()
else:
print('错误')
self.lineEdit.setText('')
if __name__=="__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow();w.show()
sys.exit(app.exec_())
UI端代码 -> ui-main.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>350</width>
<height>342</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>90</x>
<y>190</y>
<width>111</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>提交</string>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>90</x>
<y>90</y>
<width>161</width>
<height>41</height>
</rect>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>18</pointsize>
</font>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit">
<property name="geometry">
<rect>
<x>90</x>
<y>160</y>
<width>113</width>
<height>20</height>
</rect>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>14</pointsize>
</font>
</property>
</widget>
<widget class="QGroupBox" name="groupBox">
<property name="geometry">
<rect>
<x>70</x>
<y>70</y>
<width>161</width>
<height>80</height>
</rect>
</property>
<property name="title">
<string>题目</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>350</width>
<height>23</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>