from tkinter import *
import random
easy = ['A','V','M','P','O','E']
random.shuffle(easy)
def raise_frame(frame):
frame.tkraise()
root = Tk()
f1 = Frame(root)
f2 = Frame(root)
f3 = Frame(root)
f4 = Frame(root)
for frame in (f1, f2, f3, f4):
frame.grid(row=0, column=0, sticky='news')
Random = Label(f1, text=easy, bg="#00CDCD", font=("Chewy", 20))
Random.pack(side="top", fill="x", pady=20)
Entryfield = Entry(f1, font=("Chewy", 22))
Entryfield.pack(pady=105)
Begin = Button(f1, text='Begin', font=("Chewy", 24), bg="#00CDCD",
command=lambda: Random.pack_forget() + Begin.pack_forget())
Begin.pack(side="bottom", fill="both")
Submit = Button(f1, text="Submit", bg="#00CDCD", font=("Chewy", 24), command=lambda: winlose())
Submit.pack(fill="x", side="bottom")
当我按下Submit时,该语句仅显示一种错误的方式,我尝试了其他条件(例如,如果不是,否则),但仍显示相同的语句,如果我使用!=,则代码仅显示标签。
def winlose():
if Entryfield == Random:
Win = ("Your answer was correct")
tts = Label(f1, text=Win, font=('Chewy', 20), bg="#00CDCD")
tts.pack()
else:
L = ("Your answer is wrong")
ttr = Label(f1, text=L, font=('Chewy', 20), bg="#00CDCD")
ttr.pack()
raise_frame(f1)
root.geometry("800x400")
root.mainloop()
答案 0 :(得分:1)
您应该替换以下行:
if Entryfield == Random:
所在行:
if Entryfield.get() == Random['text']:
这意味着您无需比较Entry
小部件和Label
小部件,而是比较它们各自的文本。
还请注意:
from tkinter import *
应该是:
from tkinter import *
(这很明显,可能只是一个错字)
这可能会帮助您删除该行:
root.geometry("800x400")
没有此行,窗口将自动调整大小以适合其中的所有小部件。
还行:
command=lambda: Random.pack_forget() + Begin.pack_forget())
在调用时会引发错误,因为lambda
不能这样工作:您不能只是添加函数。相反,我将创建一个新函数:
def begin_func():
Random.pack_forget()
Begin.pack_forget()
并将行更改为:
command=begin_func)
最后,我假设您只希望在按下Begin
后才能键入答案,要实现此目的,您应该更改:
Entryfield = Entry(f1, font=("Chewy", 22))
收件人:
Entryfield = Entry(f1, font=("Chewy", 22), state=DISABLED)
并将以下行添加到begin_func
:
Entryfield.config(state=NORMAL)
总而言之,最终代码应为:
from tkinter import *
import random
easy = ['A','V','M','P','O','E']
random.shuffle(easy)
def raise_frame(frame):
frame.tkraise()
def begin_func():
Random.pack_forget()
Begin.pack_forget()
Entryfield.config(state=NORMAL)
root = Tk()
f1 = Frame(root)
f2 = Frame(root)
f3 = Frame(root)
f4 = Frame(root)
for frame in (f1, f2, f3, f4):
frame.grid(row=0, column=0, sticky='news')
Random = Label(f1, text=easy, bg="#00CDCD", font=("Chewy", 20))
Random.pack(side="top", fill="x", pady=20)
Entryfield = Entry(f1, font=("Chewy", 22), state=DISABLED)
Entryfield.pack(pady=105)
Begin = Button(f1, text='Begin', font=("Chewy", 24), bg="#00CDCD",
command=begin_func)
Begin.pack(side="bottom", fill="both")
Submit = Button(f1, text="Submit", bg="#00CDCD", font=("Chewy", 24), command=lambda: winlose())
Submit.pack(fill="x", side="bottom")
def winlose():
if Entryfield.get() == Random['text']:
Win = ("Your answer was correct")
tts = Label(f1, text=Win, font=('Chewy', 20), bg="#00CDCD")
tts.pack()
else:
L = ("Your answer is wrong")
ttr = Label(f1, text=L, font=('Chewy', 20), bg="#00CDCD")
ttr.pack()
raise_frame(f1)
root.mainloop()
答案 1 :(得分:0)
您的状况有几个问题。如果要获取条目的文本,则必须使用Entry.get()
,它将返回文本。其次,您需要获取标签的文本以测试相等性。为此,您可以使用Label["text"]
,它将为您提供标签文本,因为get
方法不适用于标签。还要在if
条件下删除标签文本之间的空格。我还修复了您的代码缩进。
您的if条件可能类似于:if Entryfield.get() == "".join(Random["text"].split(" ")):
from tkinter import *
import random
easy = ['A','V','M','P','O','E']
random.shuffle(easy)
def raise_frame(frame):
frame.tkraise()
root = Tk()
f1 = Frame(root)
f2 = Frame(root)
f3 = Frame(root)
f4 = Frame(root)
for frame in (f1, f2, f3, f4):
frame.grid(row=0, column=0, sticky='news')
Random = Label(f1, text=easy, bg="#00CDCD", font=("Chewy", 20))
Random.pack(side="top", fill="x", pady=20)
Entryfield = Entry(f1, font=("Chewy", 22))
Entryfield.pack(pady=105)
Begin = Button(f1, text='Begin', font=("Chewy", 24), bg="#00CDCD",
command=lambda: Random.pack_forget() + Begin.pack_forget())
Begin.pack(side="bottom", fill="both")
Submit = Button(f1, text="Submit", bg="#00CDCD", font=("Chewy", 24), command=lambda: winlose())
Submit.pack(fill="x", side="bottom")
def winlose():
if Entryfield.get() == "".join(Random["text"].split(" ")):
Win = ("Your answer was correct")
tts = Label(f1, text=Win, font=('Chewy', 20), bg="#00CDCD")
tts.pack()
else:
L = ("Your answer is wrong")
ttr = Label(f1, text=L, font=('Chewy', 20), bg="#00CDCD")
ttr.pack()
raise_frame(f1)
root.geometry("800x400")
root.mainloop()