def button_click(self, number): current = self.screen.text() self.screen.setText(current + number) def button_clear(self): self.screen.clear() self.first_num = None self.operation = None def button_add(self): self.first_num = float(self.screen.text()) self.screen.clear() self.operation = "add" def button_subtract(self): self.first_num = float(self.screen.text()) self.screen.clear() self.operation = "subtract" def button_multiply(self): self.first_num = float(self.screen.text()) self.screen.clear() self.operation = "multiply" def button_divide(self): self.first_num = float(self.screen.text()) self.screen.clear() self.operation = "divide" def button_equal(self): second_num = float(self.screen.text()) self.screen.clear() if self.operation == "add": result = self.first_num + second_num elif self.operation == "subtract": result = self.first_num - second_num elif self.operation == "multiply": result = self.first_num * second_num elif self.operation == "divide": if second_num == 0: result = "除数不能为 0" else: result = self.first_num / second_num self.screen.setText(str(result))
时间: 2024-02-14 14:27:55 浏览: 77
这段代码是一个简单的计算器的实现。它使用了PyQt库来创建GUI界面,并实现了按钮点击事件的处理逻辑。
- `button_click(self, number)`函数用于处理数字按钮的点击事件。它从屏幕上获取当前显示的数字,然后将点击的数字添加到末尾。
- `button_clear(self)`函数用于处理清除按钮的点击事件。它清空屏幕上的数字,并重置计算器的状态。
- `button_add(self)`、`button_subtract(self)`、`button_multiply(self)`、`button_divide(self)`函数分别用于处理加、减、乘、除按钮的点击事件。它们分别将屏幕上显示的数字作为第一个操作数,并清空屏幕,同时设置相应的操作符。
- `button_equal(self)`函数用于处理等号按钮的点击事件。它从屏幕上获取第二个操作数,并根据之前设置的操作符进行相应的计算。最后,将计算结果显示在屏幕上。
这段代码实现了一个简单的计算器功能,用户可以通过GUI界面进行数字输入和基本运算。
相关问题
import sys from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QVBoxLayout, QHBoxLayout from PyQt5.QtCore import Qt class QueueSystem(QWidget): def __init__(self): super().__init__() self.queue = [] # 存储队列信息 self.current_number = 0 # 当前的序号 self.initUI() def initUI(self): # 创建控件 self.label_title = QLabel('排队取号系统', self) self.label_number = QLabel('当前序号:{}'.format(self.current_number), self) self.label_queue = QLabel('等待人数:{}'.format(len(self.queue)), self) self.button_get_number = QPushButton('取号', self) self.button_reset = QPushButton('重置', self) # 设置控件样式 self.label_title.setAlignment(Qt.AlignCenter) self.label_title.setStyleSheet('font-size: 24px;') self.label_number.setStyleSheet('font-size: 18px;') self.label_queue.setStyleSheet('font-size: 18px;') self.button_get_number.setStyleSheet('font-size: 18px;') self.button_reset.setStyleSheet('font-size: 18px;') # 创建布局 vbox = QVBoxLayout() vbox.addWidget(self.label_title) vbox.addWidget(self.label_number) vbox.addWidget(self.label_queue) hbox = QHBoxLayout() hbox.addWidget(self.button_get_number) hbox.addWidget(self.button_reset) vbox.addLayout(hbox) self.setLayout(vbox) # 连接信号槽 self.button_get_number.clicked.connect(self.get_number) self.button_reset.clicked.connect(self.reset) # 设置窗口属性 self.setWindowTitle('排队取号系统') self.setGeometry(300, 300, 300, 200) self.show() def get_number(self): self.current_number += 1 self.queue.append(self.current_number) self.update_info() def reset(self): self.current_number = 0 self.queue = [] self.update_info() def update_info(self): self.label_number.setText('当前序号:{}'.format(self.current_number)) self.label_queue.setText('等待人数:{}'.format(len(self.queue))) def notify(self, number): if len(self.queue) > 0 and self.queue[0] == number: self.queue.pop(0) self.update_info() print('叫号:{}'.format(number)) if __name__ == '__main__': app = QApplication(sys.argv) queue_system = QueueSystem() sys.exit(app.exec_()) 优化该代码,使窗口最大化且不可以放大缩小,具备打印取号和记录当天取号记录功能
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QVBoxLayout, QHBoxLayout, QPlainTextEdit, QSizePolicy
from PyQt5.QtGui import QFont
from PyQt5.QtCore import Qt
class QueueSystem(QWidget):
def __init__(self):
super().__init__()
self.queue = [] # 存储队列信息
self.current_number = 0 # 当前的序号
self.history = [] # 存储取号历史记录
self.initUI()
def initUI(self):
# 创建控件
self.label_title = QLabel('排队取号系统', self)
self.label_number = QLabel('当前序号:{}'.format(self.current_number), self)
self.label_queue = QLabel('等待人数:{}'.format(len(self.queue)), self)
self.button_get_number = QPushButton('取号', self)
self.button_reset = QPushButton('重置', self)
self.button_print_history = QPushButton('打印取号记录', self)
self.text_edit_history = QPlainTextEdit(self)
self.text_edit_history.setStyleSheet('font-size: 14px;')
self.text_edit_history.setReadOnly(True)
self.text_edit_history.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
font = QFont()
font.setFamily('Consolas')
self.text_edit_history.setFont(font)
# 设置控件样式
self.label_title.setAlignment(Qt.AlignCenter)
self.label_title.setStyleSheet('font-size: 24px;')
self.label_number.setStyleSheet('font-size: 18px;')
self.label_queue.setStyleSheet('font-size: 18px;')
self.button_get_number.setStyleSheet('font-size: 18px;')
self.button_reset.setStyleSheet('font-size: 18px;')
self.button_print_history.setStyleSheet('font-size: 18px;')
# 创建布局
vbox = QVBoxLayout()
vbox.addWidget(self.label_title)
vbox.addWidget(self.label_number)
vbox.addWidget(self.label_queue)
hbox1 = QHBoxLayout()
hbox1.addWidget(self.button_get_number)
hbox1.addWidget(self.button_reset)
hbox1.addWidget(self.button_print_history)
vbox.addLayout(hbox1)
vbox.addWidget(self.text_edit_history)
self.setLayout(vbox)
# 连接信号槽
self.button_get_number.clicked.connect(self.get_number)
self.button_reset.clicked.connect(self.reset)
self.button_print_history.clicked.connect(self.print_history)
# 设置窗口属性
self.setWindowTitle('排队取号系统')
self.setGeometry(300, 300, 300, 200)
self.setFixedSize(600, 500) # 设置窗口大小固定
self.show()
def get_number(self):
self.current_number += 1
self.queue.append(self.current_number)
self.history.append(self.current_number) # 记录取号历史记录
self.update_info()
def reset(self):
self.current_number = 0
self.queue = []
self.history = [] # 重置历史记录
self.update_info()
self.text_edit_history.clear()
def update_info(self):
self.label_number.setText('当前序号:{}'.format(self.current_number))
self.label_queue.setText('等待人数:{}'.format(len(self.queue)))
def notify(self, number):
if len(self.queue) > 0 and self.queue[0] == number:
self.queue.pop(0)
self.update_info()
print('叫号:{}'.format(number))
def print_history(self):
history_str = '\n'.join([str(num) for num in self.history]) # 将历史记录转换为字符串
self.text_edit_history.setPlainText(history_str)
if __name__ == '__main__':
app = QApplication(sys.argv)
queue_system = QueueSystem()
sys.exit(app.exec_())
pyqt5怎么显示class QueryThread(QThread): query_result = pyqtSignal(object) def __init__(self, id): super().__init__() self.id = id def run(self): conn = pymysql.connect(host='39.99.214.172', user='root', password='Solotion.123', database='jj_tset') cursor = conn.cursor() cursor.execute("SELECT * FROM employee_table WHERE user_id='%s'" % self.id) result = cursor.fetchone() conn.close() self.query_result.emit(result) # 发送查询结果信号 class MainWindow(QtWidgets.QMainWindow): def __init__(self): super().__init__() self.query_thread = None # 查询线程 def read_data(self): data = self.ser.readline() if data: id = data.decode().strip() if not self.query_thread: # 如果查询线程不存在,则创建并启动 self.query_thread = QueryThread(id) self.query_thread.query_result.connect(self.update_ui) self.query_thread.start() else: self.query_thread.id = id # 如果查询线程已存在,则更新查询ID QTimer.singleShot(100, self.read_data) def update_ui(self, result): if result: self.id_label.setText("员工ID:" + result[0]) self.name_label.setText("姓名:" + str(result[1])) self.six_label.setText("性别:" + result[2]) self.sfz_label.setText("身份证:" + str(result[3])) self.tel_label.setText("电话:" + result[4]) else: self.id_label.setText("员工ID:") self.name_label.setText("姓名:") self.six_label.setText("性别:") self.sfz_label.setText("身份证:") self.tel_label.setText("电话:") def closeEvent(self, event): self.ser.close() if self.query_thread: self.query_thread.quit() self.query_thread.wait()
要在 PyQt5 中显示这段代码,可以使用 QTextEdit 或者 QPlainTextEdit 组件。首先,需要在程序中导入相关的模块:
```python
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit
```
然后,创建一个 QTextEdit 组件,并将代码添加到其中:
```python
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.code_edit = QTextEdit(self)
self.code_edit.setReadOnly(True)
self.setCentralWidget(self.code_edit)
self.code_edit.setPlainText('''
class QueryThread(QThread):
query_result = pyqtSignal(object)
def __init__(self, id):
super().__init__()
self.id = id
def run(self):
conn = pymysql.connect(host='39.99.214.172', user='root', password='Solotion.123', database='jj_tset')
cursor = conn.cursor()
cursor.execute("SELECT * FROM employee_table WHERE user_id='%s'" % self.id)
result = cursor.fetchone()
conn.close()
self.query_result.emit(result) # 发送查询结果信号
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.query_thread = None # 查询线程
def read_data(self):
data = self.ser.readline()
if data:
id = data.decode().strip()
if not self.query_thread: # 如果查询线程不存在,则创建并启动
self.query_thread = QueryThread(id)
self.query_thread.query_result.connect(self.update_ui)
self.query_thread.start()
else:
self.query_thread.id = id # 如果查询线程已存在,则更新查询ID
QTimer.singleShot(100, self.read_data)
def update_ui(self, result):
if result:
self.id_label.setText("员工ID:" + result[0])
self.name_label.setText("姓名:" + str(result[1]))
self.six_label.setText("性别:" + result[2])
self.sfz_label.setText("身份证:" + str(result[3]))
self.tel_label.setText("电话:" + result[4])
else:
self.id_label.setText("员工ID:")
self.name_label.setText("姓名:")
self.six_label.setText("性别:")
self.sfz_label.setText("身份证:")
self.tel_label.setText("电话:")
def closeEvent(self, event):
self.ser.close()
if self.query_thread:
self.query_thread.quit()
self.query_thread.wait()
''')
```
这样就可以将代码显示在程序的主窗口中了。需要注意的是,这段代码中的 self.ser 没有定义,如果没有在程序中定义,会导致程序出错。
阅读全文