Frame动画,执行5秒后停止,线程控制

1.首先为一个按钮设置监听器

2.按钮触发的代码如下:

private class ButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
imageView.setBackgroundResource(R.drawable.myframeanimation);
final AnimationDrawable animationDrawable = (AnimationDrawable) imageView
.getBackground();
animationDrawable.start();

                       //执行线程5秒后停止。

new Thread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(5000);
animationDrawable.stop();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个例子,演示如何在Tkinter中使用线程,在点击启动按钮后启动线程,在点击停止按钮后停止线程: ``` import tkinter as tk import threading import time class App(tk.Frame): def __init__(self, master=None): super().__init__(master) self.pack() self.create_widgets() def create_widgets(self): self.counter_label = tk.Label(self, text="0") self.counter_label.pack() self.start_button = tk.Button(self, text="Start", command=self.start_counter) self.start_button.pack() self.stop_button = tk.Button(self, text="Stop", command=self.stop_counter, state=tk.DISABLED) self.stop_button.pack() def start_counter(self): # 创建线程 self.counter_thread = threading.Thread(target=self.update_counter) # 启动线程 self.counter_thread.start() # 更新按钮状态 self.start_button.config(state=tk.DISABLED) self.stop_button.config(state=tk.NORMAL) def stop_counter(self): # 停止线程 self.counter_running = False # 更新按钮状态 self.start_button.config(state=tk.NORMAL) self.stop_button.config(state=tk.DISABLED) def update_counter(self): self.counter_running = True counter = 0 while self.counter_running: counter += 1 time.sleep(0.1) # 模拟耗时操作 # 在GUI线程中更新计数器的值 self.master.after(0, self.counter_label.config, {"text": str(counter)}) if __name__ == '__main__': root = tk.Tk() app = App(master=root) app.mainloop() ``` 在这个例子中,点击“Start”按钮后,程序会创建一个新线程,该线程会不断地更新计数器的值,并且每次更新后会通过`self.master.after()`方法在GUI线程中更新计数器的标签。同时,“Start”按钮的状态会被禁用,“Stop”按钮的状态会被启用。当点击“Stop”按钮时,程序会将`self.counter_running`标记为False,从而停止线程执行,同时更新按钮状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值