Here is my code:
import tkinter as tk
def abc():
print('get')
class Application(tk.Tk):
def __init__(self):
super(self.__class__, self).__init__()
self.geometry('300x350')
def bnt(self):
bnt1 = tk.Button(self, text='j', command='abc')
bnt1.place(x=0, y=0)
if __name__ == '__main__':
app = Application()
app.mainloop()
After i run the code:
I only can see the window without button:
How can i fix the problem?
CodePudding user response:
Your code does not execute self.bnt()
inside __init__()
, so the button is not created and that is why you get a blank window.
Also command='abc'
should be command=abc
instead.
Below is the updated code:
import tkinter as tk
def abc():
print('get')
class Application(tk.Tk):
def __init__(self):
super(self.__class__, self).__init__()
self.geometry('300x350')
self.bnt() # create the button
def bnt(self):
bnt1 = tk.Button(self, text='j', command=abc) # changed 'abc' to abc
bnt1.place(x=0, y=0)
if __name__ == '__main__':
app = Application()
app.mainloop()