鱼C论坛

 找回密码
 立即注册
查看: 2089|回复: 4

[已解决]Tkinter如何使得名字滚动起来

[复制链接]
发表于 2021-10-1 14:45:48 | 显示全部楼层 |阅读模式
10鱼币
想制作一个点名器,使得名字滚动起来,但是一运行就会未响应,停止按钮无法按下,且Label控件上的内容不会实时更新,只会在控制台显示内容
屏幕截图 2021-10-01 144409.png

  1. from tkinter import *
  2. import random
  3. import time

  4. root = Tk()

  5. namelist = ['一号', '二号', '三号', '四号', '五号', '六号', '七号', '八号', '九号', '十号', '十一号', '十二号']

  6. var = StringVar()
  7. var.set('准备抽签')

  8. # 创建一个Label用于显示名字
  9. textLabel = Label(root, textvariable=var, font=('微软雅黑', 30), width=10)
  10. textLabel.grid(row=0, column=2, columnspan=2)


  11. is_start = True

  12. def randomname():
  13.     global is_start
  14.     while True:
  15.         if is_start == False:
  16.             break
  17.         namerandom = random.choice(namelist)
  18.         var.set(namerandom)
  19.         print(namerandom)
  20.         time.sleep(1)

  21. def randomnamestop():
  22.     global is_start
  23.     is_start = False

  24. theButton1 = Button(root, text="开始抽签", width=14, command=randomname)
  25. theButton1.grid(row=2, column=2)
  26. theButton2 = Button(root, text="停止", width=14, command=randomnamestop)
  27. theButton2.grid(row=2, column=3)

  28. mainloop()  
复制代码
最佳答案
2021-10-1 14:45:49
  1. from tkinter import *
  2. import random
  3. import time
  4. import threading

  5. root = Tk()

  6. namelist = ['一号', '二号', '三号', '四号', '五号', '六号', '七号', '八号', '九号', '十号', '十一号', '十二号']

  7. var = StringVar()
  8. var.set('准备抽签')

  9. # 创建一个Label用于显示名字
  10. textLabel = Label(root, textvariable=var, font=('微软雅黑', 30), width=10)
  11. textLabel.grid(row=0, column=2, columnspan=2)


  12. is_start = True

  13. def randomname():
  14.     global is_start
  15.     while True:
  16.         while is_start:
  17.             namerandom = random.choice(namelist)
  18.             var.set(namerandom)
  19.             print(namerandom)
  20.             time.sleep(1)

  21. def randomnamestop():
  22.     global is_start
  23.     is_start = not is_start
  24.     theButton2['text'] = '继续' if theButton2['text'] == '停止' else '停止'

  25. def start_randomname_thread():
  26.     theButton1['state'] = 'disabled'
  27.     thread = threading.Thread(target=randomname)
  28.     thread.daemon = True
  29.     thread.start()

  30. theButton1 = Button(root, text="开始抽签", width=14, command=start_randomname_thread)
  31. theButton1.grid(row=2, column=2)
  32. theButton2 = Button(root, text="停止", width=14, command=randomnamestop)
  33. theButton2.grid(row=2, column=3)

  34. mainloop()
复制代码

前面的任务没执行完,停止按钮是按不了的,给点名函数开个线程试试

最佳答案

查看完整内容

前面的任务没执行完,停止按钮是按不了的,给点名函数开个线程试试
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-1 14:45:49 | 显示全部楼层    本楼为最佳答案   
  1. from tkinter import *
  2. import random
  3. import time
  4. import threading

  5. root = Tk()

  6. namelist = ['一号', '二号', '三号', '四号', '五号', '六号', '七号', '八号', '九号', '十号', '十一号', '十二号']

  7. var = StringVar()
  8. var.set('准备抽签')

  9. # 创建一个Label用于显示名字
  10. textLabel = Label(root, textvariable=var, font=('微软雅黑', 30), width=10)
  11. textLabel.grid(row=0, column=2, columnspan=2)


  12. is_start = True

  13. def randomname():
  14.     global is_start
  15.     while True:
  16.         while is_start:
  17.             namerandom = random.choice(namelist)
  18.             var.set(namerandom)
  19.             print(namerandom)
  20.             time.sleep(1)

  21. def randomnamestop():
  22.     global is_start
  23.     is_start = not is_start
  24.     theButton2['text'] = '继续' if theButton2['text'] == '停止' else '停止'

  25. def start_randomname_thread():
  26.     theButton1['state'] = 'disabled'
  27.     thread = threading.Thread(target=randomname)
  28.     thread.daemon = True
  29.     thread.start()

  30. theButton1 = Button(root, text="开始抽签", width=14, command=start_randomname_thread)
  31. theButton1.grid(row=2, column=2)
  32. theButton2 = Button(root, text="停止", width=14, command=randomnamestop)
  33. theButton2.grid(row=2, column=3)

  34. mainloop()
复制代码

前面的任务没执行完,停止按钮是按不了的,给点名函数开个线程试试
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-1 15:10:07 | 显示全部楼层
滚动的话应该涉及到动画了,试试用canvas组件吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-10-1 15:36:28 | 显示全部楼层
qaoapp 发表于 2021-10-1 14:45
前面的任务没执行完,停止按钮是按不了的,给点名函数开个线程试试

完美解决,非常感谢,刚刚在另一个地方有看到另一种办法,在var.set(namerandom)后面加了一行root.update()字幕也能滚动起来
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-1 15:47:15 | 显示全部楼层
wangliyao 发表于 2021-10-1 15:36
完美解决,非常感谢,刚刚在另一个地方有看到另一种办法,在var.set(namerandom)后面加了一行root.update ...

我试了一下,可能root.update()才是正常做法吧,这个模块我不是很熟悉,学习了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-29 01:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表