[英]Why is there a problem when i try to run this tkinter python code
i was making a gui thing when i came across this problem当我遇到这个问题时,我正在做一个 gui 的东西
type object 'app' has no attribute 'tk'类型 object 'app' 没有属性 'tk'
and this is my code这是我的代码
if you can solve this full code it will be very helpful如果你能解决这个完整的代码,那将非常有帮助
class app(Frame):
def __init__ (self,master=None):
Frame.__init__(self,master)
self.master=master
self.window()
def window(self):
self.master.title("TuTu")
self.master.geometry("500x350")
self.pack(fill=BOTH,expand=1)
pic = Image.open("image.jpg")#(for image in main(app) page)
picture = ImageTk.PhotoImage(pic)
image =tk.Button(app, width=200, height=200, image=picture)
image.place(relx=0.5, rely=0.35, anchor=CENTER)
Obviously Python's main focus is on indentation .显然 Python 的主要关注点是缩进。 You are trying to add two methods __init__()
and window()
to class app()
.您正在尝试将两个方法__init__()
和window()
添加到 class app()
。 But without a proper indentation, these two methods would not be added anywhere.但是如果没有适当的缩进,这两种方法就不会添加到任何地方。
A fix would be adjusting the tabs/spaces for your two methods, like this:解决方法是为您的两种方法调整制表符/空格,如下所示:
class app(Frame):
def __init__ (self,master=None):
Frame.__init__(self,master)
self.master=master
self.window()
def window(self):
self.master.title("TuTu")
self.master.geometry("500x350")
self.pack(fill=BOTH,expand=1)
pic = Image.open("image.jpg")#(for image in main(app) page)
picture = ImageTk.PhotoImage(pic)
image =tk.Button(app, width=200, height=200, image=picture)
image.place(relx=0.5, rely=0.35, anchor=CENTER)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.