简体   繁体   中英

rare exception in tkinter callback error message

I'm working on a code for a school which tells the students how many times they can skip classes. It doesn't work because there is an specific mistake that is always appearing:

    Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/tkinter/__init__.py", line 1533, in __call__
    return self.func(*args)
  File "/Users.py", line 52, in inicio
    for line in improt:
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 25: ordinal not in range(128)

I already tried to import mtTkinter but the program didn't recognize it. The mistake says tkinter is getting the wrong number of args. The line that caused the error was this one:

   for line in importar:
        lista.append(line)

But that seems like the right number of arguments, so what's going on?

There are also some strange mistakes that keep the program to run well.

Here I put my code so you can see if it works.

from tkinter import*
import math
import tkinter.filedialog
import traceback
import sys

ventana=Tk()
ventana.title("Este programa le dice cuantas veces mas puede faltar de ausencias 1, 2 y 3")
ventana.geometry("1000x1000")

lista=[]

clas=Label(ventana, text="Si tu materia es Tecno, Frances o Edufis, escribela tal cual en el cuadro").place(x=50,y=90)
classe=Entry(ventana)
classe.place(x=50,y=110)

segu=Label(ventana, text="Si tu materia es Español o Actualidad, escribela tal cual en el cuadro").place(x=50,y=140)
segun=Entry(ventana)
segun.place(x=50,y=160)

tercis=Label(ventana, text="Si tu materia es Qui, Math, Ingles o Filosofia, escribela tal cual en el cuadro").place(x=50,y=190)
terce=Entry(ventana)
terce.place(x=50,y=220)


a=Label(ventana, text="¿Cuantas veces ha faltado por ausencias o retardos 1?").place(x=50,y=250)
aa=Entry(ventana)
aa.place(x=50,y=270)

b=Label(ventana, text="¿Cuantas veces ha faltado por ausencias o retardos 2?").place(x=50,y=300)
bb=Entry(ventana)
bb.place(x=50,y=320)

c=Label(ventana, text="¿Cuantas veces ha faltado por ausencias o retardos 3?").place(x=50,y=350)
cc=Entry(ventana)
cc.place(x=50,y=380)


def inicio ():
    global lista
    global classe
    global segu
    global tercis
    global aa
    global bb
    global cc


    clases=(Entry.get(classe))
    materias = filedialog.askopenfilename(filetypes=(("Archivo de texto","*.txt"),("Cualquier archivo","*.*")))
    improt = open(materias,"r")

    for line in improt:
        lista.append(line)

    if clases== Tecno:
        Tecno= lista(0)

    if clases== Frances:
        Frances= lista(1)

    if clases== Edufis:
        Edufis= lista(2)

    for i in range (0,2):
        total=70


    AM= (total*15)/100

    A=(Entry.get(aa))

    if A<AM:
        res= AM-A

        print("le quedan ", res, " ausencias o retardos 1")

    if A==AM:   

        print("No puede tener mas ausencias o retardos 1")

    if A>AM:

        print("Ya se paso del maximo de ausencias o retardos 1")


    segunda=(Entry.get(segun))

    if segun== Español:
        Español= lista(3)

    if segun== Actualidad:
        Actualidad= lista(4)

    for i in range(3,4):
        totala=140

    BM= (totala*15)/100

    B=(Entry.get(bb))

    if B<AM:
        tes= AM-B

        print("le quedan ", tes, " ausencias o retardos 2")

    if B==AM:

        print("No puede tener mas ausencias o retardos 2")

    if B>AM:

        print("Ya se paso del maximo de ausencias o retardos 2")

    tercera=(Entry.get(terce))

    if terce== Qui:
        Qui= lista(5)

    if terce== Math:
        Math= lista(6)

    if terce== Ingles:
        Ingles= lista(7)

    if terce== Filo:
        Filo= lista(8)

    for i in range (5,8):
        totale=175

    CM= (totale*15)/100

    C=(Entry.get(cc))

    if C<BM:
        pes= BM-C

    print ("le quedan ", pes, " ausencias o retardos 2")

    if C==BM:

        print ("No puede tener mas ausencias o retardos 2")

    if C>BM:

        print ("Ya se paso del maximo de ausencias o retardos 2")



    improt.close()

escoge = Button(ventana, text = "Escoge un archivo y despues escribe tu materia en el cuadro que corresponda", command = inicio).place(x =45, y = 30)
cierra = Button(ventana, text = "Cierra", command = ventana.destroy).place(x = 50, y = 420)

ventana.mainloop()

It looks like your system default is ASCII

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 25: ordinal not in range(128)

Assuming the language the file is using is Latin-1, try adding

#! /usr/bin/python     ## (your shebang may be slightly different)
# -*- coding:latin-1 -*-
## next line after the shebang 

## and then
for line.decode('latin-1') in improt:

If you open the file in Firefox and then View > Text Encoding, it should say what the encoding is. There are also websites that will do this. Also, this is a common problem (not a rare exception) so a search for "UnicodeDecodeError: 'ascii' codec can't decode byte" should produce a lot of hits

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM