Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何测试带有Tkinter mainloop的Python代码?_Python_Testing_Tkinter - Fatal编程技术网

如何测试带有Tkinter mainloop的Python代码?

如何测试带有Tkinter mainloop的Python代码?,python,testing,tkinter,Python,Testing,Tkinter,如何使用nosetests测试以下代码 # GUI try: # for Python2 import Tkinter as tk except ImportError: # for Python3 import tkinter as tk def main(): root = tk.Tk() canvas = tk.Canvas(root, width=250, height=250) canvas.pack() root.b

如何使用nosetests测试以下代码

# GUI
try:
    # for Python2
    import Tkinter as tk
except ImportError:
    # for Python3
    import tkinter as tk


def main():
    root = tk.Tk()
    canvas = tk.Canvas(root, width=250, height=250)
    canvas.pack()
    root.bind('<B1-Motion>', lambda x: True)
    root.bind('<Button-1>', lambda x: True)
    root.mainloop()
#GUI
尝试:
#用于蟒蛇2
将Tkinter作为tk导入
除恐怖外:
#用于蟒蛇3
将tkinter作为tk导入
def main():
root=tk.tk()
canvas=tk.canvas(根,宽度=250,高度=250)
canvas.pack()
root.bind(“”,lambda x:True)
root.bind(“”,lambda x:True)
root.mainloop()
当我调用
main()
时,将执行main循环。如何防止这种情况发生?

要停止mainloop()运行,您可以注释掉该行,这将运行main(),而无需Tkinter创建画布

try:
    # for Python2
     import Tkinter as tk
except ImportError:
     # for Python3
    import tkinter as tk


def main():
    root = tk.Tk()
    canvas = tk.Canvas(root, width=250, height=250)
    canvas.pack()
    root.bind('<B1-Motion>', lambda x: True)
    root.bind('<Button-1>', lambda x: True)
    #root.mainloop() """#stops the line being run"""
main()
试试看:
#用于蟒蛇2
将Tkinter作为tk导入
除恐怖外:
#用于蟒蛇3
将tkinter作为tk导入
def main():
root=tk.tk()
canvas=tk.canvas(根,宽度=250,高度=250)
canvas.pack()
root.bind(“”,lambda x:True)
root.bind(“”,lambda x:True)
#root.mainloop()“”“#停止正在运行的线路”“”
main()
或者将root.mainloop()置于main()函数之外,并注释掉mainloop()

试试看:
#用于蟒蛇2
将Tkinter作为tk导入
除恐怖外:
#用于蟒蛇3
将tkinter作为tk导入
def main():
root=tk.tk()
canvas=tk.canvas(根,宽度=250,高度=250)
canvas.pack()
root.bind(“”,lambda x:True)
root.bind(“”,lambda x:True)
main()
#root.mainloop()

不调用root.mainloop(),而是调用root.update()。这将只运行tk循环一次(因此,应用程序将调度和处理任何挂起的事件)

测试应用程序行为将成为一系列步骤,如:

root.event_generate('<B1-Motion>') # will generate a "fake" event <B1-Motion>
root.update()
# test that '<B1-Motion>' callback was correctly executed (by eg checking an expected state of you app
root.event_generate('<Button-1>')
root.update()
# test ....
root.event_generate(“”)将生成一个“假”事件
root.update()
#测试“”回调是否正确执行(例如检查应用程序的预期状态)
root.event_generate(“”)
root.update()
#测试。。。。
请注意,您还可以通过调用
button.invoke()

您想要测试的代码最好在它自己的模块或与gui代码分离的函数/类中进行测试。这样,它可以独立于gui进行测试。gui代码会在需要时调用单独的代码。
root.event_generate('<B1-Motion>') # will generate a "fake" event <B1-Motion>
root.update()
# test that '<B1-Motion>' callback was correctly executed (by eg checking an expected state of you app
root.event_generate('<Button-1>')
root.update()
# test ....