前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >抽奖小程序

抽奖小程序

作者头像
用户6021899
发布2019-08-14 17:42:20
2.8K0
发布2019-08-14 17:42:20
举报
文章被收录于专栏:Python编程 pyqt matplotlib

本例涉及到的新的内容有:QComboBox, QSpinBox, QCheckBox,打开文件对话框和标准消息对话框的使用,布局的嵌套,多线程的应用。

程序的效果如图:

代码如下:

# _*_ coding:utf-8_*_ import sys from random import choice from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtCore import Qt,QThread,pyqtSignal

class MyThread(QThread):#自定义额外的线程 my_signal = pyqtSignal(int)# 信号类型:int

def __init__(self,wait_seconds=1,parent=None): super().__init__(parent) self.wait_seconds = wait_seconds

def run(self): i=1 while True:#可以是死循环,靠外部条件终线程 self.sleep(self.wait_seconds)#等待 self.my_signal.emit(i)#发射 int 信号 print(u"本轮已抽中%d人"% i) i += 1

class MyWidget(QWidget):#创建一个QWidget的 子类 def __init__(self,parent = None):#初始化函数 super().__init__(parent)#调用基类的初始化函数 #默认的列表,仅供演示 self.List = [u"001 小兔", u"002 小脑虎",u"003 佩奇",u"004 米奇", u"005 饭桶博士", u"006 唐老鸭", u"007 米妮",u"008 黛丝",u"009 高飞", u"010 汤姆", u"011 Jerry", u"012 奥特曼",u"013 小怪兽",u"014 猪爸爸", u"015 小强"] self.initUI() self.load_btn.clicked.connect(self.loadList) self.combo_box.currentIndexChanged.connect(self.comboxchanged)

self.spinBox.value() self.thread = MyThread(1) self.button.clicked.connect(self.thread_start) self.thread.my_signal.connect(self.updateUI) def loadList(self): filename, filetype= QFileDialog.getOpenFileName(self, u'选择名单文件', './',"Text Files (*.txt);;All Files (*)") if filename: f = open(filename) self.List = [line.strip() for line in f.readlines()]

def initUI(self): self.load_btn = QPushButton(u"载入全部名单") self.load_btn.setFixedWidth(100) label1 = QLabel(u"中奖名单:") self.browser = QTextBrowser()#多行文本浏览框 self.label2 = QLabel() self.label2.setFixedHeight(60) self.label2.setFont(QFont("Roman times",18,QFont.Bold)) self.label2.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.label2.setText(u"中奖者") self.label2.setFrameShape(QFrame.Panel) #NoFrame,Box,Panel,StyledPanel,HLine,VLine self.label2.setFrameShadow(QFrame.Raised) #Plain,Raised,Sunken

lable3 = QLabel(u"选择奖品等级:") lable3.setAlignment(Qt.AlignRight | Qt.AlignVCenter) self.combo_box = QComboBox() self.combo_box.addItem(u"一等奖") self.combo_box.addItem(u"二等奖") self.combo_box.addItem(u"三等奖") self.combo_box.setCurrentIndex(self.combo_box.count()-1)

lable4 = QLabel(u"中奖人数设置:") lable4.setAlignment(Qt.AlignRight | Qt.AlignVCenter) self.spinBox = QSpinBox() self.spinBox.setMinimum(1) self.spinBox.setMaximum(len(self.List)) self.spinBox.setValue(5)#三等奖人数默认值 self.checkBox = QCheckBox(u"可重复中奖") self.checkBox.setChecked(True)

hlayout = QHBoxLayout()#创建水平箱型布局 hlayout.addWidget(lable3) hlayout.addWidget(self.combo_box) hlayout.addWidget(lable4) hlayout.addWidget(self.spinBox) hlayout.addStretch() hlayout.addWidget(self.checkBox) self.button = QPushButton(u"开始抽奖") self.button.setFixedHeight(30) #self.button.resize(self.button.sizeHint())

vlayout = QVBoxLayout()#创建垂向箱型布局 vlayout.addWidget(self.load_btn)# vlayout.addWidget(label1)# vlayout.addWidget(self.browser)#往垂向箱型布局添加控件 vlayout.addWidget(self.label2) vlayout.addLayout(hlayout) vlayout.addWidget(self.button)

self.setLayout(vlayout)#设置self 的布局 self.resize(400,250) self.setWindowTitle(u"抽奖小程序") def updateUI(self, i):#int 信号传进来 winner = choice(self.List) self.List.remove(winner) self.winners.append(winner) self.label2.setText(winner) self.browser.append("%s (%s)"%(winner,self.combo_box.currentText())) if i >= self.spinBox.value():#若人数已抽够 self.thread.terminate()#终止额外线程 self.browser.append('') if self.checkBox.isChecked():#可重复抽中 self.List.extend(self.winners) else: self.spinBox.setMaximum(len(self.List)) self.button.setEnabled(True) self.spinBox.setEnabled(True) self.combo_box.setEnabled(True) self.checkBox.setEnabled(True) print()

def comboxchanged(self, index): if index ==0:#一等奖 self.spinBox.setValue(1)#默认中奖人数 elif index ==1:#二等奖 self.spinBox.setValue(3)#默认中奖人数 elif index ==2:#三等奖 self.spinBox.setValue(5)#默认中奖人数 else:pass def thread_start(self): if not self.spinBox.value(): self.label2.clear() return self.sender().setEnabled(False) self.spinBox.setEnabled(False) self.combo_box.setEnabled(False) self.checkBox.setEnabled(False) self.winners = [ ]

self.thread.start()#开始线程

def closeEvent(self, event): #消息对话框 reply = QMessageBox.question(self, 'Message',"Are you sure to quit?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if reply == QMessageBox.Yes:#退出前确认 self.thread.terminate()#终止额外的线程,否则主程序结束它还继续跑 event.accept() else: event.ignore() pass if __name__== '__main__': app = QApplication(sys.argv) widget = MyWidget() widget.show() #显示到屏幕 sys.exit(app.exec_())

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-01-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python可视化编程机器学习OpenCV 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云开发 CloudBase
云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档