2014-01-06 74 views
-1

在我的程序中,用户被要求提供他们的名字,姓氏和高中班级。在程序的最后有一条if语句,如果符合条件,那么我想创建一个文件,它将打印这些变量以及一条或两条消息。如何从python中的用户输入创建文本文件?

我也想要求用户输入一个关于他们自己的简短声明,所以基本上是一个文本项目,这也是要添加到文件中。

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) 
+4

你有没有为此编写过任何代码?你真正的问题是什么?没有人想要为你写代码,如果你甚至没有自己去。不要试图变得讨厌或者任何事情,但是你会首先尝试自己,并在你真正陷入困境时来到这里寻求指导。我保证你会有实际的问题然后:) – Totem

+0

如果你表明你已经付出了一些努力来解决这个问题,你会更成功地找到答案。 – That1Guy

+0

如果你有一个特定的问题,请自己先编写它自己的代码http://docs.python.org/2/tutorial/inputoutput.html回来。我们不会从头开始编写代码。 – CoryKramer

回答

0

您可以使用Python的公开声明和使用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类和初始化手动因为我不知道还有什么您的代码。而且我甚至不知道if语句的测试条件!

+0

文件模式不应该是apend - “a”或写 - “w” –

+0

@ChetanVasudevan不知道OP究竟打算做什么,我在编辑之前根据原始问题对它进行编码,只需要读访问。 – Dashadower

+0

是的,我意识到:)。 –

相关问题