LordHomie
LordHomie

Reputation: 23

reading lines in file tkinter

I am just new to tkinter and I could not figure out how to do the following, I have a piece of code that reads line by line in tkinter. There is an entry field to type the line from file and when user presses the button, it should give "correct" if

what he typed == line from the file. if not, so, it prints "incorrect".

i just dont know how to declare the line from file, actually i know how to use
for line in file:

but my teacher told me not to use loops in tkinter.

my code:

import tkinter as tk
root = tk.Tk()

def callback(sv):
    print(sv.get())
    for line in f:
        if text == line:
          print("correct")

        else:
          print("incorrect")
    return sv.get()

f = open('sentences.txt', 'r')
sv = tk.StringVar(root)
sv.trace("w", lambda name, index, mode, sv=sv: callback(sv))

label = tk.Label(root,text="")
label.pack()
e = tk.Entry(root, textvariable=sv)
tk.Button(root, text="Click for next",
          command=lambda: label.config(text=next(f))).pack()
e.pack()
text = e.get()


root.mainloop()

Upvotes: 1

Views: 180

Answers (2)

Gro
Gro

Reputation: 1683

Here is a working program based on my understanding of your requirement. Left comments for easy understanding.

from tkinter import *

#before you proceed to create tkinter window, read the file in a list
fileLineList = [line. rstrip('\n') for line in open("sample.txt")]


#check if the content exactly matches any element in the list
def evaluate(text, board):
    if text in fileLineList:
        board.set("Correct")
    else:
        board.set("Incorrect")


def clear(content, result):
    content.set("")
    result.set("")


#build tkinter window    
root = Tk()
root.geometry("400x200")

content = StringVar()
result=StringVar()

#the entry widget to take user input
entry = Entry(root, textvariable=content)
entry.pack(fill=X, padx=10, pady=10)
entry.focus_set()

evalBtn = Button(root, text="Evaluate", 
         command=lambda: evaluate(text=content.get(), board=result))
evalBtn.pack(fill=X, padx=20, pady=10)

#label to show the result
lbl = Label(root, bg="white", textvariable=result)
lbl.pack(fill=X, padx=10, pady=10)

clearBtn = Button(root, text="Clear", 
         command=lambda: clear(content, result))
clearBtn.pack(fill=X, padx=20, pady=10)

root.mainloop()

Upvotes: 1

Novel
Novel

Reputation: 13729

You need to keep track of the current line in separate variable. Try this:

import tkinter as tk
root = tk.Tk()

f = open('sentences.txt', 'r')
current_line = ''

def callback(*args):
    if sv.get() == current_line:
        print("correct")
    else:
        print("incorrect")

def next_line():
    global current_line
    current_line = next(f)
    label.config(text=current_line)

sv = tk.StringVar(root)
sv.trace("w", callback)

label = tk.Label(root,text="")
label.pack()
e = tk.Entry(root, textvariable=sv)
e.pack()
btn = tk.Button(root, text="Click for next", command=next_line)
btn.pack()

next_line() # autoload the first line of the file; remove this line to start with a blank

root.mainloop()

FWIW, try not to use lambda. It will only confuse you.

Upvotes: 1

Related Questions