栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

tkinter循环和串行写入

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

tkinter循环和串行写入

语境

您使用了Tkinter

mainloop
while
-loop,现在您想将它们放到一个程序中。

while X:    do_y()

master.mainloop()

解决方案

有几种适合您的解决方案。

  1. 分割循环,并使用它

    after
    来让GUI回叫您:

    def do_y2():do_y()if X:    master.after(123, do_y2) # after 123 milli seconds this is called again

    do_y2()
    master.mainloop()

  2. 由我使用guiLoop。

    from guiLoop import guiLoop # https://gist.github.com/niccokunzmann/8673951#file-guiloop-py

    @guiLoop
    def do_y2():
    while X:
    do_y()
    yield 0.123 # give the GUI 123 milli seconds to do everything
    do_y2(master)
    master.mainloop()

guiLoop使用从1开始的方法。但是允许您使用一个或多个while循环。

  1. 用于
    update
    刷新GUI。
    while X:do_y()master.update()

这种方法是一种不寻常的方法,因为它取代了大多数GUI框架(例如Tkinter)中的mainloop。请注意,使用1和2可以有多个循环,而不仅仅是3中的一个。

  1. 使用新的执行线程来并行执行循环。!该线程不得直接访问master或任何GUI元素,因为Tkinter可能会崩溃!

    import threading

    def do_y_loop():
    while X:
    do_y()
    thread = threading.Thread(target = do_y_loop)
    thread.deamon = True # use this if your application does not close.
    thread.start()
    master.mainloop()

  2. 在新线程中启动mainloop。与4中一样,如果您从线程访问GUI,Tkinter可能会崩溃。

    import threading

    thread = threading.Thread(target = master.mainloop)
    thread.deamon = True # use this if your application does not close.
    thread.start()
    while X:
    do_y()

在4.和5.中,GUI和while循环之间的通信可以/应该通过全局变量进行,但绝不能通过tkinter方法进行。




转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/633601.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号