鱼C论坛

 找回密码
 立即注册
查看: 1257|回复: 3

[已解决]请教tk按钮问题

[复制链接]
发表于 2022-2-18 17:59:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
请问像这样一个代码,点了“函数停止运行”之后,再次点击“主函数”,只执行一次就停了,请问是什么问题呢?

这是原帖,谢谢!!
https://fishc.com.cn/thread-174101-1-1.html

  1. from threading import Thread
  2. import tkinter as tk
  3. from time import sleep  # 这个可以去掉,只是测试时候速度慢点否则程序一下运行完都还来不及点终止
  4. root=tk.Tk()

  5. def mainfunction():
  6.     def func():
  7.         for i in range(10000):
  8.             sleep(0.5)   # 这个可以去掉
  9.             print ("第{0}步已完成".format(i))
  10.             if stop_mainfunction:
  11.                 print('函数停止运行')
  12.                 break
  13.     global t
  14.     t = Thread(target=func)
  15.     t.setDaemon(True)
  16.     t.start()

  17. def stop():
  18.     global stop_mainfunction
  19.     stop_mainfunction = True

  20. button1=tk.Button(root,text='主函数',command=mainfunction)
  21. button1.pack()

  22. stop_mainfunction = False
  23. stop_button = tk.Button(root,text='终止函数运行',command=stop)
  24. stop_button.pack()

  25. root.mainloop()
复制代码
最佳答案
2022-2-18 18:09:08
本帖最后由 isdkz 于 2022-2-18 22:13 编辑

因为你的 stop_mainfunction 还没有赋值回 False,依然是 True

  1. from threading import Thread
  2. import tkinter as tk
  3. from time import sleep  # 这个可以去掉,只是测试时候速度慢点否则程序一下运行完都还来不及点终止
  4. root=tk.Tk()

  5. def mainfunction():
  6.     global stop_mainfunction                                                 # 加上这行
  7.     stop_mainfunction = False                                                # 加上这行
  8.     def func():
  9.         for i in range(10000):
  10.             sleep(0.5)   # 这个可以去掉
  11.             print ("第{0}步已完成".format(i))
  12.             if stop_mainfunction:
  13.                 print('函数停止运行')
  14.                 break
  15.     global t
  16.     t = Thread(target=func)
  17.     t.setDaemon(True)
  18.     t.start()

  19. def stop():
  20.     global stop_mainfunction
  21.     stop_mainfunction = True

  22. button1=tk.Button(root,text='主函数',command=mainfunction)
  23. button1.pack()

  24. # stop_mainfunction = False                                                   去掉这行
  25. stop_button = tk.Button(root,text='终止函数运行',command=stop)
  26. stop_button.pack()

  27. root.mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-18 18:09:08 | 显示全部楼层    本楼为最佳答案   
本帖最后由 isdkz 于 2022-2-18 22:13 编辑

因为你的 stop_mainfunction 还没有赋值回 False,依然是 True

  1. from threading import Thread
  2. import tkinter as tk
  3. from time import sleep  # 这个可以去掉,只是测试时候速度慢点否则程序一下运行完都还来不及点终止
  4. root=tk.Tk()

  5. def mainfunction():
  6.     global stop_mainfunction                                                 # 加上这行
  7.     stop_mainfunction = False                                                # 加上这行
  8.     def func():
  9.         for i in range(10000):
  10.             sleep(0.5)   # 这个可以去掉
  11.             print ("第{0}步已完成".format(i))
  12.             if stop_mainfunction:
  13.                 print('函数停止运行')
  14.                 break
  15.     global t
  16.     t = Thread(target=func)
  17.     t.setDaemon(True)
  18.     t.start()

  19. def stop():
  20.     global stop_mainfunction
  21.     stop_mainfunction = True

  22. button1=tk.Button(root,text='主函数',command=mainfunction)
  23. button1.pack()

  24. # stop_mainfunction = False                                                   去掉这行
  25. stop_button = tk.Button(root,text='终止函数运行',command=stop)
  26. stop_button.pack()

  27. root.mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-18 21:59:52 | 显示全部楼层
isdkz 发表于 2022-2-18 18:09
因为你的 stop_mainfunction 还没有设置回 False,依然是 True

感谢!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-18 22:14:00 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-30 04:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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