在我的计划中,要求用户提供姓名,姓氏和高中课程。在程序结束时有一个if语句,如果条件满足,那么我想创建一个文件,它将打印这些变量以及一两条消息。
我还想让用户输入关于他们自己的简短陈述,所以基本上是一个文本条目,这也将被添加到文件中。
class Score_Window(tk.Toplevel):
'''A simple instruction window'''
def __init__(self, parent):
tk.Toplevel.__init__(self, parent)
score_str = str(sum(parent.score_per_question.values()))
self.score = tk.Label(self, width=80, height=4, text = "You're score was: " + score_str)
self.score.pack(side="top", fill="both", expand=True)
if int(score_str) >= 3:
print("Pass")
self.prefect = tk.Label(self, width=80, height=4, text = "You have passed, well done! You can now become a prefect.")
self.prefect.pack(side="top", fill="both", expand=True)
self.name = tk.Label(self, width=80, height=4, text = student_name)
self.name.pack(side="top", fill="both", expand=True)
self.surname = tk.Label(self, width=80, height=4, text = student_surname)
self.surname.pack(side="top", fill="both", expand=True)
self.tutor = tk.Label(self, width=80, height=4, text = student_tutor_group)
self.tutor.pack(side="top", fill="both", expand=True)
else:
print("Fail")
self.fail = tk.Label(self, width=80, height=4, text = "Unfortunately you have not scored highly enough to be considered for a prefect position.")
self.fail.pack(side="top", fill="both", expand=True)
答案 0 :(得分:0)
你可以使用python的open语句并使用tksimpiledialog获取输入。 假设你已经设置了这个类:
from Tkinter import * # tkinter in 3.0+
from tkSimpleDialog import askstring
Name = tkSimpleDialog.askstring("User data","Please enter your name")
Surname = tkSimpleDialog.askstring("User data", "Please enter your surname")
somevariable = open("data.txt", "w")
somevariable.write(Name, " ",Surname) # add a space so it is easier to read
userinfo = tkSimpleDialog.askstring("User data", "Enter something about yourself")
somevariable.write(" ",userinfo
# make if statements and a new canvas
with open("data.txt", "r") as l
for lines in l:
name2, Surname2, userinfo2 = lines.split()
namelabel = label(master, text=name2)
namelabel.pack()
surnamelable = label(master, text=Surname2)
surnamelable.pack()
Userinfolabel = label(master, text= userinfo2)
Userinfolabel.pack()
# do whatever you want here, perhaps a reset button?
你必须手动设置主Tk类和 init ,因为我不知道你的代码还有什么。我甚至不知道if语句的测试条件!