Tkinter下拉菜单

时间:2017-08-23 13:23:22

标签: python drop-down-menu tkinter user-controls

我正在尝试根据Tkinter下拉菜单添加输入字段,但我不确定我是否正确执行此操作。当我在其中一个下拉菜单中选择一个选项时,我没有弹出任何新的输入字段。代码就是我到目前为止所拥有的。我也尝试将if语句放在外部函数中并在OptionMenu中放置一个命令,但它仍然无效。

    from Tkinter import *


def callback():
    numQues = int(E1.get())
    i = 0
    while i <= numQues:
        variable = StringVar(top)
        variable.set("Questions Type") # default value
        w = OptionMenu(top, variable, "Short Answer", "Multiple Choice", "Fill in the Blank",
                  "True of False", "Matching", "Ordering")

        w.grid(row = i*2+2, column=0)
        ques = StringVar(top, value='Question')
        ans = StringVar(top, value='Answer')
        choice = StringVar(top, value='Answer Choices')

        if variable.get() == "Short Answer":
            q = Entry(top, bd = 5, textvariable = ques)
            q.grid(row=i*2+2, column = 2, columnspan=2)
            a = Entry(top, bd = 5, textvariable = answ)
            a.grid(row=i*2+2, column = 4, columnspan=2)

        if variable.get()  == "Multiple Choice":
            q = Entry(top, bd = 5, textvariable = ques)
            q.grid(row=i*2+2, column = 2, columnspan=2)
            c = Entry(top, bd = 5, textvariable = answ)
            c.grid(row=i*2+2, column = 4, columnspan=2)
            a = Entry(top, bd = 5, textvariable = answ)
            a.grid(row=i*2+2, column = 6, columnspan=2)

        if variable.get()  == "True or False":
            q = Entry(top, bd = 5, textvariable = ques)
            q.grid(row=i*2+2, column = 2, columnspan=2)
            a = Entry(top, bd = 5, textvariable = answ)
            a.grid(row=i*2+2, column = 6, columnspan=2)

        if variable.get()  == "Fill in the Blank":
            q = Entry(top, bd = 5, textvariable = ques)
            q.grid(row=i*2+2, column = 2, columnspan=2)
            a = Entry(top, bd = 5, textvariable = answ)
            a.grid(row=i*2+2, column = 4, columnspan=2)

        if variable.get()  == "Ordering":
            q = Entry(top, bd = 5, textvariable = ques)
            q.grid(row=i*2+2, column = 2, columnspan=2)
            a = Entry(top, bd = 5, textvariable = answ)
            a.grid(row=i*2+2, column = 4, columnspan=2)

        i = i+1


top = Tk()
top.geometry("600x600")
top.title("eCampus Quiz Developer")
L1 = Label(top, text="How many questions will the quiz be?")
L1.grid(row=0, column=0)
E1 = Entry(top, bd = 5)
E1.grid(row=0, column=1)

MyButton1 = Button(top, text="Submit", width=10, command=callback)
MyButton1.grid(row=1, column=1)

top.mainloop()

0 个答案:

没有答案