首页 > 其他分享 >tkinter中使用thread

tkinter中使用thread

时间:2023-03-21 11:24:28浏览次数:35  
标签:tkinter thread self time value tk 使用

import tkinter as tk
from threading import Thread
import datetime
import time

class SampleApp(tk.Tk):
	def __init__(self):
		tk.Tk.__init__(self)

		# Make a StringVar that will contain the value
		self.value = tk.StringVar()

		# Make a label that will show the value of self.value
		self.label = tk.Label(self, textvariable=self.value).pack()


		# Make a thread that will run outside the tkinter program
		self.thread = Thread(target=self.show_time)

		# set daemon to True (This means that the thread will stop when you stop the tkinter program)
		self.thread.daemon = True

		# start the thread
		self.thread.start()

		tk.Button(self, text="Click me", command=lambda: print("Hello World")).pack()

	def show_time(self):
		# The thread will execute this function in the background, so you need to while loop to update the value of self.value
		while True:
			# Get the time (in your case, you need to get the api data)
			data = datetime.datetime.now().strftime('Time: %H:%M:%S, Milliseconds: %f')

			# Update the StringVar variable
			self.value.set(data)

			# Pause the while loop with 1 second, so you can set an interval to update your value
			time.sleep(1)

# rest of your code.
root = SampleApp()
root.mainloop()

  

标签:tkinter,thread,self,time,value,tk,使用
From: https://www.cnblogs.com/hushaojun/p/17239315.html

相关文章

  • 为什么Redis不直接使用C语言的字符串?看完直接吊打面试官!
    众所周知Redis有以下几种常见的数据类型String(字符串)、List(列表)、Set(集合)、Hash(哈希)、Sortedset(有序集合)、Stream(流)、Geo(地理空间索引)、Bitmap(位图)、HyperLogLog(基数统计......
  • 如何使用ChatGPT玩游戏
    如果您正在寻找一种有趣且引人入胜的方式来消磨时间,那么绝对应该查看ChatGPT。您只需使用这个OpenAI平台与机器人聊天,就可以玩很多游戏。我们将介绍您可以使用的十大......
  • OpenStack使用ISO镜像安装虚拟机制作镜像模板(本文底稿原创,由ChatGPT润色)
    在OpenStack云平台中,使用ISO镜像安装虚拟机是非常常见的一种方式。本文将介绍如何在OpenStack中使用ISO镜像创建一个虚拟机,并将其制作成模板。第一步,我们需要将ISO镜像上......
  • Java之JasyptUtil类的使用
    在配置文件中,我们通常会对中间件密码进行加密。手动加密可以使用JasyptUtil类,代码如下:packagecom.cmit.kapok.system.utils;importorg.jasypt.encryption.pbe.Standa......
  • 使用Jcom组件操作Visio批量导出图片
    [url]http://mncc.iteye.com/blog/367389[/url]在JAVA中使用JCOM和JXL注意要点:(1)在你的lib下要有jdom-1.0.jar,jxl-2.5.5.jar,jcom-2.2.4.jar,jcom.dl......
  • 【Unity3D】使用GL绘制线段
    1前言​线段渲染器LineRenderer、拖尾TrailRenderer、绘制物体表面三角形网格从不同角度介绍了绘制线段的方法,本文再介绍一种新的绘制线段的方法:使用GL绘制线段。......
  • openai-translator的安装与使用
    Releases·yetone/openai-translator(github.com)介绍里有,这里只给出windows的安装方法 以及浏览器插件安装  ......
  • 鼠标右键添加使用sublime text打开
    WindowsRegistryEditorVersion5.00[HKEY_CLASSES_ROOT\*\shell\SublimeText]@="sublimetextopen""Icon"="C:\\ProgramFiles\\SublimeText\\sublime_text.exe"[H......
  • ASP.NET MVC Filters 4种默认过滤器的使用
    过滤器(Filters)的出现使得我们可以在ASP.NETMVC程序里更好的控制浏览器请求过来的URL,不是每个请求都会响应内容,只响应特定内容给那些有特定权限的用户,过滤器理论上有以下功......
  • Spring Cloud Alibaba系列(一)限流与防护组件Sentinel的简单使用
    Sentinel是SpringCloudAlibaba体系的安全防护组件,我们可以使用它以“非业务侵入”方式实现限流,熔断,服务降级需求。一.下载并启动Sentinel控制台从GitHub网址https......