我正在使用 Pathlib 打开一个文件夹,以便我可以在该文件夹内创建一个 txt 文件,但我收到了一个错误

阿德里安·克鲁兹

有一个我似乎无法修复的类型错误,它在运行它并通过测验创建者之后发生。

我是怎么填写的

它说错误来自代码的第 61 行

Exception in Tkinter callback

Traceback (most recent call last):

File "C:\Users\adria\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)

File "C:/Users/adria/Downloads/cafe/test.py", **line 61**, in appendtofile

file = open(Path("quizs","{}.txt".format(self.file_name)),"a")

TypeError: an integer is required (got type str)

对愿意提供帮助的人:谢谢你的帮助。

from tkinter import *
from pathlib import *
from os import *

def clearingthefrmae(x):
    for widget in x.winfo_children():
        widget.destroy() 


class quizcreator():
    def __init__(self, parent):
        self.quiz_creator = Frame(parent, width=500, height=500)
        self.quiz_creator.pack()
        self.home_page()



    def home_page(self):
        label_name = Label(self.quiz_creator,text="Quiz creator", font="none 63 bold") .grid(row=0, column=0,columnspan=3,rowspan=3)
        create_button = Button(self.quiz_creator,text="Create", command=lambda:[clearingthefrmae(self.quiz_creator),self.creating_quiz_page()]).grid(row=4,column=1)


    def creating_quiz_page(self):
        back_button = Button(self.quiz_creator,text="Back", width=4, command=lambda:[clearingthefrmae(self.quiz_creator),self.home_page()]).grid(row=0,column=0)
        self.name()


    def name(self):
        name_label = Label(self.quiz_creator, text="What is the name of the quiz you are going to make: ").grid(row=1,column=0)
        file_name = Entry(self.quiz_creator, width=50)
        file_name.grid(row=2,column=0)
        continue_button = Button(self.quiz_creator, text="Continue", command=lambda:[collect(self.quiz_creator)]).grid(row=3, column=0)

        def collect(x):
            files_name = file_name.get()
            clearingthefrmae(x)
            self.creator(files_name)   


    def creator(self,file_name):
        self.file_name = file_name
        questions=["what is the name of the question you wish to add: ","first choice: ","second choice: ","third choice: ","fourth choice: "]
        user_answers=["question_name", "choice1", "choice2", "choice3", "choice4"]
        self.answer = StringVar(self.quiz_creator," ")
        Label(self.quiz_creator, text="Name of quiz/Name of text document:").grid(row=0,column=0)
        Label(self.quiz_creator, text=file_name).grid(row=1,column=0)

        for x in range(0,10,2):
            Label(self.quiz_creator, text="{}".format(questions[int(x/2)])).grid(row=x+2, column=0)
            user_answers[int(x/2)] = Entry(self.quiz_creator, width=60)
            user_answers[int(x/2)].grid(row=x+3, column=0)

        Label(self.quiz_creator, text="answer is: ").grid(row=13, column=0)
        for x in range(0, 4):
            Radiobutton(self.quiz_creator, text="choice {}".format(x+1), value=x, variable=self.answer,width=20).grid(row=14+x, column=0)       

        continue_button = Button(self.quiz_creator, text="Continue", command=self.appendtofile).grid(row=21, column=0)


    def appendtofile(self):
        file = open(Path("quizs","{}.txt".format(self.file_name)),"a")
        file.write("{}\n".format(quiz_name))
        file.write("[{},{},{},{},{},{}]\n".format(user_answers[0], user_answers[1], user_answers[2], user_answers[3], user_answers[4],self.answer.get()))
        file.close()



if __name__ == "__main__":
    try:
        mkdir("scores")
        mkdir("quizzes")
    except Exception:
        pass


    quiz_creator_and_runner = Tk()
    quiz_creator_and_runner.title("Quiz Creator & Runner")
    quiz_creator_and_runner.geometry("500x500")
    quizcreator(quiz_creator_and_runner)
    quiz_creator_and_runner.mainloop()
绝杀

将此行更改为

file = open(Path("quizs","{}.txt".format(self.file_name)),"a")

file = Path("quizs","{}.txt".format(self.file_name)).open('a')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章