基于Python實(shí)現(xiàn)體育彩票選號(hào)器功能代碼實(shí)例
一,概要
需求: 實(shí)現(xiàn)一個(gè)GUI界面下的 6+1體育彩票選號(hào)器.
(1) 要求界面可以加載系統(tǒng)時(shí)間及開獎(jiǎng)時(shí)間
(2) 功能區(qū)完成人選及機(jī)選的功能
人選 --> 手動(dòng)輸入6+1位數(shù)字.前6位必須在0-9之間的數(shù)字.后1位必須是0-4之間的數(shù)字
機(jī)選 -->
(1) 填寫數(shù)量(加校驗(yàn)必須為數(shù)字且不能為空)點(diǎn)擊開始按鈕后把選舉的數(shù)字添加到展示界面中
(2) 允許選舉的數(shù)字重復(fù)及不重復(fù)兩種選擇
(3) 展示區(qū)的設(shè)置,顯示已選的彩票號(hào)碼
(4) 完成清空展示區(qū)內(nèi)容功能
(5) 完成關(guān)閉整個(gè)界面窗口功能
二,創(chuàng)建Sportslottery類
要求:通過(guò)面向?qū)ο蟮乃季S完成。GUI界面及功能區(qū)分開編寫
代碼中用到的圖片:
easyicon_net_64.ico格式這里不支持上傳.所以就不傳了.隨便網(wǎng)上找個(gè)png格式的圖片轉(zhuǎn)成.ico格式加載到界面中即可。也可以不加
代碼展示:
from tkinter import *import tkinter.ttk as tkimport datetimefrom tkinter.messagebox import *import tkinter.font as tfimport timeimport randomimport threadingclass Sportslottery(Tk): def __init__(self): super().__init__() self.title('體育彩票選號(hào)器') self.geometry('800x600+170+80') self.resizable(0, 0) self.iconbitmap(R'C:UsersAdministratorPycharmProjectsuntitled1GUI_體育彩票選號(hào)器photoeasyicon_net_64.ico') self['bg'] = 'lightblue' #全局變量 self.number_list = [] #存儲(chǔ)選票號(hào)碼的每一個(gè)值 self.input_datas_list = [] #存儲(chǔ)多次輸入的每一組7位數(shù)字 self.number_list01 = [0,1,2,3,4,5,6,7,8,9] #用于允許重復(fù)獲取數(shù)字的列表的前6位 self.number_list02 = [0,1,2,3,4] #用于允許重復(fù)獲取數(shù)字的列表的第7位 self.ready = 0 # 啟動(dòng)默認(rèn)標(biāo)識(shí)符 #自動(dòng)加載界面 self.SetupUI() def SetupUI(self): #設(shè)置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:UsersAdministratorPycharmProjectsuntitled1GUI_體育彩票選號(hào)器photobeijingtu.png') self.label_img = tk.Label(self,image=self.load_img) self.label_img.place(x=0,y=0) #設(shè)置title self.label01 = tk.Label(self,text ='6 + 1 體育彩票選號(hào)器',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='機(jī)選',width =410,height = 260) self.labelFrame_query04.place(x=380,y=280) #加載系統(tǒng)時(shí)間 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()) #時(shí)間格式處理 date01 = datetime.datetime.today() current_day = ' %d年%d月%d日 ' % (date01.year, date01.month, date01.day) #創(chuàng)建加載系統(tǒng)時(shí)間的Label標(biāo)簽 self.label02 = tk.Label(self,text='當(dāng)前時(shí)間: ' + 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(): ''' 通過(guò)time類中的.after(1000,trickit)方法生成動(dòng)態(tài)系統(tǒng)時(shí)間 :return: ''' currentTime = '當(dāng)前時(shí)間: ' + 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) #這條必須加,不然時(shí)間不會(huì)動(dòng)態(tài)顯示 #記載開獎(jiǎng)時(shí)間(這里沒(méi)有添加任何功能,只是顯示一個(gè)開獎(jiǎng)時(shí)間而已.后續(xù)可以單獨(dú)添加功能) date01 = datetime.datetime(2020, 5, 20, 20, 00, 00, 888888) #下面的小時(shí)分鐘秒的部分單純只是加載date01中的20:00:00而已 self.label03 = tk.Label(self,text = '開獎(jiǎng)時(shí)間: ' + 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 = '數(shù)量:',font = ('微軟雅黑',10,'bold'),style='label04.TLabel') self.label05.place(x = 10,y=80) self.label06 = tk.Label(self.labelFrame_query04,text = '是否允許重復(fù):',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) #機(jī)選Entry選框(默認(rèn)值設(shè)置為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) #填寫數(shù)量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 = '關(guān) 閉',command = self.close_Windows) self.button05.place(x = 680,y=560) #設(shè)置text,scroll bar(效果:已選選票區(qū)域如果票數(shù)過(guò)多整體畫面最右側(cè)顯示滾動(dòng)條) 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): ''' 人為選號(hào)添加方法 :return: ''' #獲取輸入的值 self.addition_number = self.var_add.get() #對(duì)輸入的值進(jìn)行驗(yàn)證 if not self.addition_number.strip().isdigit(): showinfo('系統(tǒng)消息','輸入的值必須為數(shù)字') return elif len(self.addition_number) != 7: showinfo('系統(tǒng)消息','輸入必須為【0-9】的7位數(shù)字') return else: str_6 = self.addition_number[0:6] #記錄輸入數(shù)字的前6位 str_last1 = self.addition_number[6] #記錄輸入數(shù)字的最后1位 #字符串拼接后插入到已選彩票框中 self.text.insert(1.0, '彩票編號(hào): ' + str_6 + ' ' + str_last1 + 'n') #設(shè)置輸入到文本中的字體大小 ft = tf.Font(family=’微軟雅黑’, size=16) #通過(guò)text.tag_add及text.tag_config方法設(shè)置選中下標(biāo)的字體顏色 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): ''' 清空已選彩票窗體中的所有內(nèi)容 :return: ''' self.text.delete(’1.0’, END) def close_Windows(self): ''' 關(guān)閉窗體 :return: ''' self.destroy() def draw(self): ''' 機(jī)選部分: 1. 輸入數(shù)字自動(dòng)滾動(dòng)生成每一位數(shù)字 2. 對(duì)輸入的值做驗(yàn)證必須為數(shù)字且不能為空 3. 獲取數(shù)字分為允許重復(fù)和不允許重復(fù) :return: ''' self.numb = self.var09.get() #獲取輸入的值 if not self.numb.isdigit() or self.numb == '': showinfo('系統(tǒng)消息', '【輸入必須為數(shù)字且不能為空】') else: if self.repeatable_check.get() == 0:self.repeatable() # 允許重復(fù)數(shù)字 else:self.non_repeatable() #不允許重復(fù)數(shù)字 def repeatable(self): ''' 生成的數(shù)字允許重復(fù) :return: ''' while self.ready <= int(self.numb): self.ready += 1 # 自動(dòng)選舉每個(gè)Entry標(biāo)簽的值并把選中的值賦值給entry標(biāo)簽 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) # 字符串拼接到展示區(qū) self.text.insert(1.0, '彩票編號(hào): ' + str(self.numb01) + str(self.numb02) + str(self.numb03) + str(self.numb04) + str(self.numb05) + str(self.numb06) + ' ' + str(self.numb07) + 'n') # 設(shè)置輸入到文本中的字體大小 ft = tf.Font(family=’微軟雅黑’, size=16) # 通過(guò)text.tag_add及text.tag_config方法設(shè)置選中下標(biāo)的字體顏色 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): ''' 生成的數(shù)字不允許重復(fù) :return: ''' while self.ready <= int(self.numb): self.ready += 1 #自動(dòng)選取第一到第六位的數(shù)字 seq1 = [i for i in range(0,10)] index_num = random.sample(seq1,6) #通過(guò)random.sample方法進(jìn)行選舉數(shù)字不重復(fù).一次生成6個(gè)數(shù)字 time.sleep(0.3) # 停頓0.3秒再獲取每個(gè)值 #把生成的list中的值分別賦值給每個(gè)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])) #自動(dòng)選舉第七位的數(shù)字 seq2 = [i for i in range(0,5)] index_num = random.sample(seq2,2) self.var08.set(index_num[0]) #獲取7位數(shù)字 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() # 字符串拼接到展示區(qū) self.text.insert(1.0, '彩票編號(hào): ' + str(self.number01) + str(self.number02) + str(self.number03) + str(self.number04) + str(self.number05) + str(self.number06) + ' ' + str(self.number07) + 'n') # 設(shè)置輸入到文本中的字體大小 ft = tf.Font(family=’微軟雅黑’, size=16) # 通過(guò)text.tag_add及text.tag_config方法設(shè)置選中下標(biāo)的字體顏色 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): ''' 開始按鍵的設(shè)置 :return: ''' self.ready=1 #此處必須啟動(dòng)新的線程,否則會(huì)卡死在開始按鈕 self.thread=threading.Thread(target=self.draw,args=()) #target=跟需要線程執(zhí)行的方法名.args=()線程執(zhí)行方法接收的參數(shù),該屬性是一個(gè)元組 self.thread.setDaemon(True) #設(shè)置守護(hù)線程(必須在start()方法調(diào)用之前設(shè)置,如果不設(shè)置為守護(hù)線程程序會(huì)被無(wú)限掛起) self.thread.start()if __name__ == '__main__': this_window = Sportslottery() this_window.mainloop()
三,缺陷分析
總結(jié):
1. 加載系統(tǒng)時(shí)間沒(méi)有完全模塊化 (對(duì)于面向?qū)ο蟮拈_發(fā)這里不足-----希望哪位大神看到此文,可以幫忙完成這里的面向?qū)ο蟛糠?
2. 開獎(jiǎng)時(shí)間這里是被寫死到晚上八點(diǎn)的.但是個(gè)人并沒(méi)有添加開獎(jiǎng)功能(感興趣的小伙伴可以開發(fā)這塊的功能實(shí)現(xiàn)真正開獎(jiǎng)后顯示中獎(jiǎng)號(hào)碼)
3. 不允許重復(fù)的地方第一次點(diǎn)擊,會(huì)不用點(diǎn)擊開始按鈕就自動(dòng)生成指定數(shù)量的票號(hào)(本人一時(shí)沒(méi)有想到好的解決辦法,希望大神們幫忙解決)
4. 最后執(zhí)行界面沒(méi)有獨(dú)立模塊化(個(gè)人比較懶因此沒(méi)有另外創(chuàng)建類把這部分放進(jìn)去)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python如何批量生成和調(diào)用變量2. windows服務(wù)器使用IIS時(shí)thinkphp搜索中文無(wú)效問(wèn)題3. Python基于requests實(shí)現(xiàn)模擬上傳文件4. ASP.NET MVC實(shí)現(xiàn)橫向展示購(gòu)物車5. 通過(guò)CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫特效6. python利用opencv實(shí)現(xiàn)顏色檢測(cè)7. Python 中如何使用 virtualenv 管理虛擬環(huán)境8. ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn)9. Python sorted排序方法如何實(shí)現(xiàn)10. Python獲取B站粉絲數(shù)的示例代碼
