Tkinter循环“Tkinternãorestarespondendo”

时间:2017-12-24 15:46:05

标签: python loops tkinter arduino

我有一个来自与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)

2 个答案:

答案 0 :(得分:1)

您可以使用线程,而不是使用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)

这是一个简短的细分:

  1. 按下按钮执行lambda函数(使用lambda函数很有用,因为它允许你避免使用包装函数)
  2. lambda函数为您的agendar函数创建一个单独的线程
  3. 线程开始
  4. 注意:我定义了limit并以全局变量开头,但我不确定你是否需要它。
    另外,我还没有测试过,但我确信它有效。

答案 1 :(得分:0)

您应该使用 if (Build.VERSION.SDK_INT >= 23) { alarm.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, interval * SystemClock.elapsedRealtime() + 60 * 1000 , pending); } else if (Build.VERSION.SDK_INT >= 19) { alarm.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, interval * SystemClock.elapsedRealtime() + 60 * 1000, pending); } else { alarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, interval * SystemClock.elapsedRealtime() + 60 * 1000, pending); } 定期运行代码。

我无法测试,但我会做

after