我们可以在方法中定义的varibale上使用global吗?

时间:2017-12-01 02:48:43

标签: tkinter global python-3.6

wrongcounter = 0

from tkinter import *
from tkinter import ttk
#the algebra questions
question_of_alge_levels = {             '1' : 'Solve g - 8 = 16?' ,
                                        '2' : 'Solve x - 56 = 23?' ,
                                        '3' : 'Enter 3x / 3 = 3?' ,
                                        '4' : 'Enter 10x / 5 = 40?' ,
                                        '5' : 'Enter 70x + 100 = 240?' ,
                                        '6' : 'Enter 95x + 130 =320',
                                        '7' : 'Enter 4x + 32 + 52 = 163' ,
                                        '8' : 'Enter 4(2a+3)=-3(a-1)+31',
                                        '9' : 'Enter 5(7a+5)=-3(a-2)+42',
                                        '10': '12(9a+5)=-10(a-4)+48?',
                                        '11':'You Have Finished You Are a Genius Panther:)'
                                          }  
#the answers of algebra questions                      
anwser_of_alge_levels = {              '1' : '24' ,
                                       '2' : '79' ,
                                       '3' : '3' ,
                                       '4' : '20' ,
                                       '5' : '2'  ,
                                       '6' : '2' ,
                                       '7' : '19.75' ,
                                       '8' : '2' ,
                                       '9' : '0.60' ,
                                      '10' : '0.23',
                                      '11' : '{Pha<n>tex}'
                                      }

我将此窗口分组为一个方法,以便将其绑定到我的按钮中 所以可以单击此按钮并显示新窗口。

root = Tk()




          #Algebra Section
#the algebra window grouped into a method
def algebra(a):
         #declaring the level as 1
        level = 1
         #our window(window)

        window = Tk()
         #Algebra Answer entry
        Alge_answerbox = ttk.Entry(window)
        Alge_answerbox.grid(row = 2 , column = 0 , sticky = 'we')
               #algebra label
        Alge_Question = ttk.Label(window , text=question_of_alge_levels[str(level)],font = 'times 10 bold')
        Alge_Question.grid(row = 1, column = 0 , sticky = 'we')
              #something that not 

              #submit button  
        #the submit button inside of the algebra window

        alge_submit = ttk.Button(window  , text= 'Submit x')
        alge_submit.grid(row = 2, column = 1, sticky = 'we')
        #binding the submit button so the answer is checked when clicked

        alge_submit.bind('<Button-1>' , algebra_method)
        #the numberwrong 
        alge_NumberWrong = ttk.Label(window , text = 'Number Incorrect : 0')
        alge_NumberWrong.pack()
        root.withdraw()

问题出现在哪里,我无法获得条目Alge_answerbox的值,即使它被定义为全局,我不确定这是否合乎逻辑,如果没有,你能引导我到另一个解决方案,以便algebra_method可以获取条目Alge_answerbox的值,并使用其数据

def algebra_method(a):


    level = 1
    global Alge_answerbox
    global wrongcounter

    #checking to see if the answer is wrong          
    while True:

        if Alge_answerbox.get() != anwser_of_alge_levels[str(level)]:
            wrongcounter +=1
            alge_NumberWrong.configure(text ='Number Incorrect: ' + str(wrongcounter))
        break
    #checking to see if the answer is right               
    while True :
        if Alge_answerbox.get()  == anwser_of_alge_levels[str(level)]:
                        #if true increment the level by 1
                           level =level + 1
                        #update the question because the level has changed
                           Alge_Question.configure(text = question_of_alge_levels[str(level)])
                        #update the displayed level because the level has changed
                           Alge_levellabel.configure(text = 'Level : '+ str(level))

                           Alge_answerbox.delete(0 , 'end')
button = ttk.Button(root ,text = 'click')
button.pack()
#binding the algebra window into this button
button.bind('<Button-1>' , algebra)
#constantly looping through our main window 
root.mainloop()

1 个答案:

答案 0 :(得分:1)

您需要在首次定义的任何地方声明global Alge_answerbox。正如您需要在将其分配给对象之前将其放在algebra函数中:

def algebra(a):

    global Alge_answerbox, Alge_Question, level
    #declaring the level as 1
    level = 1

    #our window(window)
    window = Tk()
    ...

同样,您也需要将Alge_Questionlevel声明为global