8
社区成员




import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QLabel, QRadioButton, QListWidget, QListWidgetItem, QToolBar, QMenu, QMessageBox, QLineEdit
from PySide6.QtGui import QAction, QIcon, QBrush, QColor
from PySide6.QtCore import Qt
class VotingApp(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("投票程序")
self.setGeometry(100, 100, 400, 600)
self.voters = ["张三", "李四", "王五", "赵六"] # 有资格投票的人
self.voted_voters = [] # 已经投票的人
self.init_ui()
self.init_menu()
self.init_toolbar()
def init_ui(self):
central_widget = QWidget(self)
self.setCentralWidget(central_widget)
layout = QVBoxLayout(central_widget)
# 姓名输入区域
self.name_label = QLabel("请输入您的姓名:")
layout.addWidget(self.name_label)
self.name_input = QLineEdit()
layout.addWidget(self.name_input)
# 投票选项区域
self.vote_label = QLabel("请选择投票选项:")
layout.addWidget(self.vote_label)
self.vote_radiobuttons = []
options = ["选项A", "选项B", "选项C", "选项D"]
for option in options:
radiobutton = QRadioButton(option)
radiobutton.toggled.connect(lambda checked, rb=radiobutton: self.on_radio_button_toggled(checked, rb))
self.vote_radiobuttons.append(radiobutton)
layout.addWidget(radiobutton)
# 结果显示区域
self.results_label = QLabel("当前投票结果:")
layout.addWidget(self.results_label)
self.results_list = QListWidget()
layout.addWidget(self.results_list)
# 投票人列表区域
self.voter_label = QLabel("已投票的人:")
layout.addWidget(self.voter_label)
self.voter_list = QListWidget()
layout.addWidget(self.voter_list)
# 有资格投票的人列表区域
self.eligible_voter_label = QLabel("有资格投票的人:")
layout.addWidget(self.eligible_voter_label)
self.eligible_voter_list = QListWidget()
layout.addWidget(self.eligible_voter_list)
self.votes = {option: 0 for option in options}
self.update_eligible_voter_list()
def init_menu(self):
# 创建菜单栏
menu_bar = self.menuBar()
# 创建“文件”菜单
file_menu = menu_bar.addMenu("文件")
# 添加“重置投票”菜单项
reset_action = QAction("重置投票", self)
reset_action.triggered.connect(self.reset_votes)
file_menu.addAction(reset_action)
# 添加“退出”菜单项
exit_action = QAction("退出", self)
exit_action.triggered.connect(self.close)
file_menu.addAction(exit_action)
# 创建“帮助”菜单
help_menu = menu_bar.addMenu("帮助")
# 添加“关于”菜单项
about_action = QAction("关于", self)
about_action.triggered.connect(self.show_about)
help_menu.addAction(about_action)
def init_toolbar(self):
# 创建工具栏
toolbar = QToolBar("工具栏")
self.addToolBar(toolbar)
# 添加“提交投票”按钮
submit_action = QAction(QIcon.fromTheme("document-save"), "提交投票", self)
submit_action.triggered.connect(self.submit_votes)
toolbar.addAction(submit_action)
# 添加“重置投票”按钮
reset_action = QAction(QIcon.fromTheme("edit-undo"), "重置投票", self)
reset_action.triggered.connect(self.reset_votes)
toolbar.addAction(reset_action)
# 添加“退出”按钮
exit_action = QAction(QIcon.fromTheme("application-exit"), "退出", self)
exit_action.triggered.connect(self.close)
toolbar.addAction(exit_action)
def on_radio_button_toggled(self, checked, radiobutton):
if checked and not self.name_input.text().strip():
QMessageBox.warning(self, "警告", "操作无效,请输入姓名!")
radiobutton.setChecked(False)
def submit_votes(self):
voter_name = self.name_input.text().strip()
if not voter_name:
QMessageBox.warning(self, "警告", "请输入您的姓名!")
self.clear_votes() # 清除投票选择
return
if voter_name not in self.voters:
QMessageBox.warning(self, "警告", "您没有投票资格,请联系管理员!")
self.clear_votes() # 清除投票选择
return
if voter_name in self.voted_voters:
QMessageBox.warning(self, "警告", "您已经投过票了!")
self.clear_votes() # 清除投票选择
return
checked_option = None
for radiobutton in self.vote_radiobuttons:
if radiobutton.isChecked():
checked_option = radiobutton.text()
break
if checked_option is None:
QMessageBox.warning(self, "警告", "请选择一个选项再投票!")
self.clear_votes() # 清除投票选择
return
self.votes[checked_option] += 1
self.update_results()
self.voted_voters.append(voter_name)
self.update_voter_list()
self.update_eligible_voter_list()
self.clear_votes() # 清除投票选择
def update_results(self):
self.results_list.clear()
for option, count in self.votes.items():
item = QListWidgetItem(f"{option}: {count}")
self.results_list.addItem(item)
def update_voter_list(self):
self.voter_list.clear()
for voter in self.voted_voters:
item = QListWidgetItem(voter)
item.setBackground(QBrush(QColor("red")))
self.voter_list.addItem(item)
def update_eligible_voter_list(self):
self.eligible_voter_list.clear()
for voter in self.voters:
item = QListWidgetItem(voter)
if voter in self.voted_voters:
item.setBackground(QBrush(QColor("red")))
self.eligible_voter_list.addItem(item)
def clear_votes(self):
for radiobutton in self.vote_radiobuttons:
radiobutton.setChecked(False)
def reset_votes(self):
for radiobutton in self.vote_radiobuttons:
radiobutton.setChecked(False)
self.votes = {option: 0 for option in self.votes.keys()}
self.update_results()
self.voted_voters = []
self.update_voter_list()
self.update_eligible_voter_list()
self.name_input.clear()
def show_about(self):
msg = "这是一个使用pyside6编写的投票桌面程序。\n\n版本:1.0\n作者:传奇开心果"
QMessageBox.about(self, "关于投票程序", msg)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = VotingApp()
window.show()
sys.exit(app.exec())
代码实现了一个投票桌面应用程序,使用PySide6库构建GUI界面。以下是该应用程序的主要功能和特点:用户界面:程序窗口包含多个组件,如标签、输入框、单选按钮、列表框和工具栏。用户可以通过输入姓名并选择投票选项来进行投票。投票逻辑:只有在voters列表中的用户才有资格投票。用户必须先输入姓名才能选择投票选项。同一用户只能投票一次,重复投票将被拒绝。投票后,结果会实时更新到结果列表中,并记录已投票的用户。结果显示:投票结果通过列表框显示,每个选项及其对应的票数都会实时更新。已投票的用户名单也会显示在一个列表框中,并用红色背景标记以示区分。有资格投票但尚未投票的用户名单同样会在另一个列表框中展示。菜单和工具栏:菜单栏包括“文件”和“帮助”两个菜单,其中“文件”菜单包含“重置投票”和“退出”选项,“帮助”菜单包含“关于”选项。工具栏提供了三个按钮:“提交投票”、“重置投票”和“退出”。事件处理:当用户尝试投票但未输入姓名时,会弹出警告提示。如果用户没有投票资格或已经投过票,则无法再次投票。选择投票选项时,如果未输入姓名,单选按钮会被取消选择,并弹出警告提示。其他功能:提供了“重置投票”功能,可以清空所有投票记录,使投票系统回到初始状态。“关于”对话框用于显示程序的基本信息,如版本号和作者。通过这些功能,该应用程序为用户提供了一个简单直观的投票体验,同时确保了投票过程的公正性和准确性。