tkinter 中 if 语句的变量有问题



大家好,我的脚本有问题,因为当我启动它进行测试并按 L0 按钮时,我收到以下错误消息:

Tkinter 回调中的异常 回溯(最近一次调用): 文件 "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/init.py",第 1705 行,在调用中 return self.func(*args) 文件"nucleo.py",第 33 行,在 spegni 中 value = int(previous_state.get()) UnboundLocalError:赋值前引用的局部变量 'previous_state'

显示我的完整代码:

import serial
import random
import time
from tkinter import *
from tkinter import messagebox
import os
previuos_state=0
def accendi():
  if previous_state == 0:
    TimeDelay = random.randrange(10,20)
    time.sleep(TimeDelay)
    ser.write('L1'.encode())
    previous_state = 1
    USER_TIME = list(ser.read(5).decode("utf8"))
    if USER_TIME[0] == 'T':

        USER_TIME = str(USER_TIME[1])+str(USER_TIME[2])+str(USER_TIME[3])+str(USER_TIME[4])
        print(str(int(USER_TIME,16))+" ms")
    else:
        print("Errore in ricezione")
  else:
    messagebox.showwarning("Attenzione", "Spegnere led per iniziare un nuovo test")
def spegni():
  if previous_state == 1:
    TimeDelay = random.randrange(10,20)
    time.sleep(TimeDelay)
    ser.write('L0'.encode())
    previous_state = 0
    USER_TIME = list(ser.read(5).decode("utf8"))
    if USER_TIME[0] == 'T':
        USER_TIME = str(USER_TIME[1])+str(USER_TIME[2])+str(USER_TIME[3])+str(USER_TIME[4])
        print(str(int(USER_TIME,16))+" ms")

    else:
        print("Errore in ricezione")
else:
    messagebox.showwarning("Attenzione", "Accendere led per iniziare un nuovo test")

finestra=Tk()
finestra.geometry("520x230")
finestra.title("Misuratore di riflessi")
testo=Label(finestra, text="Premere il pulsante L0 se il LED è accesonPremere il pulsante L1 se il LED è spentonPremere  EXIT per uscire")
testo.grid(row=0, column=0, columnspan=3)
tasto1=Button(finestra, text="L0", command=spegni)
tasto1.grid(row=3, column=0)
tasto2=Button(finestra, text="L1", command=accendi)
tasto2.grid(row=3, column=2)
lista=Listbox(finestra)
lista.insert(END, "uno", "due", "tre", "quattro", "cinque", "sei", "sette", "otto")
lista.grid(row=0, column= 3, columnspan=5, rowspan=5)
tasto3=Button(finestra, text="EXIT", command=exit)
tasto3.grid(row=4, column=1)
finestra.mainloop()

我不明白为什么我不能在函数中使用变量"previuous_state"。

以前的状态在其初始声明中拼写错误

import serial
import random
import time
from tkinter import *
from tkinter import messagebox
import os
previuos_state=0

这使得函数开始时的检查失败,因为未声明变量

正确的代码:

import serial
import random
import time
from tkinter import *
from tkinter import messagebox
import os
previous_state=0
def stampa():
  print(previous_state)
def accendi():
  if previous_state == 0:
    TimeDelay = random.randrange(10,20)
    time.sleep(TimeDelay)
    ser.write('L1'.encode())
    previous_state = 1
    USER_TIME = list(ser.read(5).decode("utf8"))
    if USER_TIME[0] == 'T':

        USER_TIME = str(USER_TIME[1])+str(USER_TIME[2])+str(USER_TIME[3])+str(USER_TIME[4])
        print(str(int(USER_TIME,16))+" ms")
    else:
        print("Errore in ricezione")
   else:
    messagebox.showwarning("Attenzione", "Spegnere led per iniziare un nuovo test")
def spegni():
  if previous_state == 1:
    TimeDelay = random.randrange(10,20)
    time.sleep(TimeDelay)
    ser.write('L0'.encode())
    previous_state = 0
    USER_TIME = list(ser.read(5).decode("utf8"))
    if USER_TIME[0] == 'T':
        USER_TIME = str(USER_TIME[1])+str(USER_TIME[2])+str(USER_TIME[3])+str(USER_TIME[4])
        print(str(int(USER_TIME,16))+" ms")

    else:
        print("Errore in ricezione")
  else:
    messagebox.showwarning("Attenzione", "Accendere led per iniziare un nuovo test")

finestra=Tk()
finestra.geometry("520x230")
finestra.title("Misuratore di riflessi")
testo=Label(finestra, text="Premere il pulsante L0 se il LED è accesonPremere il pulsante L1 se il LED è spentonPremere  EXIT per uscire")
testo.grid(row=0, column=0, columnspan=3)
tasto1=Button(finestra, text="L0", command=spegni)
tasto1.grid(row=3, column=0)
tasto2=Button(finestra, text="L1", command=accendi)
tasto2.grid(row=3, column=2)
lista=Listbox(finestra)
lista.insert(END, "uno", "due", "tre", "quattro", "cinque", "sei", "sette", "otto")
lista.grid(row=0, column= 3, columnspan=5, rowspan=5)     
tasto3=Button(finestra, text="EXIT", command=exit)
tasto3.grid(row=4, column=1)
tastoo=Button(finestra, text="STAMPA", command=stampa)
tastoo.grid(row=1, column=1)
finestra.mainloop()

我继续尝试,但我不能将变量用于 if

最新更新