Tkinter循环“Tkinter没有响应”
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tkinter循环“Tkinter没有响应”相关的知识,希望对你有一定的参考价值。
我有一个来自与arduino通信的tkinter图形界面的代码,碰巧我得到时间打开和关闭的时间,所以我将每个当前时间与无限循环中的输入进行比较,该循环仅用于第二次在这里输入代码,要求关闭LED结束,它发生在进入循环时tkinter没有响应。
from tkinter import *
import serial
from time import strftime
conexao = serial.Serial('COM3', 9600, timeout=0.5)
cont=0
janela = Tk()
def ligar():
global cont
cont=cont+1
if cont==1:
valor=bytes(('1'),'utf-8')
conexao.write(valor)
ligar['text']='Desligar led'
elif cont==2:
valor=bytes(('2'),'utf-8')
conexao.write(valor)
ligar['text']='Ligar led'
cont=0
def agendar():
comeco= start.get()
final= limit.get()
while 1>0:
if comeco == strftime('%H:%M:%S'):
valor=bytes(('1'),'utf-8')
conexao.write(valor)
elif final == strftime('%H:%M:%S'):
valor=bytes(('2'),'utf-8')
conexao.write(valor)
break
ligar=Button(janela,command=ligar,text='ligar',width="60")
ligar.grid(row=1)
texto=Label(text='Defina o começo:').grid(row=2)
start=Entry(janela)
start.grid(row=3)
texto2=Label(text='Defina o fim:').grid(row=4)
limit=Entry(janela)
limit.grid(row=5)
salvar=Button(janela,width="30",text='Salvar Horarios',command=agendar)
salvar.grid(row=6)
答案
您可以使用线程来代替使用after
方法,这允许您的GUI与while循环同时运行。
import threading
from tkinter import *
import serial
from time import strftime
conexao = serial.Serial('COM3', 9600, timeout=0.5)
cont=0
janela = Tk()
def ligar():
global cont
cont=cont+1
if cont==1:
valor=bytes(('1'),'utf-8')
conexao.write(valor)
ligar['text']='Desligar led'
elif cont==2:
valor=bytes(('2'),'utf-8')
conexao.write(valor)
ligar['text']='Ligar led'
cont=0
def agendar():
global start,limit
comeco= start.get()
final= limit.get()
while 1>0:
if comeco == strftime('%H:%M:%S'):
valor=bytes(('1'),'utf-8')
conexao.write(valor)
elif final == strftime('%H:%M:%S'):
valor=bytes(('2'),'utf-8')
conexao.write(valor)
break
global start,limit
ligar=Button(janela,command=ligar,text='ligar',width="60")
ligar.grid(row=1)
texto=Label(text='Defina o começo:').grid(row=2)
start=Entry(janela)
start.grid(row=3)
texto2=Label(text='Defina o fim:').grid(row=4)
limit=Entry(janela)
limit.grid(row=5)
salvar=Button(janela,width="30",text='Salvar Horarios',command=lambda: threading.Thread(target=agendar).start())
salvar.grid(row=6)
这是一个简短的细分:
- 按下按钮执行lambda函数(使用lambda函数很有用,因为它允许你避免使用包装函数)
- lambda函数为agendar函数创建一个单独的线程
- 线程开始
注意:我定义了limit并作为全局变量启动,但我不确定你是否需要它。 另外,我还没有测试过,但我相信它确实有效。
另一答案
您应该使用after
定期运行代码。
我无法测试,但我会这样做
from tkinter import *
import serial
from time import strftime
# --
def ligar():
global cont
cont = not cont
if cont:
conexao.write( b'1' )
ligar['text'] = 'Desligar led'
else:
conexao.write( b'2' )
ligar['text'] = 'Ligar led'
def agendar():
comeco = start.get()
final = limit.get()
current = strftime('%H:%M:%S')
if comeco == current:
conexao.write( b'1' )
# run again after 1000ms (1s)
janela.after(1000, agendar)
elif final == current:
conexao.write( b'2' )
conexao = serial.Serial('COM3', 9600, timeout=0.5)
cont = False
janela = Tk()
ligar = Button(janela, command=ligar, text='ligar', width="60")
ligar.grid(row=1)
texto = Label(janela, text='Defina o começo:')
texto.grid(row=2)
start = Entry(janela)
start.grid(row=3)
texto2 = Label(janela, text='Defina o fim:')
texto2.grid(row=4)
limit = Entry(janela)
limit.grid(row=5)
salvar = Button(janela, width="30", text='Salvar Horarios', command=agendar)
salvar.grid(row=6)
janela.mainloop()
以上是关于Tkinter循环“Tkinter没有响应”的主要内容,如果未能解决你的问题,请参考以下文章