如何同时从多个变量获取值? (Tkinter)

时间:2019-04-13 03:17:50

标签: python-3.x tkinter

我没有从许多变量中获取值(为什么?!),但是当我使用相同的方法时,它从中获取价值(注意:“但是当我使用相同的方法时,它正在从中获取价值“->使用一个变量给我值)。

我尝试过移动函数的位置,首先在按钮上方,现在在代码上方。

def addStudent():

    def SubmitBtnFunc():
        stuname = NameOfNewStudent.get()
        sroll = RollNumberOfNewStudent.get()
        sclass = ClassNameOfNewStudent.get()
        ssection = SectionOfNewStudent.get()
        sphone = PhoneNumberOfNewStudent.get()
        snumber = GrNumberOfNewStudent.get()

        File = open("info.txt", "a+")
        File.write("\n" + stuname + ":")
        File.write(sroll + ":")
        File.write(sclass + ":")
        File.write(ssection + ":")
        File.write(sphone + ":")
        File.write(snumber)

        File.close()

    root2 = Tk()
    root2.title("New Student")
    root2.geometry("350x350")

    NameOfNewStudent = StringVar()
    RollNumberOfNewStudent = StringVar()
    ClassNameOfNewStudent = StringVar()
    SectionOfNewStudent = StringVar()
    PhoneNumberOfNewStudent = StringVar()
    GrNumberOfNewStudent = StringVar()

    lbl1 = Label(root2, text="Enter Student's Name: ")
    lbl1.grid(column=0, row=2, pady=10, padx=7)

    NewStudentName = ttk.Entry(root2, textvariable=NameOfNewStudent)
    NewStudentName.grid(column=1, row=2, pady=13)

    lbl2 = Label(root2, text="Enter Student's roll number: ")
    lbl2.grid(column=0, row=3, padx=7)

    rollNumber = ttk.Entry(root2, textvariable=RollNumberOfNewStudent)
    rollNumber.grid(column=1, row=3)

    lbl3 = Label(root2, text="Enter Student's class: ")
    lbl3.grid(column=0, row=4, padx=7)

    className = ttk.Combobox(root2, values=["I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII"], textvariable=ClassNameOfNewStudent)
    className.current(8)
    className.grid(column=1, row=4, pady=10)

    lbl4 = Label(root2, text="Section: ")
    lbl4.grid(column=0, row=5, padx=7, pady=3)

    Section = ttk.Combobox(root2, values=["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"], textvariable=SectionOfNewStudent)
    Section.grid(column=1, row=5, pady=10)
    Section.current(0)

    lbl5 = Label(root2, text="Phone Number: ", textvariable=PhoneNumberOfNewStudent)
    lbl5.grid(column=0, row=6, padx=7, pady=3)

    StudentNumber = ttk.Entry(root2)
    StudentNumber.grid(column=1, row=6, pady=10)

    lbl6 = Label(root2, text="Gr. Number: ")
    lbl6.grid(column=0, row=7, padx=7, pady=10)

    StudentGrNumber = ttk.Entry(root2, textvariable=GrNumberOfNewStudent)
    StudentGrNumber.grid(column=1, row=7, pady=10)

    SubmitBtn = ttk.Button(root2, text="Submit", command=SubmitBtnFunc)
    SubmitBtn.grid(column=1, row=8, pady=5)

    root2.mainloop()

我希望将输出存储在info.txt文件中,例如-

zaid:36:IX-E:#### Number ####:##注册编号####

但是

我在info.txt文件中得到的结果为:::::

这是因为它没有获取值。

0 个答案:

没有答案