Reputation: 61
I am trying to create a program which prints something everytime i click a button, but it has to bee done using a class.
When i run my code, i get this error : NameError: name 'self' is not defined
(I dont want to put the test_button inside the class, because this is just a part of a much bigger program, and if i fix my problem in this way, then some other functions wont work.)
Any help is much appreciated!!
import tkinter as tk
from tkinter import *
window = tk.Tk()
window.geometry("500x400")
window.configure(background='grey')
class person():
def __init__(self):
pass
def test(self):
print('something')
#title label
label = tk.Label(window, text = "title",bg = '#42eff5',fg ='red',width = 35, height = 5).pack()
#button
test_button = Button(window,text='something',command = person.test(self),width= 11,height = 2,bg='blue',activebackground = 'blue',fg='white').place(x = 10,y = 30)
window.mainloop()
Upvotes: 0
Views: 801
Reputation: 386332
You need to create an instance of the person, and call the method on that person.
somebody = person()
test_button = Button(.., command=somebody.test, ...)
Upvotes: 1