xiaosi4081 发表于 2020-7-31 16:41:15

tkinter界面卡死,求助

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

func = Thread(target=load)
func.setDaemon(True)
func.run()
root.mainloop()


开了线程了,但是毫无作用

zltzlt 发表于 2020-7-31 16:46:07

本帖最后由 zltzlt 于 2020-7-31 16:53 编辑



# -*- coding: utf-8 -*-
from requests import get
from re import search
import tkinter as tk
import tkinter.messagebox
from threading import Thread

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


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


func = Thread(target=load)
func.setDaemon(True)
func.run()
root.mainloop()

xiaosi4081 发表于 2020-7-31 16:48:20

zltzlt 发表于 2020-7-31 16:46
你都 while True 了,不卡死才怪

可我想它等5秒就抓取一次呀

zltzlt 发表于 2020-7-31 16:50:19

本帖最后由 zltzlt 于 2020-7-31 17:19 编辑

xiaosi4081 发表于 2020-7-31 16:48
可我想它等5秒就抓取一次呀

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

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


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


func = Thread(target=load)
func.setDaemon(True)
func.run()
root.mainloop()

v.ki 发表于 2020-7-31 16:51:34

zltzlt 发表于 2020-7-31 16:46
你都 while True 了,不卡死才怪

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

xiaosi4081 发表于 2020-7-31 17:00:54

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

那怎么改?

nahongyan1997 发表于 2020-7-31 17:16:25

本帖最后由 nahongyan1997 于 2020-7-31 17:18 编辑

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




顺便说一句,你的代码归我了{:5_109:}

zltzlt 发表于 2020-7-31 17:19:09

xiaosi4081 发表于 2020-7-31 17:00
那怎么改?

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

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


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


func = Thread(target=load)
func.setDaemon(True)
func.run()
root.mainloop()

zltzlt 发表于 2020-7-31 17:19:27

xiaosi4081 发表于 2020-7-31 17:00
那怎么改?

顺便说下,我给你发的站内消息你还没回复我

xiaosi4081 发表于 2020-7-31 17:19:28

nahongyan1997 发表于 2020-7-31 17:16
func.run() 改成 func.start()问题已解决 !!!!!




无所谓,反正我也是要公开的

nahongyan1997 发表于 2020-7-31 17:23:55

zltzlt 发表于 2020-7-31 17:19
顺便说下,我给你发的站内消息你还没回复我

我的答案是对的,问题没有那么复杂

nahongyan1997 发表于 2020-7-31 17:24:27

nahongyan1997 发表于 2020-7-31 17:23
我的答案是对的,问题没有那么复杂

为什么我的帖子还没过审核,都好几天了

nahongyan1997 发表于 2020-7-31 17:26:16

xiaosi4081 发表于 2020-7-31 17:19
无所谓,反正我也是要公开的

满意设置最佳答案呦

zltzlt 发表于 2020-7-31 17:27:02

xiaosi4081 发表于 2020-7-31 17:19
无所谓,反正我也是要公开的

weiter 发表于 2020-7-31 19:28:49

我第一次专辑求助帖……主要是看上你的代码了{:10_250:}
页: [1]
查看完整版本: tkinter界面卡死,求助