我正在使用小部件构建一个简单的程序,该程序将根据某人的分数显示其分数。例如。 5 = A,4 = B ... 1&0 = F。
我的程序面临的一个问题是在输入框中输入分数后,将结果显示在标签中。我最初运行程序时,它是根据分数显示成绩的,而不是根据Entry
框显示的。
我为您提供了无数涉及输入框的视频,但没有一个起作用。我还尝试将成绩(“成绩”)设置为与输入框(entry
)相等。
这是我的程序:
'''Program to connect entry to button and output results'''
import tkinter as tk
constant = tk.Tk()
def put_grade():
entry = tk.Entry(constant)
entry.place(relx = .4, rely = .1, relwidth = .2, relheight = .1)
values = [5, 4, 3, 2, 1, 0]
grades = ["A", "B", "C", "D", "F", "F"]
# Source of problem
grade = entry
done = False
index = 0
while not done:
if values[index] == grade:
grade = grades[index]
done = True
index += 1
return grade
def get_grade(grade):
label = tk.Label(constant, bg = "light blue", text = grade)
label.place(relx = .4, rely = .2, relwidth = .2, relheight = .5)
def button_clicked():
button = tk.Button(constant, bg = "light green", text = "Check grade", command = put_grade)
button.place(relx = .4, rely = .6, relwidth = .2, relheight = .1)
def main():
grade = put_grade()
get_grade(grade)
button_clicked()
tk.mainloop()
if __name__ == '__main__':
main()
如果有人需要对我的代码或预期的输出做进一步的说明,我将很高兴进行澄清。
答案 0 :(得分:1)
这是原始代码的某种修改版本。您可以解决其余的逻辑问题。
'''Program to connect entry to button and output results'''
import tkinter as tk
values = [5, 4, 3, 2, 1, 0]
grades = ["A", "B", "C", "D", "F", "F"]
root = tk.Tk()
def put_grade(entry,label):
grade=entry.get()
try:
grade=int(grade)
except Exception as e:
grade=0
done = False
index = 0
while not done:
if values[index] == grade:
grade = grades[index]
done = True
index += 1
label.config(text=grade)
def make_gui(root):
entry = tk.Entry(root)
entry.place(relx = .4, rely = .1, relwidth = .2, relheight = .1)
label = tk.Label(root, bg = "light blue", text = "0")
label.place(relx = .4, rely = .2, relwidth = .2, relheight = .5)
button = tk.Button(root, bg = "light green", text = "Check grade", command = lambda:put_grade(entry,label))
button.place(relx = .4, rely = .6, relwidth = .2, relheight = .1)
def main():
make_gui(root)
tk.mainloop()
if __name__ == '__main__':
main()