0

我正在尝试运行 If 语句,而我的滚动小部件仅适用于其他 1、2、3、4、5。这个数字由用户在 gui 中输入,然后确定哪些字符串用于变量 TextForReview。我收到错误“NameError: name 'ChildFirstBox4' is not defined”,但它们都是全局变量。我正在使用滚动小部件来清理 gui,因此在尝试小部件之前,.place 将适用于所有变量。有什么我没有正确调用或者函数的顺序是正确的吗?

    def Scroll():
        global ChildScroll
        global text_area
        ForScrollText()
        # Title Label
        ChildScroll = Label(AppWindow, text="ScrolledText Widget Example",
                            font=("Times New Roman", 15),background='green',
                            foreground="white")
        ChildScroll.place(relx=0.5, rely=0.03)

       # Creating scrolled text area
        # widget with Read only by
        # disabling the state
        text_area = st.ScrolledText(AppWindow, width=15,height=8,
                            font=("Ariel",10))

        text_area.place(relx=0.5, rely=0.1, relheight=.4, relwidth=.3)
        # Inserting Text which is read only
        # IF statement + Batman functions
        text_area.insert(tk.INSERT, TextForReview)

        # Making the text read only
        text_area.configure(state='disabled')

    #All the different texts for info page for children
    def BatmanCommand():
        Batman1()
        Batman2()
        Batman3()
        Batman4()
        Batman5()
        Batman6()

    # Infomation for child 1
    def Batman1():
        global a
        a = ChildFirstBox1.get() + ", " + DOBBox1.get() + ", " + GenBox1.get() + ", " + AgeBox1.get()\
            + "\nShirt Size: " + ShirtBox1.get()\
            + "\nPants Size: " + PantsBox1.get()\
            + "\nShoes Size: " + ShoeBox1.get() \
            + "\nWish 1: " + WishBox1.get()\
            + "\nWish 2: " + WishBox2.get()\
            + "\nNeed: " + WishBox3.get()


    # Infomation for child 2
    def Batman2():
        global b
        b = ChildFirstBox2.get() + ", " + DOBBox2.get() + ", " + GenBox2.get() + ", " + AgeBox2.get()\
            + "\nShirt Size: " + ShirtBox2.get()\
            + "\nPants Size: " + PantsBox2.get()\
            + "\nShoes Size: " + ShoeBox2.get() \
            + "\nWish 1: " + WishBox1_2.get()   \
            + "\nWish 2: " + WishBox2_2.get()  \
            + "\nNeed: " + WishBox3_2.get()    \

    # Infomation for child 3
    def Batman3():
        global c
        c = ChildFirstBox3.get() + ", " + DOBBox3.get() + ", " + GenBox3.get() + ", " + AgeBox3.get()\
            + "\nShirt Size: " + ShirtBox3.get()\
            + "\nPants Size: " + PantsBox3.get()\
            + "\nShoes Size: " + ShoeBox3.get() \
            + "\nWish 1: " + WishBox1_3.get()   \
            + "\nWish 2: " + WishBox2_3.get()  \
            + "\nNeed: " + WishBox3_3.get()    \

    # Infomation for child 4
    def Batman4():
        global d
        d = ChildFirstBox4.get() + ", " + DOBBox4.get() + ", " + GenBox4.get() + ", " + AgeBox4.get()\
            + "\nShirt Size: " + ShirtBox4.get()\
            + "\nPants Size: " + PantsBox4.get()\
            + "\nShoes Size: " + ShoeBox4.get() \
            + "\nWish 1: " + WishBox1_4.get()   \
            + "\nWish 2: " + WishBox2_4.get()  \
            + "\nNeed: " + WishBox3_4.get()  \

    # Infomation for child 5
    def Batman5():
        global e
        e = ChildFirstBox5.get() + ", " + DOBBox5.get() + ", " + GenBox5.get() + ", " + AgeBox5.get()\
            + "\nShirt Size: " + ShirtBox5.get()\
            + "\nPants Size: " + PantsBox5.get()\
            + "\nShoes Size: " + ShoeBox5.get() \
            + "\nWish 1: " + WishBox1_5.get()   \
            + "\nWish 2: " + WishBox2_5.get()  \
            + "\nNeed: " + WishBox3_5.get()    \

    # Infomation for child 6
    def Batman6():
        global f
        f = ChildFirstBox6.get() + ", " + DOBBox6.get() + ", " + GenBox6.get() + ", " + AgeBox6.get()\
            + "\nShirt Size: " + ShirtBox6.get()\
            + "\nPants Size: " + PantsBox6.get()\
            + "\nShoes Size: " + ShoeBox6.get() \
            + "\nWish 1: " + WishBox1_6.get()   \
            + "\nWish 2: " + WishBox2_6.get()  \
            + "\nNeed: " + WishBox3_6.get()    \

    def ForScrollText():
        BatmanCommand()
        global TextForReview
        TextForReview = ""
        if NumKids.get() == "1":
            TextForReview = "  " + a

        elif NumKids.get() == "2":
            TextForReview = "  " + a + "\n\n" + b

        elif NumKids.get() == "3":
            TextForReview = "  " + a + "\n\n" + b + "\n\n" + c

        elif NumKids.get() == "4":
            TextForReview = "  " + a + "\n\n" + b + "\n\n" + c\
                            + "\n\n" + d

        elif NumKids.get() == "5":
            TextForReview = "  " + a + "\n\n" + b + "\n\n" + c\
                            + "\n\n" + d + "\n\n" + e

        else:
            TextForReview = "  " + a + "\n\n" + b + "\n\n" + c\
                            + "\n\n" + d + "\n\n" + e + "\n\n" + f

这是 ChildFirstBox 4 定义的地方。上面代码中的 .get() 其余部分的定义与下面的代码类似。

    def Child4():
       global ChildFirstLabel4
       global ChildFirstBox4
       global ChildLastLabel4
       global ChildLastBox4
       global AgeLabel4
       global AgeBox4
       global ShirtsLabel4
       global ShirtBox4
       global PantsLabel4
       global PantsBox4
       global ShoeLabel4
       global ShoeBox4
       global GenLabel4
       global GenBox4
       global DOBLabel4
       global DOBBox4
       ChildFirstLabel4 = Label(AppWindow, text="f name")
       ChildFirstLabel4.place(relx=0.6, rely=0.32)
       ChildFirstBox4 = Entry(AppWindow, width=10)
       ChildFirstBox4.insert(0, "")
       ChildFirstBox4.place(relx=0.6, rely=0.36)
       ChildLastLabel4 = Label(AppWindow, text="l name")
       ChildLastLabel4.place(relx=0.68, rely=0.32)
       ChildLastBox4 = Entry(AppWindow, width=10)
       ChildLastBox4.insert(0, "")
       ChildLastBox4.place(relx=0.68, rely=0.36)
       AgeLabel4 = Label(AppWindow, text="Enter Child's Age")
       AgeLabel4.place(relx=0.6, rely=0.4)
       AgeBox4 = Entry(AppWindow, width=5)
       AgeBox4.insert(0, "")
       AgeBox4.place(relx=0.6, rely=0.44)
       ShirtsLabel4 = Label(AppWindow, text="Shirt size")
       ShirtsLabel4.place(relx=0.74, rely=0.32)
       ShirtBox4 = Entry(AppWindow, width=10)
       ShirtBox4.insert(0, "")
       ShirtBox4.place(relx=0.74, rely=0.36)
       PantsLabel4 = Label(AppWindow, text="Pant size")
       PantsLabel4.place(relx=0.74, rely=0.4)
       PantsBox4 = Entry(AppWindow, width=10)
       PantsBox4.insert(0, "")
       PantsBox4.place(relx=0.74, rely=0.44)
       ShoeLabel4 = Label(AppWindow, text="Shoe Size")
       ShoeLabel4.place(relx=0.80, rely=0.32)
       ShoeBox4 = Entry(AppWindow, width=5)
       ShoeBox4.insert(0, "")
       ShoeBox4.place(relx=0.80, rely=0.36)
       GenLabel4 = Label(AppWindow, text="Gender")
       GenLabel4.place(relx=0.8, rely=.4)
       GenBox4 = Entry(AppWindow, width=10)
       GenBox4.insert(0, "")
       GenBox4.place(relx=0.8, rely=0.44)
       DOBLabel4 = Label(AppWindow, text="DOB")
       DOBLabel4.place(relx=0.86, rely=0.32)
       DOBBox4 = Entry(AppWindow, width=15)
       DOBBox4.insert(0, "")
       DOBBox4.place(relx=0.86, rely=0.36)
4

0 回答 0