为什么按下按钮时op整数不会改变?

时间:2016-07-04 12:17:00

标签: python math tkinter

我是python的新手,所以这段代码很乱,但我发现了一个我无法解决的错误。该程序旨在为您提供数学总和来解决,我使用的按钮将int作为1到4的值发送,然后在guess函数中测试以生成下一个问题。问题是当按下按钮时int不会改变,并且每个和是一个加法,因为它在根中设置为1。如果有人能提供帮助,那就更好了,谢谢!

from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from random import randint

def plus(*args):
    op = 1

def minus(*args):
    op = 2

def times(*args):
    op = 3

def divide(*args):
    op = 4

def  guess(*args):
    try:
        numberguess = int(ans.get())
    except ValueError:
        ask.set('That is not a number!')

    global number
    gdif = number - numberguess

    if gdif == 0:
        ask.set('You got it right!')
        global score
        sco = score.get() + 1
        score.set(sco)
        result = messagebox.askquestion('', 'Next question')
        if result == 'yes':
            if top.get() == 0:
                noone = randint(1,10)
                notwo = randint (1,10)
            else:
                noone = randint(1,top.get())
                notwo = randint(1,top.get())
            ans_entry.focus()
            if op == 1:
                number = noone + notwo
                numb.set('What is ' + str(noone) + ' + '+ str(notwo) + '?')
            elif op == 2:
                number = noone - notwo
                numb.set('What is ' + str(noone) + ' - '+ str(notwo) + '?')
            elif op == 3:
                number = noone * notwo
                numb.set('What is ' + str(noone) + ' x '+ str(notwo) + '?')
            elif op == 4:
                number = noone / notwo
                numb.set('What is ' + str(noone) + ' / '+ str(notwo) + '?')
        elif result == 'no':
            root.destroy()
    elif gdif>0:
        ask.set(ans.get() + ' is too low')
    elif gdif<0:
        ask.set(ans.get() + ' is too high')
    ans.set('')

root = Tk()
root.title('Maths Game') #window title
mainframe = ttk.Frame(root, padding = '3 3 12 12')
mainframe.grid(column = 0, row = 0, sticky = (N, W, E, S))
mainframe.columnconfigure(0,weight = 1)
mainframe.rowconfigure(0, weight = 1)
#organises grid well

ans = StringVar()
numb = StringVar()
ask = StringVar()
pts = StringVar()
score = IntVar()
top = IntVar()
#sets as variables

ans_entry = ttk.Entry(mainframe, width = 7, textvariable = ans) #defines          guess entry
ans_entry.grid(column = 2, row = 1, sticky = (W, E)) #places guess entry on grid
ttk.Label(mainframe, textvariable = numb).grid(column = 2, row = 2, sticky = (W, E)) #label
ttk.Label(mainframe, textvariable = ask).grid(column = 3, row = 1, sticky = (W, E)) #label
ttk.Label(mainframe, textvariable = score).grid(column = 3, row = 2, sticky = (E)) #label
ttk.Label(mainframe, textvariable = pts).grid(column = 4, row = 2, sticky = (W, E))
ttk.Button(mainframe, text = 'Answer', command = guess).grid(column = 3, row = 3, sticky = (W, E)) #guess button
top_entry = ttk.Entry(mainframe, width = 3, textvariable = top)
top_entry.grid(column = 3, row = 4, sticky = (E))
ttk.Button(mainframe, text = '+', command = plus).grid(column = 1, row = 5, sticky = (W, E))
ttk.Button(mainframe, text = '-', command = minus).grid(column = 2, row = 5, sticky = (W, E))
ttk.Button(mainframe, text = 'x', command = times).grid(column = 3, row = 5, sticky = (W, E))
ttk.Button(mainframe, text = '/', command = divide).grid(column = 4, row = 5, sticky = (W, E))

for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)                       #pads grid nicely
ans_entry.focus() #cursor starts in box
root.bind('<Return>', guess) #binds entry key to guess button

noone = randint(1,10)
notwo = randint (1,10)
number = noone + notwo
numb.set('What is ' + str(noone) + ' + '+ str(notwo) + '?')
right = False
sco = 0
score.set(sco)
pts.set(' points')
one = 10
op = 1

root.mainloop() #launches main code

1 个答案:

答案 0 :(得分:1)

因为op是每个函数中的局部变量。

您应该添加global op

def plus(*args):
    global op
    op = 1

.
.
.

但请注意,这是一种非常不理想的做法。 拥有一个类更有意义,然后将这些函数作为其方法。这样,他们都可以访问op变量,而不必是全局变量。