一,概要
需求: 实现一个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格式加载到界面中即可。也可以不加
代码展示:
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. 最后执行界面没有独立模块化(个人比较懒因此没有另外创建类把这部分放进去)