python打开其他应用程序锁定_Python – 循环锁应用程序

这个博客展示了如何使用Tkinter库在Python中创建一个倒计时计数器。通过root.after方法,计数器从500递减到0,并在完成时返回主线程。还提到了在显示阻塞消息框时可能遇到的问题,以及可以考虑使用多处理代替线程来解决。
摘要由CSDN通过智能技术生成

既然我明白你想要什么(秒表)我会推荐root.after命令

from Tkinter import *

import tkMessageBox

import threading

import time

root = Tk()

root.geometry("450x250+300+300")

root.title("Raspberry PI Test")

print dir(root)

count = 0

def 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 tkMessageBox

import threading

import time

root = 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:', count

count = count + 1

time.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级别阻止的信息框时,线程计数将等到关闭…为了解决这个问题,您可以用多处理替换线程我认为

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值