Ejemplo de hilo simple de Tkinter

Ejemplo de hilo simple de Tkinter (tenga en cuenta el uso de global)

from tkinter import *  
import threading  
import time  
 
global coninueRun
coninueRun=FALSE

def count(i):  
    global coninueRun
    lista.delete(0,"end")
    for k in range(1, 1000+1):  
        lista.insert(END,'第'+str(i)+'线程count:  '+str(k)+'\n')  
        time.sleep(0.1)  
        var.set(str(k)) 
        if(coninueRun==FALSE):
            break;

    var.set('thread  is over') 

  
def fun():
    global coninueRun
    coninueRun=TRUE 
    var.set('begin')  
    th=threading.Thread(target=count,args=(1,))  
    th.setDaemon(True)#守护线程  
    th.start() 
     

  
def fun2(): 
    global coninueRun 
    coninueRun=FALSE
    var.set('thread  is over')  
  
  
root=Tk()  
root.title('GUI线程测试')  #窗口标题  
root.geometry('800x600')#窗口呈现位置  


button2=Button(root,text='停止线程',font=('微软雅黑',10),command=fun2)  
button2.grid(row=1,column=2)   

button=Button(root,text='启动线程',font=('微软雅黑',10),command=fun)  
button.grid(row=1,column=3) 

var=StringVar()#设置变量  
lista=Listbox(root,width=20)
lista.grid(row=2,column=2)

label=Label(root,font=('微软雅黑',10),fg='red',textvariable=var)  
label.grid(row=3,column=2)  
var.set('信息提示')  

root.mainloop()  

 

Supongo que te gusta

Origin blog.csdn.net/sichuanpb/article/details/114444920
Recomendado
Clasificación