大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
今天闲来无事,用python写了一个抽奖程序
pyqt版本:pyqt5
python版本:3.6.6
下面直接上代码,有建议的话,欢迎大家交流评论!
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Lucky_draw.ui'
#
# Created by: PyQt5 UI code generator 5.15.1
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import (QMainWindow, QApplication,QLabel,QLineEdit,QMessageBox,QComboBox)
from PyQt5.QtCore import Qt, QSize
from PyQt5.QtCore import Qt, QThread, pyqtSignal
import sys
import random
import time
tester = ["发", "我", "人", "他", "去", "个", "给", "如", "吃", "分"]
class Ui_Lucky_draw(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(658, 526)
self.horizontalLayoutWidget = QtWidgets.QWidget(Dialog)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(9, 9, 641, 401))
self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.textEdit_2 = QtWidgets.QTextEdit(self.horizontalLayoutWidget)
self.textEdit_2.setEnabled(False)
self.textEdit_2.setObjectName("textEdit_2")
self.horizontalLayout.addWidget(self.textEdit_2)
self.label = QtWidgets.QLabel(self.horizontalLayoutWidget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.textEdit = QtWidgets.QTextEdit(self.horizontalLayoutWidget)
self.textEdit.setEnabled(False)
self.textEdit.setObjectName("textEdit")
self.horizontalLayout.addWidget(self.textEdit)
self.verticalLayoutWidget = QtWidgets.QWidget(Dialog)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 430, 641, 91))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.pushButton = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.pushButton_2.setObjectName("pushButton_2")
self.verticalLayout.addWidget(self.pushButton_2)
qssStyle = ''' QPushButton{background-color:orange} '''
# 加载设置好的样式
self.verticalLayoutWidget.setStyleSheet(qssStyle)
self.font = QtGui.QFont()
self.font.setFamily("Arial") # 括号里可以设置成自己想要的其它字体
self.font.setPointSize(20) # 括号里的数字可以设置成自己想要的字体大小
self.textEdit.setFont(self.font)
self.textEdit_2.setFont(self.font)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "欢乐大抽奖"))
self.label.setText(_translate("Dialog", "-》"))
self.pushButton.setText(_translate("Dialog", "开始游戏"))
self.pushButton.clicked.connect(self.test)
#self.textEdit.setPlaceholderText("大奖究竟花落谁家,敬请期待!!!")
self.pushButton_2.setText(_translate("Dialog", "抽奖结束"))
self.pushButton_2.clicked.connect(self.jump_to_success)
def test(self):
thread_conut = 0
self.my_thread = MyThread() # 实例化线程对象
self.my_thread.my_signal.connect(self.set_label_func)
self.my_thread.is_on = True
self.my_thread.start() # 启动线程
self.pushButton.setEnabled(False)
self.textEdit.setText("大奖究竟花落谁家,敬请期待!!!")
# 线程自定义信号连接的槽函数
def set_label_func(self, num):
global tester
self.textEdit_2.setPlainText(num)
# 由于自定义信号时自动传递一个字符串参数,所以在这个槽函数中要接受一个参数
def jump_to_success(self):
#tester = ["发", "我", "人", "他", "去", "个", "给", "如", "吃", "分"]
t = random.randint(0, 9)
print(random.randint(0, 9))
print('选中的幸运儿是:', tester[t], '新年快乐!')
self.textEdit.setText('选中的幸运儿是:'+ tester[t]+' 新年快乐!')
self.textEdit_2.setPlainText(tester[t])
self.my_thread.is_on = False
self.my_thread.count = 0
self.pushButton.setEnabled(True)
# 线程类
class MyThread(QThread):
# 自定义信号对象。参数str就代表这个信号可以传一个字符串
my_signal = pyqtSignal(str)
def __init__(self):
super(MyThread, self).__init__()
self.count = 0
self.is_on = True
def run(self): #线程执行函数
global tester
while self.count < (len(tester)):
while self.is_on:
print('次数',self.count+1)
self.my_signal.emit(str(tester[self.count])) #释放自定义的信号
self.count = random.randint(0, 9)
#通过自定义信号把str(self.count)传递给槽函数
self.sleep(1) #本线程睡眠n秒【是QThread函数】
if __name__ == "__main__":
app = QApplication(sys.argv)
form = QtWidgets.QWidget()
window = Ui_Lucky_draw()
window.setupUi(form)
form.show()
sys.exit(app.exec_())
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/184383.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...