Python 去掉线里面的标签?
所以我有这个代码:Python 去掉线里面的标签?,python,tkinter,Python,Tkinter,所以我有这个代码: import thread from Tkinter import * import random import time Admin=Tk() def moveit(number): songas=Label(Admin,text=number,bg='red') def ji(): plad=0.0 recount=0 times=0 while 1: plad-=0.
import thread
from Tkinter import *
import random
import time
Admin=Tk()
def moveit(number):
songas=Label(Admin,text=number,bg='red')
def ji():
plad=0.0
recount=0
times=0
while 1:
plad-=0.1
recount+=1
times+=1
time.sleep(0.5)
pls=0.0
pls+=plad
if recount==4:
pls=0
plad=0.0
recount=0
songas.place(relx=pls,rely=0.7)
thread.start_new_thread(ji,())
za=random.random()
button=Button(Admin,text='Press',command=lambda:moveit(str(za)))
button.place(relx=0.2)
Admin.mainloop()
它开始向左移动,但如果你再次按下“按下”按钮,它会在旧的数字上再加上一些数字。
是否有人知道如何删除旧的数字以使其成为已知的数字?您必须以某种方式向旧线程发出退出信号。使用锁可能是最容易的——在创建新线程时创建锁并获取它。当不再需要线程时释放它。然后线程只需要在主循环中检查它的锁是否仍然被锁定——如果不是,它将移除标签并退出。此处是代码的修改版本(用合适的代码替换“删除此处的标签”注释):
Tkinter不是线程安全的——除了主线程外,您不能在任何线程中操作小部件,否则您将得到未定义的结果 这不需要线程。您的代码添加了一个无限循环,但应用程序已经有了一个可以利用的无限循环(事件循环) 如果要移动某些项,请创建一个函数,该函数可执行两项操作。首先,它可以执行您想要的任何操作,例如移动项目。其次,它使用标准的
after
方法在短时间内(例如,半秒或500毫秒)再次调用自己。通过这种方式,您可以让事件循环驱动动画,不需要线程,并且您的UI保持响应
这里有一个例子。我怀疑它能完全满足你的需求,因为我不确定你到底想要什么
import Tkinter as tk
import random
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
self._running = False
self._relx = None
tk.Tk.__init__(self, *args, **kwargs)
self.pack_propagate(False)
self.configure(width=400, height=400)
self.label = tk.Label(self, text="hello, world", background="red")
self.button = tk.Button(self, text="Start", command=self.toggle)
self.button.pack(side="top")
def toggle(self):
'''toggle animation on or off'''
self._running = not self._running
if self._running:
self.button.configure(text="Stop")
self.moveit()
else:
self.button.configure(text="Start")
def moveit(self):
'''Animate the label'''
if not self._running:
# animation has been stopped
# hide the label from view.
self.label.place_forget()
if self._running:
if not self.label.winfo_viewable():
# not visible; establish future locations
self._relx = [.5, .4, .3, .2, .1, 0]
relx = self._relx.pop(0)
self._relx.append(relx)
self.label.place(relx=relx, rely=0.7)
self.after(1000, self.moveit)
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
import Tkinter as tk
import random
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
self._running = False
self._relx = None
tk.Tk.__init__(self, *args, **kwargs)
self.pack_propagate(False)
self.configure(width=400, height=400)
self.label = tk.Label(self, text="hello, world", background="red")
self.button = tk.Button(self, text="Start", command=self.toggle)
self.button.pack(side="top")
def toggle(self):
'''toggle animation on or off'''
self._running = not self._running
if self._running:
self.button.configure(text="Stop")
self.moveit()
else:
self.button.configure(text="Start")
def moveit(self):
'''Animate the label'''
if not self._running:
# animation has been stopped
# hide the label from view.
self.label.place_forget()
if self._running:
if not self.label.winfo_viewable():
# not visible; establish future locations
self._relx = [.5, .4, .3, .2, .1, 0]
relx = self._relx.pop(0)
self._relx.append(relx)
self.label.place(relx=relx, rely=0.7)
self.after(1000, self.moveit)
if __name__ == "__main__":
app = SampleApp()
app.mainloop()