1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| import time import tkinter as tk import random
window = tk.Tk()
var1 = tk.StringVar() text = tk.Label(window, textvariable=var1) text.grid(row=0, column=1)
going = True is_run = False
def getnum(var1): global going a = random.randint(0, 100000) str_a=str(a) var1.set(str_a) if going: window.after(100, getnum, var1) else: going = True return
def start(var1): global is_run if is_run: return is_run = True getnum(var1)
def stop(): global going, is_run if is_run: going = False is_run = False
but = tk.Button(window, text="开始", command=lambda: start(var1)) but.grid(row=1, column=1) but2 = tk.Button(window, text="结束", command=stop) but2.grid(row=1, column=2)
window.mainloop()
|