目的

掌握随机生成输

实现

代码

  import tkinter
  from tkinter import *
  import random
  
  going = True
  is_run = False
  Red = range(1, 34)
  Blue = range(1, 17)
  
  
  def lottery(redball, blueball):
      global going
      show_redNum = random.sample(Red, 6)
      show_blueNum = random.sample(Blue, 1)
      redball.set(show_redNum)
      blueball.set(show_blueNum)
      if going:
          window.after(50, lottery, redball, blueball)
      else:
          redBall.set(show_redNum)
          blueball.set(show_blueNum)
          going = True
          return
  
  
  def start(redball, blueball):
      global is_run
      if is_run:
          return
      is_run = True
      blueBall.set("")
      lottery(redball, blueball)
  
  
  def end():
      global going, is_run
      if is_run:
          going = False
          is_run = False
  
  
  if __name__ == '__main__':
      window = tkinter.Tk()
      window.geometry('405x250+250+15')
      window.title('双色球中奖号码生成器')
      bg_label = Label(window, width=70, height=20)
      bg_label.place(anchor=NW, x=0, y=0)
  
      label1 = Label(window, text="红球", justify='left', anchor=CENTER, width=30, height=3,
                     font='楷体 -25 bold', foreground='red')
      label1.place(anchor=NW, x=0, y=0)
      show_label2 = Label(window, text="蓝球", justify='right', anchor=CENTER, width=6, height=3,
                          font='楷体 -25 bold', foreground='blue')
      show_label2.place(anchor=NW, x=0, y=0)
  
      redBall = StringVar(value="幸运号码即将出现")
      blueBall = StringVar(value="注意:")
      show_label1 = Label(window, textvariable=redBall, justify='left', anchor=CENTER, width=30, height=3, bg='#BFEFFF',
                          font='楷体 -25 bold', foreground='red')
      show_label1.place(anchor=NW, x=10, y=80)
      show_label2 = Label(window, textvariable=blueBall, justify='right', anchor=CENTER, width=6, height=3, bg='#BFEFFF',
                          font='楷体 -25 bold', foreground='blue')
      show_label2.place(anchor=NW, x=0, y=80)
      button1 = Button(window, text='开始', command=lambda: start(redBall, blueBall), width=14, height=2, bg='#A8A8A8',
                       font='宋体 -18 bold')
      button1.place(anchor=NW, x=20, y=180)
      button2 = Button(window, text='结束', command=lambda: end(), width=14, height=2, bg='#A4A4A4',
                       font='宋体 -18 bold')
      button2.place(anchor=NW, x=232, y=180)
      window.mainloop()