-
Notifications
You must be signed in to change notification settings - Fork 1
/
untitled.py
201 lines (180 loc) · 8.65 KB
/
untitled.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# 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.
import sys
import pygame
from PIL import Image, ImageFont, ImageDraw
import random
import time
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QThread
class happy:
def __init__(self, image_path, text_list, big_size=3):
self.big_size = big_size
self.image_path = image_path
self.image = Image.open(self.image_path)
self.image = self.image.resize((int(467 * self.big_size), int(350 * self.big_size)))
self.x0, self.y0 = self.image.size
self.draw = ImageDraw.Draw(self.image)
self.text_list = text_list
def write(self, text, high):
font = ImageFont.truetype('data/simhei.ttf', size=int(45 * self.big_size))
ascent, descent = font.getsize(text)
x = self.x0 / 2 - ascent / 2
self.draw.text((x, int(high * self.big_size)), text, fill=(255, 0, 0), color=(0, 255, 0), font=font)
def show(self):
self.image.show()
def run(self, write_type):
if write_type == 1:
for i in range(3):
self.write(self.text_list[i][0], self.text_list[i][1])
if write_type in [2, 3]:
self.write(self.text_list[0][0], self.text_list[0][1])
class mp(QThread):
def __init__(self, people, write_type, big_size):
super(mp, self).__init__()
self.people = people
self.write_type = write_type
self.book_list = open('data/book_list.txt', 'r', encoding='utf-8').readlines()
self.book_list = list(map(lambda x: x[0:-1], self.book_list))
self.big_size = big_size
def run(self):
people = self.people
people_list = list(range(1, people + 1))
if self.write_type == 1:
do = happy('data/background.png',
[[f'{random.choice(people_list)}号', 100], ['背诵', 150], [random.choice(self.book_list), 200]],
big_size=self.big_size)
do.run(1)
do.show()
if self.write_type == 2:
do = happy('data/background.png', [[f'{random.choice(people_list)}号', 150]], big_size=self.big_size)
do.run(2)
do.show()
if self.write_type == 3:
do = happy('data/background.png', [[random.choice(self.book_list), 150]], big_size=self.big_size)
do.run(3)
do.show()
class Ui_MainWindow(object):
def __init__(self):
self.write_type = 1
pygame.init()
pygame.mixer.music.load(r"data/bgm.mp3")
pygame.mixer.music.play()
self.io = True
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(800, 600)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy)
MainWindow.setMinimumSize(QtCore.QSize(800, 600))
MainWindow.setMaximumSize(QtCore.QSize(800, 600))
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("data/favicon.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.clicked.connect(self.pushButton_check)
self.pushButton.setGeometry(QtCore.QRect(260, 120, 240, 130))
self.pushButton.setCheckable(False)
self.pushButton.setObjectName("pushButton")
self.radioButton = QtWidgets.QRadioButton(self.centralwidget)
self.radioButton.setGeometry(QtCore.QRect(700, 520, 90, 50))
font = QtGui.QFont()
font.setPointSize(15)
self.radioButton.setFont(font)
self.radioButton.setToolTipDuration(-1)
self.radioButton.setChecked(True)
self.radioButton.setAutoExclusive(False)
self.radioButton.setObjectName("radioButton")
self.radioButton.clicked.connect(self.io_bgm)
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(300, 280, 186, 202))
self.widget.setObjectName("widget")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.widget)
self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.widget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.spinBox = QtWidgets.QSpinBox(self.widget)
self.spinBox.setProperty("value", 40)
self.spinBox.setObjectName("spinBox")
self.horizontalLayout.addWidget(self.spinBox)
self.verticalLayout_2.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_2 = QtWidgets.QLabel(self.widget)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.doubleSpinBox = QtWidgets.QDoubleSpinBox(self.widget)
self.doubleSpinBox.setProperty("value", 3.0)
self.doubleSpinBox.setObjectName("doubleSpinBox")
self.horizontalLayout_2.addWidget(self.doubleSpinBox)
self.verticalLayout_2.addLayout(self.horizontalLayout_2)
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.radioButton_2 = QtWidgets.QRadioButton(self.widget)
self.radioButton_2.setChecked(True)
self.radioButton_2.setObjectName("radioButton_2")
self.verticalLayout.addWidget(self.radioButton_2)
self.radioButton_2.clicked.connect(self.set_static_1)
self.radioButton_3 = QtWidgets.QRadioButton(self.widget)
self.radioButton_3.setObjectName("radioButton_3")
self.verticalLayout.addWidget(self.radioButton_3)
self.radioButton_3.clicked.connect(self.set_static_2)
self.radioButton_4 = QtWidgets.QRadioButton(self.widget)
self.radioButton_4.setObjectName("radioButton_4")
self.verticalLayout.addWidget(self.radioButton_4)
self.radioButton_4.clicked.connect(self.set_static_3)
self.verticalLayout_2.addLayout(self.verticalLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "快乐抽奖"))
self.pushButton.setText(_translate("MainWindow", "点击开始快乐抽奖"))
self.radioButton.setText(_translate("MainWindow", "BGM"))
self.label.setText(_translate("MainWindow", "班级人数"))
self.label_2.setText(_translate("MainWindow", "图片大小"))
self.radioButton_2.setText(_translate("MainWindow", "学号+篇目"))
self.radioButton_3.setText(_translate("MainWindow", "单学号"))
self.radioButton_4.setText(_translate("MainWindow", "单篇目"))
def pushButton_check(self):
self.thread = mp(int(self.spinBox.text()), self.write_type, big_size=float(self.doubleSpinBox.text()))
self.thread.start()
def set_static_1(self):
self.write_type = 1
def set_static_2(self):
self.write_type = 2
def set_static_3(self):
self.write_type = 3
def io_bgm(self):
if self.io:
pygame.mixer.music.pause()
self.io = False
else:
pygame.mixer.music.unpause()
self.io = True
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
Ui = Ui_MainWindow()
Ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())