pyqt5写一个抽奖程序(未美化)

今天闲来无事,用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_())

你可能感兴趣的:(python,python,pyqt,qt)