简体   繁体   English

为什么当我尝试运行此 tkinter python 代码时会出现问题

[英]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.

相关问题 当我尝试在 Python 中运行 matplotlib 命令时遇到问题 - I have a problem when I try run a matplotlib command in Python 我尝试在bash中运行python命令时遇到问题 - I have a problem when I try to run python command in bash 超出时间限制错误 - 当我尝试运行 Python 3 代码时 - Time limit exceeded Error - When I try to run Python 3 code Python try except 块在我不希望出现问题时运行异常代码,并且在我希望它运行异常代码时只给出错误 - Python try except block runs the except code when I don't expect a problem and just gives an error when I expect it to run the except code 为什么多线程时我的python代码不起作用-Tkinter - Why is my python code not working ONLY when I multithread - Tkinter github和Python 3.8有问题吗? 为什么我不能运行这段代码? - Is there a problem with github and Python 3.8? Why can't I run this code? 每当我运行我的程序时,以下 python tkinter 代码中的字体大小问题不会增加 - problem with font size in following python tkinter code whenever i run my program font size do not increase 尝试运行 python 代码时出现 TypeError - Getting TypeError when try to run the python code 为什么每次尝试运行 VS Code python 终端时都会出现语法错误? - Why am I getting a syntax error whenever I try to run VS code python terminal? Python 当我尝试将 VSCode 中的项目作为代码运行时,它们以“code=1”退出 - Python Projects In VSCode Exit With "code=1" When I Try to Run Them as Code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM