基于Python实现体育彩票选号器功能代码实例

时间:2022-03-02 20:18:39

一,概要

需求: 实现一个GUI界面下的 6+1体育彩票选号器.

(1) 要求界面可以加载系统时间及开奖时间

(2) 功能区完成人选及机选的功能

人选 --> 手动输入6+1位数字.前6位必须在0-9之间的数字.后1位必须是0-4之间的数字

机选 -->

(1) 填写数量(加校验必须为数字且不能为空)点击开始按钮后把选举的数字添加到展示界面中

(2) 允许选举的数字重复及不重复两种选择

(3) 展示区的设置,显示已选的彩票号码

(4) 完成清空展示区内容功能

(5) 完成关闭整个界面窗口功能

二,创建Sportslottery类

要求:通过面向对象的思维完成。GUI界面及功能区分开编写

代码中用到的图片

easyicon_net_64.ico格式这里不支持上传.所以就不传了.随便网上找个png格式的图片转成.ico格式加载到界面中即可。也可以不加

基于Python实现体育彩票选号器功能代码实例

代码展示:

?
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
from tkinter import *
import tkinter.ttk as tk
import datetime
from tkinter.messagebox import *
import tkinter.font as tf
import time
import random
import threading
 
class Sportslottery(Tk):
  def __init__(self):
    super().__init__()
    self.title("体育彩票选号器")
    self.geometry("800x600+170+80")
    self.resizable(0, 0)
    self.iconbitmap(R"C:\Users\Administrator\PycharmProjects\untitled1\GUI_体育彩票选号器\photo\easyicon_net_64.ico")
    self["bg"] = "lightblue"
 
 
    #全局变量
    self.number_list = [] #存储选票号码的每一个值
    self.input_datas_list = [] #存储多次输入的每一组7位数字
    self.number_list01 = [0,1,2,3,4,5,6,7,8,9] #用于允许重复获取数字的列表的前6位
    self.number_list02 = [0,1,2,3,4] #用于允许重复获取数字的列表的第7位
    self.ready = 0 # 启动默认标识符
 
    #自动加载界面
    self.SetupUI()
 
  def SetupUI(self):
    #设置Style
    self.style01 = tk.Style()
    self.style01.configure("title.TLabel",foreground = "RoyalBlue")
    self.style01.configure("TPanedwindow", foreground="RoyalBlue")
    self.style01.configure("label02.TLabel", foreground="blue")
    self.style01.configure("label03.TLabel", foreground="red")
    self.style01.configure("label04.TLabel", foreground="blue")
    self.style01.configure("TButton", foreground="black")
 
 
    # 加载窗体图片
    self.load_img = PhotoImage(file=R"C:\Users\Administrator\PycharmProjects\untitled1\GUI_体育彩票选号器\photo\beijingtu.png")
    self.label_img = tk.Label(self,image=self.load_img)
    self.label_img.place(x=0,y=0)
 
    #设置title
    self.label01 = tk.Label(self,text ="6 + 1 体育彩票选号器",style="title.TLabel",font = ("微软雅黑",30,"bold"))
    self.label01.place(x =180,y =20)
 
    self.labelFrame_query01 = tk.LabelFrame(self,width =380,height = 60)
    self.labelFrame_query01.place(x=0,y=100)
 
    self.labelFrame_query02 = tk.LabelFrame(self,width =370,height = 60)
    self.labelFrame_query02.place(x=420,y=100)
 
    self.labelFrame_query03 = tk.LabelFrame(self,text="人选",width =410,height = 60)
    self.labelFrame_query03.place(x=380,y=200)
 
    self.labelFrame_query04 = tk.LabelFrame(self,text="机选",width =410,height = 260)
    self.labelFrame_query04.place(x=380,y=280)
 
    #加载系统时间
    def get_week_day(date):
      """
      :param date: 生成中文的星期
      :return:
      """
      week_day_dict = {
        0: '星期一',
        1: '星期二',
        2: '星期三',
        3: '星期四',
        4: '星期五',
        5: '星期六',
        6: '星期天',
      }
      day = date.weekday()
      return week_day_dict[day]
    current_week = get_week_day(datetime.datetime.now())
 
    #时间格式处理
    date01 = datetime.datetime.today()
    current_day = " %d年%d月%d日 " % (date01.year, date01.month, date01.day)
 
    #创建加载系统时间的Label标签
    self.label02 = tk.Label(self,text="当前时间: " + current_day + time.strftime('%H:%M:%S', time.localtime(time.time())) + " "+ current_week,font = ("微软雅黑",12,"bold")
                ,style="label02.TLabel")
    self.label02.place(x = 20,y=120)
 
    def trickit():
      """
      通过time类中的.after(1000,trickit)方法生成动态系统时间
      :return:
      """
      currentTime = "当前时间: " + current_day + time.strftime('%H:%M:%S', time.localtime(time.time())) + " " + current_week
      self.label02.config(text=currentTime)
      self.update()
      self.label02.after(1000, trickit)
    self.label02.after(1000, trickit) #这条必须加,不然时间不会动态显示
 
    #记载开奖时间(这里没有添加任何功能,只是显示一个开奖时间而已.后续可以单独添加功能)
    date01 = datetime.datetime(2020, 5, 20, 20, 00, 00, 888888)
 
    #下面的小时分钟秒的部分单纯只是加载date01中的20:00:00而已
    self.label03 = tk.Label(self,text = "开奖时间: " + current_day + date01.strftime("%H:%M:%S"),font = ("微软雅黑",12,"bold")
                ,style="label03.TLabel")
    self.label03.place(x = 460,y = 120)
 
    self.label04 = tk.Label(self,text = "已选彩票:",font = ("微软雅黑",12,"bold"),style="label04.TLabel")
    self.label04.place(x = 20,y=170)
 
    self.label05 = tk.Label(self.labelFrame_query04,text = "数量:",font = ("微软雅黑",10,"bold"),style="label04.TLabel")
    self.label05.place(x = 10,y=80)
 
    self.label06 = tk.Label(self.labelFrame_query04,text = "是否允许重复:",font = ("微软雅黑",10,"bold"),style="label04.TLabel")
    self.label06.place(x = 10,y=140)
 
    self.repeatable_check = IntVar()
    self.radio_repeatable = tk.Radiobutton(self.labelFrame_query04,text = "允许",variable = self.repeatable_check,value = 0,
                     command = self.draw)
    self.radio_repeatable.place(x = 10,y=170)
    self.radio_non_repeatable = tk.Radiobutton(self.labelFrame_query04,text = "不允许",variable = self.repeatable_check,value = 1,
                     command = self.draw)
    self.radio_non_repeatable.place(x = 70,y=170)
 
    #人选Entry选框
    self.var_add = StringVar()
    self.entry01 = tk.Entry(self.labelFrame_query03,textvariable=self.var_add,font = ("微软雅黑",12,"bold"))
    self.entry01.place(x = 0,y=0,width=250,height=35)
 
    #机选Entry选框(默认值设置为0)
    self.var02 = StringVar(value=" 0 ")
    self.entry02 = tk.Entry(self.labelFrame_query04,state = DISABLED,textvariable=self.var02,font = ("微软雅黑",24,"bold"))
    self.entry02.place(x = 2,y=5,width=50,height=50)
 
    self.var03 = StringVar(value=" 0 ")
    self.entry03 = tk.Entry(self.labelFrame_query04,state = DISABLED,textvariable=self.var03,font = ("微软雅黑",24,"bold"))
    self.entry03.place(x = 60,y=5,width=50,height=50)
 
    self.var04 = StringVar(value=" 0 ")
    self.entry04 = tk.Entry(self.labelFrame_query04,state = DISABLED,textvariable=self.var04,font = ("微软雅黑",24,"bold"))
    self.entry04.place(x = 118,y=5,width=50,height=50)
 
    self.var05 = StringVar(value=" 0 ")
    self.entry05 = tk.Entry(self.labelFrame_query04,state = DISABLED,textvariable=self.var05,font = ("微软雅黑",24,"bold"))
    self.entry05.place(x = 176,y=5,width=50,height=50)
 
    self.var06 = StringVar(value=" 0 ")
    self.entry06 = tk.Entry(self.labelFrame_query04,state = DISABLED,textvariable=self.var06,font = ("微软雅黑",24,"bold"))
    self.entry06.place(x = 234,y=5,width=50,height=50)
 
    self.var07 = StringVar(value=" 0 ")
    self.entry07 = tk.Entry(self.labelFrame_query04,state = DISABLED,textvariable=self.var07,font = ("微软雅黑",24,"bold"))
    self.entry07.place(x = 292,y=5,width=50,height=50)
 
    self.var08 = StringVar(value=" 0 ")
    self.entry08 = tk.Entry(self.labelFrame_query04,foreground = "red",state = DISABLED,textvariable=self.var08,font = ("微软雅黑",24,"bold"))
    self.entry08.place(x = 350,y=5,width=50,height=50)
 
    #填写数量Entry选框
    self.var09 = StringVar()
    self.entry09 = tk.Entry(self.labelFrame_query04,textvariable=self.var09,font = ("微软雅黑",14,"bold"))
    self.entry09.place(x = 50,y=75,width=50)
 
    self.button01 = tk.Button(self.labelFrame_query03,text = "添 加",command = self.Artificial_addition_number)
    self.button01.place(x = 280,y=5)
 
    self.button02 = tk.Button(self.labelFrame_query04,text = "开 始",command=self.start)
    self.button02.place(x = 295,y = 170)
 
    self.button04 = tk.Button(self,text = "清 空",command = self.clear_all)
    self.button04.place(x = 380,y=560)
 
    self.button05 = tk.Button(self,text = "关 闭",command = self.close_Windows)
    self.button05.place(x = 680,y=560)
 
    #设置text,scroll bar(效果:已选选票区域如果票数过多整体画面最右侧显示滚动条)
    self.text = Text(self,width=51, height=30)
    self.scroll = Scrollbar(self,width=4,command=self.text.yview)
    self.text.configure(yscrollcommand=self.scroll.set)
    self.scroll.pack(side=RIGHT, fill=Y)
    self.text.place(x=0,y=200)
 
  def Artificial_addition_number(self):
    """
    人为选号添加方法
    :return:
    """
    #获取输入的值
    self.addition_number = self.var_add.get()
 
    #对输入的值进行验证
    if not self.addition_number.strip().isdigit():
      showinfo("系统消息","输入的值必须为数字")
      return
    elif len(self.addition_number) != 7:
      showinfo("系统消息","输入必须为【0-9】的7位数字")
      return
    else:
      str_6 = self.addition_number[0:6] #记录输入数字的前6位
      str_last1 = self.addition_number[6] #记录输入数字的最后1位
 
      #字符串拼接后插入到已选彩票框中
      self.text.insert(1.0, "彩票编号: " + str_6 + "  " + str_last1 + "\n")
 
      #设置输入到文本中的字体大小
      ft = tf.Font(family='微软雅黑', size=16)
 
      #通过text.tag_add及text.tag_config方法设置选中下标的字体颜色
      self.text.tag_add('1', '1.0', '1.11', '1.11')
      self.text.tag_config('1', foreground='black',font=ft)
      self.text.tag_add('fag', '1.15', '1.16', '1.15')
      self.text.tag_config('fag', background='yellow', foreground='red',font = ft)
 
  def clear_all(self):
    """
    清空已选彩票窗体中的所有内容
    :return:
    """
    self.text.delete('1.0', END)
 
  def close_Windows(self):
    """
    关闭窗体
    :return:
    """
    self.destroy()
 
  def draw(self):
    """
    机选部分: 1. 输入数字自动滚动生成每一位数字
         2. 对输入的值做验证必须为数字且不能为空
         3. 获取数字分为允许重复和不允许重复
    :return:
    """
    self.numb = self.var09.get() #获取输入的值
    if not self.numb.isdigit() or self.numb == "":
      showinfo("系统消息", "【输入必须为数字且不能为空】")
    else:
      if self.repeatable_check.get() == 0:
        self.repeatable() # 允许重复数字
      else:
        self.non_repeatable() #不允许重复数字
 
  def repeatable(self):
    """
    生成的数字允许重复
    :return:
    """
    while self.ready <= int(self.numb):
      self.ready += 1
      # 自动选举每个Entry标签的值并把选中的值赋值给entry标签
      index_num = random.randint(1, 1000) % len(self.number_list01)
      self.numb01 = self.number_list01[index_num]
      self.var02.set(self.numb01)
 
      index_num = random.randint(1, 1000) % len(self.number_list01)
      self.numb02 = self.number_list01[index_num]
      self.var03.set(self.numb02)
 
      index_num = random.randint(1, 1000) % len(self.number_list01)
      self.numb03 = self.number_list01[index_num]
      self.var04.set(self.numb03)
 
      index_num = random.randint(1, 1000) % len(self.number_list01)
      self.numb04 = self.number_list01[index_num]
      self.var05.set(self.numb04)
 
      index_num = random.randint(1, 1000) % len(self.number_list01)
      self.numb05 = self.number_list01[index_num]
      self.var06.set(self.numb05)
 
      index_num = random.randint(1, 1000) % len(self.number_list01)
      self.numb06 = self.number_list01[index_num]
      self.var07.set(self.numb06)
 
      index_num = random.randint(1, 1000) % len(self.number_list02)
      self.numb07 = self.number_list02[index_num]
      self.var08.set(self.numb07)
 
      # 字符串拼接到展示区
      self.text.insert(1.0, "彩票编号: " + str(self.numb01) + str(self.numb02) + str(self.numb03) +
               str(self.numb04) + str(self.numb05) + str(self.numb06) + "  " + str(self.numb07) + "\n")
      # 设置输入到文本中的字体大小
      ft = tf.Font(family='微软雅黑', size=16)
 
      # 通过text.tag_add及text.tag_config方法设置选中下标的字体颜色
      self.text.tag_add('1', '1.0', '1.11', '1.11')
      self.text.tag_config('1', foreground='black', font=ft)
      self.text.tag_add('fag', '1.15', '1.16', '1.15')
      self.text.tag_config('fag', background='yellow', foreground='red', font=ft)
 
  def non_repeatable(self):
    """
    生成的数字不允许重复
    :return:
    """
    while self.ready <= int(self.numb):
      self.ready += 1
      #自动选取第一到第六位的数字
      seq1 = [i for i in range(0,10)]
      index_num = random.sample(seq1,6) #通过random.sample方法进行选举数字不重复.一次生成6个数字
      time.sleep(0.3) # 停顿0.3秒再获取每个值
 
      #把生成的list中的值分别赋值给每个entry选框中
      self.var02.set(index_num[0])
      self.var03.set(index_num[1])
      self.var04.set((index_num[2]))
      self.var05.set((index_num[3]))
      self.var06.set((index_num[4]))
      self.var07.set((index_num[5]))
 
      #自动选举第七位的数字
      seq2 = [i for i in range(0,5)]
      index_num = random.sample(seq2,2)
      self.var08.set(index_num[0])
 
      #获取7位数字
      self.number01 = self.var02.get()
      self.number02 = self.var03.get()
      self.number03 = self.var04.get()
      self.number04 = self.var05.get()
      self.number05 = self.var06.get()
      self.number06 = self.var07.get()
      self.number07 = self.var08.get()
 
      # 字符串拼接到展示区
      self.text.insert(1.0, "彩票编号: " + str(self.number01) + str(self.number02) + str(self.number03) +
               str(self.number04) + str(self.number05) + str(self.number06) + "  " + str(self.number07) + "\n")
 
      # 设置输入到文本中的字体大小
      ft = tf.Font(family='微软雅黑', size=16)
 
      # 通过text.tag_add及text.tag_config方法设置选中下标的字体颜色
      self.text.tag_add('1', '1.0', '1.11', '1.11')
      self.text.tag_config('1', foreground='black', font=ft)
      self.text.tag_add('fag', '1.15', '1.16', '1.15')
      self.text.tag_config('fag', background='yellow', foreground='red', font=ft)
 
  def start(self):
    """
    开始按键的设置
    :return:
    """
    self.ready=1
    #此处必须启动新的线程,否则会卡死在开始按钮
    self.thread=threading.Thread(target=self.draw,args=()) #target=跟需要线程执行的方法名.args=()线程执行方法接收的参数,该属性是一个元组
    self.thread.setDaemon(True) #设置守护线程(必须在start()方法调用之前设置,如果不设置为守护线程程序会被无限挂起)
    self.thread.start()
 
if __name__ == "__main__":
  this_window = Sportslottery()
  this_window.mainloop()

三,缺陷分析

总结:

1. 加载系统时间没有完全模块化 (对于面向对象的开发这里不足-----希望哪位大神看到此文,可以帮忙完成这里的面向对象部分)

2. 开奖时间这里是被写死到晚上八点的.但是个人并没有添加开奖功能(感兴趣的小伙伴可以开发这块的功能实现真正开奖后显示中奖号码)

3. 不允许重复的地方第一次点击,会不用点击开始按钮就自动生成指定数量的票号(本人一时没有想到好的解决办法,希望大神们帮忙解决)

4. 最后执行界面没有独立模块化(个人比较懒因此没有另外创建类把这部分放进去)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/liupengpengg/p/12973951.html