TKINTER IN PYTHON

0

Hoola give me a file called notes.txt the file looks like this:

NOTAS;EJERCICIO 1;EJERCICIO 2;EJERCICIO 3;EJERCICIO 4;EJERCICIO
AAAA;2;4;7;2;6;4
BBBB;3;5;5;4;5;5
CCCC;4;6;5;5;4;6
DDDD;5;7;5;6;4;4

I need to Open and upload the file notes.txt

Using Tkinter - Show in a dropdown (menuoption) the names of students

Using Tkinter - Show all the student's grades and if this is approved or disapproved (approved - notes = 4.0)

WHAT I HAVE IS THE FOLLOWING .. YOU COULD GIVE ME A PLEASANT HAND ..

from Tkinter import*
from ttk import*
ventana=Tk()
ventana.title('Notas')
cuadro1=Frame(ventana)
cuadro1.pack()
cuadro2=Frame(ventana)
cuadro2.pack()
texto=StringVar()

#Abrir el archivo con open('notas.txt', 'r') y luego recorremos cada linea 
# que vamos leyendo
#Luego, despues de la linea 0. Guardamos nuestras notas en el diccionario 
#"notas={}"

#Por cada linea que vaya leyendo, usamos una función que nos separe la linea 
#de su componentes

#LineaComoLista=linea.split(;) y nos quedara algo asi['LLLL', '1', '2', '7']

#Queremos hacer algo como esto: notas['LLLL']=['1', '2', '7']

#Y LineaComoLista[1:]=['1', '2', '7']

#Al final lo que vamos a tener es un diccionario con los nombres de los 
#alumnos y sus notas

#Uno puede acceder a las llaves de un diccionario con notas.keys() -> 
#entrega una lista con los nombres de los alumnos

valores=notas.keys()
menu=OptionMenu(cuadro1,texto,*valores)
subtitulo=Label(cuadro1, text='Notas de alumno', font=(None, 20))
subtitulo.grid(row=1, column=1)
menu.grid(row=2, column=1)
def LeerNotas(*args):
    NombreDelAlumno=texto.get()
    subtitulo.config(text='Notas de' + NombreDelAlumno)
    NumeroDeNotas = 6

    for y in range(0,5):
        etiqueta=Label(cuadro2, text=str(LeerNotas[y]), font=(None, 30), 
relief=RIDGE)
        etiqueta.grid(row=0, column=y, sticky=NSEW)

texto.trace('r', LeerNotas)
ventana.mainloop()

THANKS

    
asked by Soledad 27.11.2017 в 23:40
source

1 answer

0

First of all I have to comment that I have practically no experience with tkinter so my help will be incomplete.

Having said that, I told you that Python includes the csv library that allows you to parse a file as notas.txt

import csv

notas = {}

with open('notas.txt', mode='r') as infile:
    reader = csv.reader(infile, delimiter=';')
    for linea in reader:
        notas[linea[0]] = linea[1:]

valores = notas.keys()

With that notas is a dictionary that contains

{
    'NOTAS':['EJERCICIO 1', 'EJERCICIO 2', 'EJERCICIO 3', 'EJERCICIO 4', 'EJERCICIO'],
    'AAAA': ['2', '4', '7', '2', '6', '4'],
    'BBBB': ['3', '5', '5', '4', '5', '5'],
    'CCCC': ['4', '6', '5', '5', '4', '6'],
    'DDDD': ['5', '7', '5', '6', '4', '4']
}

Y valores is a dict_keys :

dict_keys(['NOTAS', 'AAAA', 'BBBB', 'CCCC', 'DDDD'])

You want that when you change the selector menu the notes of the student that appears in the selector are printed. For that you are listening when the value of the selector text changes. But instead of

texto.trace('r', LeerNotas)

should be

texto.trace('w', LeerNotas)

because, otherwise, you need a second click to refresh the data.

In LeerNotas you are correctly bringing the NombreAlumno , but you are not bringing the notes. I imagine that when you put:

etiqueta=Label(cuadro2, text=str(LeerNotas[y]), font=(None, 30), 
relief=RIDGE)

That LeerNotas[y] should be the grade obtained in the subject y . But LeerNotas is a function. Not a list. You need an intermediate step to get the notes. For example:

notas_alumno = notas.get(NombreDelAlumno)
for y in range(0, 5):
    etiqueta = Label(
        cuadro2,
        text=str(notas_alumno[y]),
        font=(None, 30),
        relief=RIDGE)
    etiqueta.grid(row=0, column=y, sticky=NSEW)

And with that you will have a behavior as seen in the image:

Probably the output of the notes you need in another format (for example showing the subjects). But I'll leave that for homework, as well as determining if the student is approved or failed.

    
answered by 28.11.2017 в 15:14