【问题标题】:Threading in TkinterTkinter 中的线程
【发布时间】:2015-05-22 16:29:11
【问题描述】:

我在 Tkinter 中遇到线程问题。我读到应该在除主 GUI 线程之外的单独线程中完成事情。每当我单击按钮时,它会在线程停止后关闭。我认为我没有正确实现线程。

from Tkinter import *
from scrapy import cmdline
import thread

def scrapeMe():
    if(RadioVar.get() == 1):
        thread.start_new_thread(cmdline.execute("scrapy crawl s1".split()))
    if(RadioVar.get() == 2):
        thread.start_new_thread(cmdline.execute("scrapy crawl s2".split()))


root = Tk()

B1 = Button(root, text="Scrape", command = scrapeMe)

RadioVar = IntVar()

R1 = Radiobutton(root, text = "1", variable = RadioVar, value=1)
R2 = Radiobutton(root, text = "2", variable = RadioVar, value=2)


B1.pack()
R1.pack()
R2.pack()
root.mainloop()

【问题讨论】:

  • 您的问题是什么?你什么都没问。
  • 程序在应该继续运行时单击按钮后关闭。抱歉,我认为可以推断应该添加。
  • 好的,程序关闭。问题是什么?你想让它关闭吗?它是否因错误而关闭?
  • 没有错误,我希望线程执行并让 GUI 在执行时继续运行。因此,您单击按钮,线程开始,GUI 继续运行,线程结束,GUI 仍在运行。 -谢谢

标签: python multithreading tkinter


【解决方案1】:

我解决了大部分问题。第一个是,您以错误的方式使用了“线程”。我不确定如何使用scrapy,所以我会把它留给你或其他人。至少,我让它为你运行。

from tkinter import *
from scrapy import cmdline
import threading

def scrapeMe():
    if(RadioVar.get() == 1):
        threading.Thread(cmdline.execute("scrapy crawl s1".split()))
    if(RadioVar.get() == 2):
        threading.Thread(cmdline.execute("scrapy crawl s2".split()))


root = Tk()

B1 = Button(root, text="Scrape", command = scrapeMe)

RadioVar = IntVar()

R1 = Radiobutton(root, text = "1", variable = RadioVar, value=1)
R2 = Radiobutton(root, text = "2", variable = RadioVar, value=2)


B1.pack()
R1.pack()
R2.pack()
root.mainloop()

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-29
  • 2014-07-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多