這是我的代碼:
from tkinter import *
import random
from pathlib import Path
from typing import Text
score = 0
def handle(*args):
global totype
global typevar
global score
print(f'{typevar.get()}, {totype}')
if typevar.get().strip() == totype:
score = 1
print(score)
p = Path(__file__).with_name('words.txt')
with p.open('r') as f:
words = f.readlines()
tab = Tk()
tab.geometry('300x200')
typevar = StringVar()
rand = random.randint(1, len(words))
totype = words[rand]
tab.bind('<Return>', handle)
type = Label(tab, text=totype).pack()
typed = Entry(tab, show = '^', textvariable = typevar).pack()
tab.mainloop()
它作業得很好(除了它不會給我新詞)但它無法識別您何時輸入正確的詞。當您輸入某些內容時,它應該會增加您的分數并在您輸入正確時列印您的當前分數。但是,即使您輸入正確,它也不會列印任何內容。不過,這兩個變數是相同的。
我怎樣才能解決這個問題?
uj5u.com熱心網友回復:
您正在洗掉錯誤的文本,您需要從檔案中洗掉讀取的文本,而不是從永遠不會有的條目的輸入中洗掉\n:
def handle(*args):
global score # Removed unwanted globals
print(f'{typevar.get()}, {totype}')
if typevar.get() == totype.strip():
score = 1
print(score)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/402459.html
標籤:
