源代码二

import tkinter
import random
import threading
import time
import random as rd
import tkinter as tk
from tkinter import messagebox
from tkinter import *

import os, sys
import re
from tkinter import *
from tkinter.font import Font
from tkinter.ttk import *
from tkinter.messagebox import *


Participant = ['ERROR', '奖品数不够']
class Application_ui(Frame):

    
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master.title('抽奖系统_模式二')
        self.master.geometry('584x694')
        self.createWidgets()

    def createWidgets(self):
        self.top = self.winfo_toplevel()

        self.style = Style()
        self.flag = True


        self.style.configure('Label1.TLabel',anchor='w', font=('微软雅黑',9))
        self.Label1 = Label(self.top, text='请在下方输入抽奖者人数', style='Label1.TLabel')
        self.Label1.place(relx=0.1, rely=0.08, relwidth=0.8, relheight=0.048)

        self.Text1Var = StringVar(value='')
        self.Text1 = Entry(self.top, textvariable=self.Text1Var, font=('微软雅黑',9))
        self.Text1.place(relx=0.1, rely=0.13, relwidth=0.500, relheight=0.067)

        self.style.configure('Command1.TButton',font=('微软雅黑',9))
        self.Command1 = Button(self.top, text='输入人数', command=self.Command1_Cmd, style='Command1.TButton')
        self.Command1.place(relx=0.650, rely=0.13, relwidth=0.200, relheight=0.067)
        

        self.style.configure('Label1.TLabel',anchor='w', font=('微软雅黑',9))
        self.Label1 = Label(self.top, text='请在下方输入抽奖者名称', style='Label1.TLabel')
        self.Label1.place(relx=0.100, rely=0.2, relwidth=0.8, relheight=0.048)

        self.Text2Var = StringVar(value='')
        self.Text2 = Entry(self.top, textvariable=self.Text2Var, font=('微软雅黑',9))
        self.Text2.place(relx=0.100, rely=0.25, relwidth=0.500, relheight=0.067)

        self.style.configure('Command2.TButton',font=('微软雅黑',9))
        self.Command2 = Button(self.top, text='参与抽奖', command=self.Command2_Cmd, style='Command2.TButton')
        self.Command2.place(relx=0.650, rely=0.25, relwidth=0.200, relheight=0.067)


        self.style.configure('Command3.TButton',font=('微软雅黑',9))
        self.Command3 = Button(self.top, text='开始抽奖', command=self.Command3_Cmd, style='Command3.TButton')
        self.Command3.place(relx=0.100, rely=0.35, relwidth=0.300, relheight=0.067)

        self.style.configure('Command4.TButton',font=('微软雅黑',9))
        self.Command4 = Button(self.top, text='结束抽奖', command=self.Command4_Cmd, style='Command4.TButton')
        self.Command4.place(relx=0.600, rely=0.35, relwidth=0.300, relheight=0.067)

        self.first = tkinter.Label(self.top,text='',font = ("微软雅黑", 20,"normal"))
        self.first.place(x=180,y=400,width=150,height=100)

        self.second = tkinter.Label(self.top,text='',font = ("微软雅黑", 20,"normal"))
        self.second['fg'] = 'red'
        self.second.place(x=180,y=500,width=150,height=100)

        self.third = tkinter.Label(self.top,text='',font = ("微软雅黑", 20,"normal"))
        self.third.place(x=180,y=600,width=150,height=100)



class Application(Application_ui):

    Participant = []
    n = 0
    cc = 0
    def __init__(self, master=None):
        Application_ui.__init__(self, master)

    #输入人数
    def Command1_Cmd(self, event=None):
        global c
        c = self.Text1Var.get()
        self.n = int(c)
        messagebox.showinfo('SUCCESS','输入人数成功!')
        pass


    #参与抽奖
    def Command2_Cmd(self, event=None):
        global commodity
        commodity = self.Text2Var.get()
        if (self.cc >= self.n):
            messagebox.showinfo('ERROR', '抽奖人数已满')
        else:
            self.Participant.append(commodity)
            self.cc = self.cc + 1
            messagebox.showinfo('SUCCESS',commodity +'参与抽奖成功!')
        pass


    #开始抽奖
    def Command3_Cmd(self, event=None):
        def switch():
            Application_ui.flag=True
            while Application_ui.flag:
                i = random.randint(0,(self.n)-1)
                self.first['text'] = self.second['text']
                self.second['text'] = self.third['text']
                self.third['text'] = self.Participant[i]
                time.sleep(0.1)
        if ( self.n <= 0):
            messagebox.showinfo('ERROR', '没有参与者,无法开始抽奖!')
        else:      
            t=threading.Thread(target=switch)
            t.start()
        pass


    #结束抽奖
    def Command4_Cmd(self, event=None):
        Application_ui.flag=False
        pass

    

if __name__ == "__main__":
    top = Tk()
    Application(top).mainloop()
    try: top.destroy()
    except: pass

猜你喜欢

转载自www.cnblogs.com/gameforpeace/p/11027623.html