创建Tkinter文本框并插入字典

时间:2017-08-30 15:34:04

标签: python loops dictionary tkinter

我有很长一段代码。我不想把它全部粘贴在这里,所以让我解释一下我在这里想要完成的事情。基于用户提供的数字,我想创建许多文本框,然后获取输入到该文本框中的内容并将其插入到字典中。我尝试过这几种方法,但却无法让它正常工作。该列表为空或仅包含最后一个文本框作为每个键的值。

def multiple_choice():
    def add():
        top.destroy()



    top = Tk()
    top.title("Add Question")
    w = 800
    h = 800
    ws = top.winfo_screenwidth()
    hs = top.winfo_screenheight()
    x = (ws/2) - (w/2)
    y = (hs/2) - (h/2)
    top.geometry('%dx%d+%d+%d' % (w, h, x, y))

    question = Label(top, text="Question to be asked?", font = "Times 14 bold", fg = "blue")
    question.grid(row = 2, column = 4)
    questionText = Text(top, borderwidth = 5, width=50,height=5, wrap=WORD, background = 'grey')
    questionText.grid(row = 3, column = 4)

    numQuestions = Label(top, text = "Number of answer choices?", font = "Times 14 bold", fg = "blue")
    numQuestions.grid(row = 4, column=4)
    num = Entry(top, bd = 5)
    num.grid(row=5, column = 4)
    answerList = {}


    def multiple():
        def preview():
            preview = Tk()
            top.title("Question Preview")
            w = 500
            h = 500
            ws = top.winfo_screenwidth()
            hs = top.winfo_screenheight()
            x = (ws/2) - (w/2)
            y = (hs/2) - (h/2)
            top.geometry('%dx%d+%d+%d' % (w, h, x, y))
            title = Label(preview, text = "Short Answer Question Preview", font = "Times 18 bold", fg = "blue" )
            title.grid(row = 0, column = 2)
            qtext = "Question text will read: "
            ques = Label(preview, text = qtext)
            ques.grid(row=1, column = 2)
            ques2 = Label( preview, text = questionText.get("1.0",END))

            let = 'A'
            i = 1
            for word in answerList:
                prev = let + ": " + word
                ans = Label(preview, text = prev)
                ans.grid(row=1+i, column = 2) 
                let =  chr(ord(let) + 1)

            answerCor = "The correct answer(s): "
            a = Label(preview, text = answerCor)
            a.grid(row=4, column = 2)
            b = Label(preview, text = cor.get)
            b.grid(row=5, column = 2)

        if num.get().isdigit():
            number = int(num.get())
            AnswerChoices = Label(top, text = "Answer Choices?", font = "Times 14 bold", fg = "blue")
            AnswerChoices.grid(row = 6, column=4)

            i = 0
            let = 'A'
            while i < number:
                letter = Label(top, text = let)
                letter.grid(row = 8+(i*4), column = 3)
                answer = Text(top, borderwidth = 5, width=50, height=3, wrap=WORD, background = 'grey')
                answer.grid(row = 8+(i*4), column = 4)
                answerList[let] = answer.get("1.0",END)
                i = i+1
                let =  chr(ord(let) + 1)


            print answerList
            correct = Label(top, text = "Correct Answer(s) (seperated by commas)", 
                            font = "Times 14 bold", fg = "blue")
            correct.grid(row =99 , column=4)
            cor = Text(top, borderwidth = 5, width=50, height=3, wrap=WORD, background = 'grey')
            cor.grid(row=100, column = 4)


        else:
            error = Tk()
            w = 500
            h = 100
            ws = top.winfo_screenwidth()
            hs = top.winfo_screenheight()
            x = (ws/2) - (w/2)
            y = (hs/2) - (h/2)
            error.geometry('%dx%d+%d+%d' % (w, h, x, y))
            text = "ERROR: You must enter an integer number"
            Label(error,text = text, fg = "red", font = "Times 16 bold").pack()
        MyButton5 = Button(top, text="Preview Question", width=20, command = preview, anchor=S)
        MyButton5.grid(row=102, column=5)
        MyButton4 = Button(top, text="Finish and Add Question", width=20, command = add, anchor=S)
        MyButton4.grid(row=102, column=2)

    but = Button(top, text="Submit", width=10, command = multiple)
    but.grid(row=6, column = 4)

    top.mainloop()

def button():
    MyButton21 = Button(quiz, text="Short Answer Question", width=20, command = short_answer)
    MyButton21.grid(row=8, column=2)
    MyButton22 = Button(quiz, text="True/False Question", width=20, command = true_false)
    MyButton22.grid(row=8, column=4)
    MyButton23 = Button(quiz, text="Multiple Choice Question", width=20, command = multiple_choice)
    MyButton23.grid(row=9, column=2)
    #MyButton24 = Button(quiz, text="Matching Question", width=20, command = matching)
    #MyButton24.grid(row=9, column=4)
    MyButton25 = Button(quiz, text="Ordering Question", width=20, command =order)
    MyButton25.grid(row=10, column=2)
    MyButton26 = Button(quiz, text="Fill in the Blank Question", width=20, command = fill_blank)
    MyButton26.grid(row=10, column=4)

    MyButton3 = Button(quiz, text="Finsh Quiz", width=10, command = quiz)
    MyButton3.grid(row=12, column=3)


quiz = Tk()
w = 700
h = 300
ws = quiz.winfo_screenwidth()
hs = quiz.winfo_screenheight()
x = 0
y = 0
quiz.geometry('%dx%d+%d+%d' % (w, h, x, y))
quiz.title("eCampus Quiz Developer")
L1 = Label(quiz, text="Quiz Title?")
L1.grid(row=0, column=0)
E1 = Entry(quiz, bd = 5)
E1.grid(row=0, column=3)
name_file = E1.get()
name_file = name_file.replace(" ", "")
name_file = name_file + ".txt"
with open(name_file,"w") as data:
    MyButton1 = Button(quiz, text="Submit", width=10, command = button)
    MyButton1.grid(row=1, column=3)
    quiz.mainloop()

我正在尝试使用这段代码创建字典:

i = 0
let = 'A'
while i < number:
  letter = Label(top, text = let)
  letter.grid(row = 8+(i*4), column = 3)
  answer = Text(top, borderwidth = 5, width=50, height=3, wrap=WORD, background = 'grey')
  answer.grid(row = 8+(i*4), column = 4)
  answerList[let] = answer.get("1.0",END)
  i = i+1
  let =  chr(ord(let) + 1)

我甚至尝试在预览函数中放置一个循环,但是当最后一个框是字典中包含的唯一值时。任何想法将不胜感激

1 个答案:

答案 0 :(得分:0)

请参阅下面的我所记录的片段,其中说明了这一点:

from tkinter import *

class App:
    def __init__(self, root):
        self.root = root
        self.entry = Entry(self.root) #entry to input number of entries later
        self.button = Button(self.root, text="ok", command=self.command) #calls command to draw the entry boxes
        self.frame = Frame(self.root)
        self.entry.pack()
        self.button.pack()
        self.frame.pack()
    def command(self):
        self.frame.destroy() #destroys frame and contents
        self.frame = Frame(self.root) #recreates frame
        self.text = [] #empty array to store entry boxes
        for i in range(int(self.entry.get())):
            self.text.append(Entry(self.frame, text="Question "+str(i))) #creates entry boxes
            self.text[i].pack()
        self.done = Button(self.frame, text="Done", command=self.dict) #button to call dictionary entry and print
        self.done.pack()
        self.frame.pack()
    def dict(self):
        self.dict = {}
        for i in range(len(self.text)):
            self.dict.update({self.text[i].cget("text"): self.text[i].get()})
            #the above line adds a new dict entry with the text value of the respective entry as the key and the value of the entry as the value
        print(self.dict) #prints the dict

root = Tk()
App(root)
root.mainloop()

上面询问用户他们想要创建多少个entry小部件,使用这些frame小部件填充entry,然后遍历包含它们的list按下button,为每个dictionary窗口小部件添加新的entry条目,其中key的{​​{1}}是属性dict对于每个textentry的{​​{1}}是value的{​​{1}}。