鱼C论坛

 找回密码
 立即注册
查看: 2192|回复: 14

[已解决]tkinter界面卡死,求助

[复制链接]
发表于 2020-7-31 16:41:15 | 显示全部楼层 |阅读模式

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

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

x
代码:
  1. # -*- coding: utf-8 -*-
  2. from requests import get
  3. from re import search
  4. import time as ti
  5. import tkinter as tk
  6. import tkinter.messagebox
  7. from threading import Thread
  8. # 注:\1 用于引用前面编号为 1 的子组
  9. a = []
  10. root = tk.Tk()
  11. root.title("求助帖提醒")
  12. t = tk.Text(root)
  13. t.pack()
  14. def load():
  15.     while True:
  16.         res = get(f"https://fishc.com.cn/bestanswer.php?mod=huzhu&type=undo").text
  17.         # 获取问题帖的名字
  18.         name = search(r'<a href="https://fishc.com.cn/thread-\d+?-1-1.html" target="_blank">(.+?)</a>', res).group(1)
  19.         #获取问题帖的URL
  20.         url = "https://fishc.com.cn/thread-"+search(r'<a href="https://fishc.com.cn/thread-(.+?)-1-1.html" target="_blank"',res).group(1)+"-1-1.html"
  21.         # 获取回答数
  22.         ans = search(r'<font color="#999999">(\d+?)</font>', res).group(1)
  23.         # 获取时间
  24.         time = search(r'<font color="#999999">(\d+?-\d+?-\d+? \d+?:\d+?)</font>', res).group(1)
  25.         if name not in a:
  26.             b = f" 标题:{name}\n 回答数:{ans}\n 提问时间:{time}\n 地址:{url}\n\n"
  27.             t.insert(tk.END,b)    # 打印相应的内容
  28.             tkinter.messagebox.showwarning("提示",b)
  29.             a.append(name)
  30.         ti.sleep(5)

  31. func = Thread(target=load)
  32. func.setDaemon(True)
  33. func.run()
  34. root.mainloop()
复制代码


开了线程了,但是毫无作用
最佳答案
2020-7-31 17:16:25
本帖最后由 nahongyan1997 于 2020-7-31 17:18 编辑

func.run() 改成 func.start()  问题已解决 !!!!!




顺便说一句,你的代码归我了

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-7-31 16:46:07 | 显示全部楼层
本帖最后由 zltzlt 于 2020-7-31 16:53 编辑


  1. # -*- coding: utf-8 -*-
  2. from requests import get
  3. from re import search
  4. import tkinter as tk
  5. import tkinter.messagebox
  6. from threading import Thread

  7. # 注:\1 用于引用前面编号为 1 的子组
  8. a = []
  9. root = tk.Tk()
  10. root.title("求助帖提醒")
  11. t = tk.Text(root)
  12. t.pack()


  13. def load():
  14.     res = get(f"https://fishc.com.cn/bestanswer.php?mod=huzhu&type=undo").text
  15.     # 获取问题帖的名字
  16.     name = search(r'<a href="https://fishc.com.cn/thread-\d+?-1-1.html" target="_blank">(.+?)</a>', res).group(1)
  17.     # 获取问题帖的URL
  18.     url = "https://fishc.com.cn/thread-" + search(
  19.         r'<a href="https://fishc.com.cn/thread-(.+?)-1-1.html" target="_blank"', res).group(1) + "-1-1.html"
  20.     # 获取回答数
  21.     ans = search(r'<font color="#999999">(\d+?)</font>', res).group(1)
  22.     # 获取时间
  23.     time = search(r'<font color="#999999">(\d+?-\d+?-\d+? \d+?:\d+?)</font>', res).group(1)
  24.     if name not in a:
  25.         b = f" 标题:{name}\n 回答数:{ans}\n 提问时间:{time}\n 地址:{url}\n\n"
  26.         t.insert(tk.END, b)  # 打印相应的内容
  27.         tkinter.messagebox.showwarning("提示", b)
  28.         a.append(name)


  29. func = Thread(target=load)
  30. func.setDaemon(True)
  31. func.run()
  32. root.mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-31 16:48:20 | 显示全部楼层
zltzlt 发表于 2020-7-31 16:46
你都 while True 了,不卡死才怪


可我想它等5秒就抓取一次呀
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-31 16:50:19 | 显示全部楼层
本帖最后由 zltzlt 于 2020-7-31 17:19 编辑
xiaosi4081 发表于 2020-7-31 16:48
可我想它等5秒就抓取一次呀


那就把 if name not in a 去掉
  1. # -*- coding: utf-8 -*-
  2. from requests import get
  3. from re import search
  4. import time as ti
  5. import tkinter as tk
  6. import tkinter.messagebox
  7. from threading import Thread

  8. # 注:\1 用于引用前面编号为 1 的子组
  9. a = []
  10. root = tk.Tk()
  11. root.title("求助帖提醒")
  12. t = tk.Text(root)
  13. t.pack()


  14. def load():
  15.     while True:
  16.         res = get(f"https://fishc.com.cn/bestanswer.php?mod=huzhu&type=undo").text
  17.         # 获取问题帖的名字
  18.         name = search(r'<a href="https://fishc.com.cn/thread-\d+?-1-1.html" target="_blank">(.+?)</a>', res).group(1)
  19.         # 获取问题帖的URL
  20.         url = "https://fishc.com.cn/thread-" + search(
  21.             r'<a href="https://fishc.com.cn/thread-(.+?)-1-1.html" target="_blank"', res).group(1) + "-1-1.html"
  22.         # 获取回答数
  23.         ans = search(r'<font color="#999999">(\d+?)</font>', res).group(1)
  24.         # 获取时间
  25.         time = search(r'<font color="#999999">(\d+?-\d+?-\d+? \d+?:\d+?)</font>', res).group(1)
  26.         b = f" 标题:{name}\n 回答数:{ans}\n 提问时间:{time}\n 地址:{url}\n\n"
  27.         t.insert(tk.END, b)  # 打印相应的内容
  28.         tkinter.messagebox.showwarning("提示", b)
  29.         a.append(name)
  30.         ti.sleep(5)


  31. func = Thread(target=load)
  32. func.setDaemon(True)
  33. func.run()
  34. root.mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-31 16:51:34 | 显示全部楼层
zltzlt 发表于 2020-7-31 16:46
你都 while True 了,不卡死才怪

不是这个上的问题,根本问题是线程里的函数不对,开启另一个线程是为了让爬取的同时不影响,tkinter显示,而他在另一个线程里有关于tkinter的操作,应该把关于tkinter的相关操作放在load函数外面

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
zltzlt + 3 + 3

查看全部评分

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

使用道具 举报

 楼主| 发表于 2020-7-31 17:00:54 | 显示全部楼层
v.ki 发表于 2020-7-31 16:51
不是这个上的问题,根本问题是线程里的函数不对,开启另一个线程是为了让爬取的同时不影响,tkinter显示 ...

那怎么改?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-31 17:16:25 | 显示全部楼层    本楼为最佳答案   
本帖最后由 nahongyan1997 于 2020-7-31 17:18 编辑

func.run() 改成 func.start()  问题已解决 !!!!!




顺便说一句,你的代码归我了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-31 17:19:09 | 显示全部楼层

把 if name not in a 去掉
  1. # -*- coding: utf-8 -*-
  2. from requests import get
  3. from re import search
  4. import time as ti
  5. import tkinter as tk
  6. import tkinter.messagebox
  7. from threading import Thread

  8. # 注:\1 用于引用前面编号为 1 的子组
  9. a = []
  10. root = tk.Tk()
  11. root.title("求助帖提醒")
  12. t = tk.Text(root)
  13. t.pack()


  14. def load():
  15.     while True:
  16.         res = get(f"https://fishc.com.cn/bestanswer.php?mod=huzhu&type=undo").text
  17.         # 获取问题帖的名字
  18.         name = search(r'<a href="https://fishc.com.cn/thread-\d+?-1-1.html" target="_blank">(.+?)</a>', res).group(1)
  19.         # 获取问题帖的URL
  20.         url = "https://fishc.com.cn/thread-" + search(
  21.             r'<a href="https://fishc.com.cn/thread-(.+?)-1-1.html" target="_blank"', res).group(1) + "-1-1.html"
  22.         # 获取回答数
  23.         ans = search(r'<font color="#999999">(\d+?)</font>', res).group(1)
  24.         # 获取时间
  25.         time = search(r'<font color="#999999">(\d+?-\d+?-\d+? \d+?:\d+?)</font>', res).group(1)
  26.         b = f" 标题:{name}\n 回答数:{ans}\n 提问时间:{time}\n 地址:{url}\n\n"
  27.         t.insert(tk.END, b)  # 打印相应的内容
  28.         tkinter.messagebox.showwarning("提示", b)
  29.         a.append(name)
  30.         ti.sleep(5)


  31. func = Thread(target=load)
  32. func.setDaemon(True)
  33. func.run()
  34. root.mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-31 17:19:27 | 显示全部楼层

顺便说下,我给你发的站内消息你还没回复我
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-31 17:19:28 | 显示全部楼层
nahongyan1997 发表于 2020-7-31 17:16
func.run() 改成 func.start()  问题已解决 !!!!!

无所谓,反正我也是要公开的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-31 17:23:55 | 显示全部楼层
zltzlt 发表于 2020-7-31 17:19
顺便说下,我给你发的站内消息你还没回复我

我的答案是对的,问题没有那么复杂
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-31 17:24:27 | 显示全部楼层
nahongyan1997 发表于 2020-7-31 17:23
我的答案是对的,问题没有那么复杂

为什么我的帖子还没过审核,都好几天了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-31 17:26:16 | 显示全部楼层
xiaosi4081 发表于 2020-7-31 17:19
无所谓,反正我也是要公开的

满意设置最佳答案呦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-31 17:27:02 | 显示全部楼层
xiaosi4081 发表于 2020-7-31 17:19
无所谓,反正我也是要公开的

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

使用道具 举报

发表于 2020-7-31 19:28:49 | 显示全部楼层
我第一次专辑求助帖……主要是看上你的代码了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-24 12:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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