【问题标题】:How do I create a text file from user input in python?如何从 python 中的用户输入创建文本文件?
【发布时间】:2014-01-06 18:33:29
【问题描述】:

在我的程序中,用户被要求输入他们的姓名、姓氏和高中班级。在程序的末尾有一个 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)

【问题讨论】:

  • 到目前为止,您是否为此编写过任何代码?你的实际问题是什么?如果您自己都没有尝试过,没有人愿意为您编写代码。不要试图变得讨厌或任何东西,但你会先尝试自己,然后在你真正陷入困境时来这里寻求指导,你会学到更多。我保证你会有实际的问题:)
  • 如果你表明你已经付出了一些努力来解决这个问题,你会更成功地找到答案。
  • 先自己编写代码docs.python.org/2/tutorial/inputoutput.html 如果您有具体问题,请回来。我们不会从头开始为您编写代码。
  • raw_input 可能是您要找的东西?
  • 对不起,伙计们:我已经编写了代码,并且几乎完成了我的程序,这是一个十题多项选择测验。我不是在寻找一个人来编写整个事情,而只是一个如何完成的示例将是一个很好的帮助。

标签: python file tkinter tkinter-entry


【解决方案1】:

您可以使用 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 语句的测试条件!

【讨论】:

  • 文件模式不应该是追加 - “a”还是写入 - “w”
  • @ChetanVasudevan 在不知道 OP 究竟打算做什么的情况下,我根据编辑前的原始问题对其进行了编码,只需要读取权限。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-24
  • 2014-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-08
相关资源
最近更新 更多