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

While循环锁定应用程序

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

While循环锁定应用程序

现在,我了解了您想要更好的东西(秒表),我建议使用root.after命令

from Tkinter import *import tkMessageBoximport threadingimport timeroot = Tk()root.geometry("450x250+300+300")root.title("Raspberry PI Test")print dir(root)count = 0def start_counter():    global count    count = 500    root.after(1,update_counter)def update_counter():    global count    count -= 1    if count < 0:        count_complete()    else:        root.after(1,update_counter)def count_complete():    print "DONE COUNTING!! ... I am now back in the main thread"def mymessage():    tkMessageBox.showinfo(title="alert", message="Hello World!")buttonLoop = Button(root, text="Start Loop", command=myloop)buttonLoop.place(x=5, y=15)buttonMessage = Button(root, text="Start Loop", command=mymessage)buttonMessage.place(x=85, y=15)root.mainloop()

(以下为原始答案)

使用线程

from Tkinter import *import tkMessageBoximport threadingimport timeroot = Tk()root.geometry("450x250+300+300")root.title("Raspberry PI Test")print dir(root)def myloop():    def run():        count = 0        while (count < 500) and root.wm_state():print 'The count is:', countcount = count + 1time.sleep(1)        root.after(1,count_complete)    thread = threading.Thread(target=run)    thread.start()def count_complete():    print "DONE COUNTING!! ... I am now back in the main thread"def mymessage():    tkMessageBox.showinfo(title="alert", message="Hello World!")buttonLoop = Button(root, text="Start Loop", command=myloop)buttonLoop.place(x=5, y=15)buttonMessage = Button(root, text="Start Loop", command=mymessage)buttonMessage.place(x=85, y=15)root.mainloop()

请注意,当您显示将在Windows api级别阻塞的信息框时,线程计数将一直等到关闭为止…为了解决该问题,您可以将线程替换为多处理,我认为



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

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

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