버튼을 클릭 할 때마다 뭔가를 인쇄하는 프로그램을 만들려고하는데, 클래스를 사용해서해야합니다.
내 코드를 실행할 때 다음 오류가 발생합니다. NameError : name 'self'is not defined
(이것은 훨씬 더 큰 프로그램의 일부이기 때문에 test_button을 클래스 안에 넣고 싶지 않으며 이런 식으로 문제를 해결하면 다른 기능이 작동하지 않습니다.)
어떤 도움이라도 대단히 감사합니다!
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()
그 사람의 인스턴스를 만들고 그 사람에 대해 메서드를 호출해야합니다.
somebody = person()
test_button = Button(.., command=somebody.test, ...)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다