0

我试图reset从当前类外部调用该函数(这最终将在多个类上调用重置)但我得到的是似乎再次运行整个类的程序,而不仅仅是该类中的函数。

到底是怎么回事?

from tkinter import *

root = Tk()

class Scores1(Frame):
    def __init__(self, parent=None, **kw):
        Frame.__init__(self, parent, kw)
        self.team1 = 888
        self.team1str = StringVar()
        self.teamA()

    def teamA(self):
        self.team1str.set('%03d' % (self.team1))
        t1 = Label(textvariable=self.team1str, font=("Digiface", 120), fg='#fffd80000', bg='black')
        t1.grid()

    def ScoreA1(self):
        self.team1 = self.team1 + 1
        self.team1str.set('%03d' % (self.team1))

    def Reset(self):
        self.team1 = 0
        self.team1str.set('%03d' % (self.team1))

class Resetter(Frame):
    def __init__(self, parent=None, **kw):
        Frame.__init__(self, parent, kw)

    def reset(self):
        Scores1().Reset


def main():


    sc1 = Scores1(root)
    sc1.grid(row=0, column=0)
    res = Resetter(root)

    sc1.configure(highlightbackground='white', highlightthickness=2)
    root.configure(background = 'black')

    Button(root, text='Team A +1', command=sc1.ScoreA1).grid(row=1, column=0)
    Button(root, text='ResetInside', command=sc1.Reset).grid(row=2, column=0)
    Button(root, text='ResetOutside', command=res.reset).grid(row=3, column=0)

    root.mainloop()

if __name__ == '__main__':
    main()
4

1 回答 1

1

首先,您的代码中有一个错字:

Scores1().Reset

创建一个新的实例,Scores1然后返回方法Reset。要实际调用Reset,请附加().

下一个问题是Scores1()创建一个新实例。但是你想打电话Reset()sc1解决方案是传递sc1Resetter

class Resetter(Frame):
    def __init__(self, sc1, parent=None, **kw):
        Frame.__init__(self, parent, kw)
        self.sc1 = sc1
    ...

    def reset(self):
        self.sc1.Reset()

res = Resetter(sc1, root)
于 2013-10-17T08:36:19.990 回答