基于Python實現體育彩票選號器功能代碼實例
一,概要
需求: 實現一個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 tkimport datetimefrom tkinter.messagebox import *import tkinter.font as tfimport timeimport randomimport threadingclass Sportslottery(Tk): def __init__(self): super().__init__() self.title('體育彩票選號器') self.geometry('800x600+170+80') self.resizable(0, 0) self.iconbitmap(R'C:UsersAdministratorPycharmProjectsuntitled1GUI_體育彩票選號器photoeasyicon_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:UsersAdministratorPycharmProjectsuntitled1GUI_體育彩票選號器photobeijingtu.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. 最后執行界面沒有獨立模塊化(個人比較懶因此沒有另外創建類把這部分放進去)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章: