0

Hello.
I have a Class in my python file. That Class contains some functions.
How can i set the command of a Tkinter butten so that call a function of that class?

I tried this but it didn't work:

class calculating(object):
    def __inint__(self):
        self.current = 0
    def addition(self, amount):
        self.current += amount
    def result(self):
        return self.current
        print (self.current)
    def show(self):
        lbl["text"] = result()


btn1 = Button(root, text = "add", command = calculating.addition)
btn1.pack()

btn2 = Button(root, text = "result", command = calculating.show)
btn2.pack()

lbl = Label(root)
lbl.pack
3
Contributors
2
Replies
19
Views
2 Years
Discussion Span
Last Post by HiHe
3

I see that your __init__ method is misspelled, but you're gonna want an instance to work with. You have a calculating class, but no object. Instantiate the class:

mycalc = calculating()

...then use the method (mycalc.addition). Also, its good practice in python for classes to start with a capital letter. So calculating should be called Calculating instead.

Edited by chriswelborn: explanation

0

There are a lot of mistakes in that code!
Maybe you should study up on Python class and argument passing in Tkinter Button command.

Edited by HiHe

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.