全球问题(python)

全球问题(python),python,tkinter,Python,Tkinter,我有密码: from Tkinter import * admin = Tk() a = 1 def up(): global a a += 1 def upp(): up() print a print 'its ',a buttton = Button(admin, text='up', command=upp) buttton.pack() mainloop() 我想让“its”在我每次按下按钮时都上升。重放代码,这样每次它都会上升一个…帮助我测试了这

我有密码:

from Tkinter import *
admin = Tk()
a = 1

def up():
    global a
    a += 1

def upp():
    up()
    print a
print 'its ',a
buttton = Button(admin, text='up', command=upp)
buttton.pack()
mainloop()
我想让“its”在我每次按下按钮时都上升。重放代码,这样每次它都会上升一个…帮助我测试了这个:

from Tkinter import *
import itertools

admin = Tk()
a = itertools.count(1).next


def upp():
    print a()

buttton = Button(admin, text='up', command=upp)
buttton.pack()
mainloop()
这将在值为1时开始,每次打印时将再添加一个。因此,当您第一次按下它时,它将在标准输出中显示1。

我测试了这个:

from Tkinter import *
import itertools

admin = Tk()
a = itertools.count(1).next


def upp():
    print a()

buttton = Button(admin, text='up', command=upp)
buttton.pack()
mainloop()
这将在值为1时开始,每次打印时将再添加一个。因此,您第一次按下它时,它将显示1 in standard out。

Replace

def upp():
    up()
    print a
print 'its ',a
buttton = Button(admin, text='up', command=upp)
buttton.pack()
mainloop()

它可以按照你的意愿工作

更新:请注意,您不需要两个函数。简化版:

from Tkinter import *
admin = Tk()
a = 0

def upp():
    global a
    a += 1
    print 'its ', a

buttton = Button(admin, text='up', command=upp)
buttton.pack()
mainloop()
无论如何,应避免使用全局变量(参见Alan answer以获得更好的解决方案)

替换

def upp():
    up()
    print a
print 'its ',a
buttton = Button(admin, text='up', command=upp)
buttton.pack()
mainloop()

它可以按照你的意愿工作

更新:请注意,您不需要两个函数。简化版:

from Tkinter import *
admin = Tk()
a = 0

def upp():
    global a
    a += 1
    print 'its ', a

buttton = Button(admin, text='up', command=upp)
buttton.pack()
mainloop()

无论如何,应该避免使用全局变量(请参见Alan answer以获得更好的解决方案)

测试并编辑我的样本。除非我误解了,否则它似乎按照他的要求工作。你就我的解决方案向我提出质疑是对的。我没有测试这个想法来验证它的正确性。我对python还比较陌生,所以我很感激有这种健全性检查。它没有按照我想要的方式工作。我希望它在函数外打印+1,然后重播所有代码。测试并编辑了我的示例。除非我误解了,否则它似乎按照他的要求工作。你就我的解决方案向我提出质疑是对的。我没有测试这个想法来验证它的正确性。我对python还比较陌生,所以我很感激有这种健全性检查。它没有按照我想要的方式工作。我希望它在函数外打印+1,然后重播所有代码。