dak*_*kov 5 python testing multithreading unit-testing tkinter
我有一个 tkinter 应用程序(作为主线程运行),在其中我打开新的顶级窗口 - 它是一个日志窗口打印测试结果(测试是使用 selenium webdriver 执行的)。该对话框也是所有测试的调用者。
所以我想显示对话框(作为顶层,整个应用程序还有一个窗口),运行测试,等到测试完成并打印结果,然后对另一个测试单元执行相同的操作。但我不想让窗口在测试过程中冻结。
我尝试过使用线程,但显然它可以像那样工作。在这种情况下,在测试完成之前对话框甚至不会启动。
这是对话框窗口的代码。
class TestDialog(tkinter.Toplevel):
def __init__(self, parent, tester, url):
super().__init__(parent)
self.parent = parent
self.webtester = tester;
self.__initComponents()
self.run(url)
self.wait_window(self)
def __initComponents(self):
self.transient(self.parent)
frame = tkinter.Frame(self)
self._tarea = tkinter.Text(frame, state='disabled',wrap='none', width=55, height=25)
vsb = tkinter.Scrollbar(frame, orient=tkinter.VERTICAL, command=self._tarea.yview)
self._tarea.configure(yscrollcommand=vsb.set)
self._tarea.grid(row=1, column=0, columnspan=4, sticky="NSEW", padx=3, pady=3)
vsb.grid(row=1, column=4, sticky='NS',pady=3)
frame.grid(row=0, column=0, sticky=tkinter.NSEW)
frame.columnconfigure(0, weight=2)
frame.rowconfigure(1, weight=1)
window = self.winfo_toplevel()
window.columnconfigure(0, weight=1)
window.rowconfigure(0, weight=1)
self.bind("<Escape>", self.close)
self.protocol("WM_DELETE_WINDOW", self.close)
self.grab_set()
def appendLine(self, msg):
self._tarea['state'] = 'normal'
self._tarea.insert("end", msg+'\n')
self._tarea['state'] = 'disabled'
def run(self, url):
self.appendLine("Runneing test #1...")
try:
thr = threading.Thread(target=self.webtester.urlopen, args=(url,))
thr.start()
except:
pass
thr.join()
self.webtester.urlopen(url)
self.appendLine("Running test #2")
try:
thr = threading.Thread(target=self.webtester.test2)
thr.start()
except:
pass
def close(self, event=None):
self.parent.setBackgroundScheme(DataTreeView.S_DEFAULT)
self.parent.focus_set()
self.destroy()
Run Code Online (Sandbox Code Playgroud)
只需通过以下命令即可从父窗口打开此对话框:
testDialog = TestDialog(self.parent, self._webtester, url)
Run Code Online (Sandbox Code Playgroud)
感谢您的任何建议。
为了防止 GUI 冻结,您需要self.run()快速结束。它需要生成一个线程,启动该线程,然后结束:
import Queue
sentinel = object()
root = tkinter.Tk()
...
def run(self, url):
outqueue = Queue.Queue()
thr = threading.Thread(target=self.run_tests, args=(url, outqueue))
thr.start()
root.after(250, self.update, outqueue)
Run Code Online (Sandbox Code Playgroud)
现在这个线程运行的函数可以运行很长时间:
def run_tests(self, url, outqueue):
outqueue.put("Running test #1...")
self.webtester.urlopen(url)
outqueue.put("Running test #2")
self.webtester.test2()
outqueue.put(sentinel)
Run Code Online (Sandbox Code Playgroud)
但由于 Tkinter 期望所有 GUI 调用都源自单个线程,因此该生成的线程不得进行任何 GUI 调用。为了与 GUI 交互,您可以通过 a 发送输出(例如状态更新消息),Queue.Queue同时让主 Tkinter 线程Queue.Queue定期监视此情况(通过调用root.after):
def update(self, outqueue):
try:
msg = outqueue.get_nowait()
if msg is not sentinel:
self.appendLine(msg)
root.after(250, self.update, outqueue)
else:
# By not calling root.after here, we allow update to
# truly end
pass
except Queue.Empty:
root.after(250, self.update, outqueue)
Run Code Online (Sandbox Code Playgroud)