|
10鱼币
想制作一个点名器,使得名字滚动起来,但是一运行就会未响应,停止按钮无法按下,且Label控件上的内容不会实时更新,只会在控制台显示内容
- from tkinter import *
- import random
- import time
- root = Tk()
- namelist = ['一号', '二号', '三号', '四号', '五号', '六号', '七号', '八号', '九号', '十号', '十一号', '十二号']
- var = StringVar()
- var.set('准备抽签')
- # 创建一个Label用于显示名字
- textLabel = Label(root, textvariable=var, font=('微软雅黑', 30), width=10)
- textLabel.grid(row=0, column=2, columnspan=2)
- is_start = True
- def randomname():
- global is_start
- while True:
- if is_start == False:
- break
- namerandom = random.choice(namelist)
- var.set(namerandom)
- print(namerandom)
- time.sleep(1)
- def randomnamestop():
- global is_start
- is_start = False
- theButton1 = Button(root, text="开始抽签", width=14, command=randomname)
- theButton1.grid(row=2, column=2)
- theButton2 = Button(root, text="停止", width=14, command=randomnamestop)
- theButton2.grid(row=2, column=3)
- mainloop()
复制代码
- from tkinter import *
- import random
- import time
- import threading
- root = Tk()
- namelist = ['一号', '二号', '三号', '四号', '五号', '六号', '七号', '八号', '九号', '十号', '十一号', '十二号']
- var = StringVar()
- var.set('准备抽签')
- # 创建一个Label用于显示名字
- textLabel = Label(root, textvariable=var, font=('微软雅黑', 30), width=10)
- textLabel.grid(row=0, column=2, columnspan=2)
- is_start = True
- def randomname():
- global is_start
- while True:
- while is_start:
- namerandom = random.choice(namelist)
- var.set(namerandom)
- print(namerandom)
- time.sleep(1)
- def randomnamestop():
- global is_start
- is_start = not is_start
- theButton2['text'] = '继续' if theButton2['text'] == '停止' else '停止'
- def start_randomname_thread():
- theButton1['state'] = 'disabled'
- thread = threading.Thread(target=randomname)
- thread.daemon = True
- thread.start()
- theButton1 = Button(root, text="开始抽签", width=14, command=start_randomname_thread)
- theButton1.grid(row=2, column=2)
- theButton2 = Button(root, text="停止", width=14, command=randomnamestop)
- theButton2.grid(row=2, column=3)
- mainloop()
复制代码
前面的任务没执行完,停止按钮是按不了的,给点名函数开个线程试试
|
最佳答案
查看完整内容
前面的任务没执行完,停止按钮是按不了的,给点名函数开个线程试试
|