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_()) 优化该代码,使窗口最大化且不可以放大缩小,具备打印取号和记录当天取号记录功能
时间: 2024-04-28 15:26:27 浏览: 227
pyqt5、qtdesigner安装和环境设置教程
5星 · 资源好评率100%
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_())
阅读全文