Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么不';两个标签都更新了吗?_Python_Tkinter - Fatal编程技术网

Python 为什么不';两个标签都更新了吗?

Python 为什么不';两个标签都更新了吗?,python,tkinter,Python,Tkinter,定义一个调用要添加的两个计数器的函数 import Tkinter class Store1: def __init__(self): self.variable = Tkinter.IntVar() self.variable1 = Tkinter.IntVar() def add(self): counter1 = self.variable counter1.set(counter1.get() + 1)

定义一个调用要添加的两个计数器的函数

import Tkinter

class Store1:
    def __init__(self):
        self.variable = Tkinter.IntVar()
        self.variable1 = Tkinter.IntVar()

    def add(self):
        counter1 = self.variable
        counter1.set(counter1.get() + 1)
        counter2 = self.variable1
        counter2.set(counter2.get() + 1)
        return counter1.get(), counter2.get()

class Main(Tkinter.Tk):
    def __init__(self, *args, **kwargs):
        Tkinter.Tk.__init__(self, *args, **kwargs)
        counter1 = Store1()
        counter2 = Store1()


        self.label  = Tkinter.Label(self, textvariable=counter1.variable)
        self.button = Tkinter.Button(self, command=lambda:counter1.add(), text='+1')
        self.label.pack()
        self.button.pack()

        self.label1  = Tkinter.Label(self, textvariable=counter2.variable1)
        self.button1 = Tkinter.Button(self, command=lambda:counter2.add(), text='+1')
        self.label1.pack()
        self.button1.pack()




root = Main()
root.mainloop()

你的意思是,当你按下任何一个按钮时,两个变量都应该更新?这就像我所期望的那样。是的,我是python新手,两个变量都应该更新,但它们不会:(非常感谢您的帮助我将在下面为您提供一些代码。请稍等。请注意,最好将计数器设置为属性,这样您就不必嵌套函数。好的,谢谢,我在尝试修复时走对了方向。我也这么做了,但我将lambda保留在命令中。非常感谢。我走对了方向为了修复它,我创建了函数,但在命令中使用了lambda。非常感谢
import Tkinter

class Store1:
    def __init__(self):
        self.variable = Tkinter.IntVar()
        self.variable1 = Tkinter.IntVar()

    def add(self):
        counter1 = self.variable
        counter1.set(counter1.get() + 1)
        counter2 = self.variable1
        counter2.set(counter2.get() + 1)
        return counter1.get(), counter2.get()

class Main(Tkinter.Tk):
    def __init__(self, *args, **kwargs):
        Tkinter.Tk.__init__(self, *args, **kwargs)
        counter1 = Store1()
        counter2 = Store1()

        def both_counters():
            counter1.add()
            counter2.add()

        self.label  = Tkinter.Label(self, textvariable=counter1.variable)
        self.button = Tkinter.Button(self, command=both_counters, text='+1')
        self.label.pack()
        self.button.pack()

        self.label1  = Tkinter.Label(self, textvariable=counter2.variable1)
        self.button1 = Tkinter.Button(self, command=both_counters, text='+1')
        self.label1.pack()
        self.button1.pack()

root = Main()
root.mainloop()