【发布时间】:2014-07-29 08:05:57
【问题描述】:
这是我的问题:使用 Tkinter,我想单击一个按钮并启动一个 python 脚本。 这个 python 脚本现在是一个模块(我不知道它是否是最好的方法)导入到我的主脚本中。 该脚本应该在后台运行。它有一种方法可以退出它。我怎么称呼它? 我想向模块传递一个标志或其他东西,但我不知道该怎么做。
现在,我这样称呼我的代码:在 gui.py 中
import sniffer_wideband_v09
from Tkinter import *
root = Tk()
def handle_click():
global t
global stop_flag
stop_flag = 0
def callback():
sniffer_wideband_v09.main(sampling_rates, center_frequencies, gains, file_dir, files_names)
t = Thread(target=callback)
t.start()
root.mainloop()
我想调用 sniffer_wideband_v09.py 中的 quitting() 方法。或者将标志传递给我的模块以停止无限循环。 之后,我需要将所有这些绑定到 Tkinter Buttons。
我对这个问题做了一些研究,发现:
Is there any way to kill a Thread in Python? 随着 How to run a function in the background of tkinter 和 How to run and stop an infinite loop in a python thread
第一个很有希望,但我不完全理解,我正在努力。
注意:我使用 ./gui.py 直接从我的 shell 中运行它,并且我在 Ubuntu 下,而不是 windows。(我认为它可以改变一些处理多线程的方式)。
感谢您的阅读,任何提示或帮助将不胜感激。如果我同时找到回应,我会发布我的代码。
编辑:提供有关线程中启动的脚本的更多信息:(它是一个 GNURadio 脚本)
class foo():
def __init__(self):
self.parameters()
def methods():
self.dostuff()
def main(sampling_rates, center_frequencies, gains, file_dir, files_names):
tb = foo()
while True: #flag could be here to exit the infinite while.
tb.start()
tb.stop()
def quitting():
tb.stop()
## not mandatory piece of code from now on
if __name = "__main__":
main()
quitting()
【问题讨论】:
标签: python multithreading python-2.7 tkinter