李伦奥 发表于 2022-2-15 22:25:48

求解! 为什么这段代码在我这一直报错啊?(新手)

import tkinter as tk
class App:
    def __init__(self,master):
      frame = tk.Frame(master)
      frame.pack()
      self.hi_there = tk.Button(frame,text="打招呼",bg='black',fg="blue",command=self.say_hi)
      self.hi_there.pack()

def say_hi(self):
    print("gcygucgcgw")





root = tk.Tk()
app = App(root)
root.mainloop()






报错信息:
Traceback (most recent call last):
File "Desktop\tk2(报错).py", line 17, in <module>
    app = App(root)
File "Desktop\tk2.py", line 6, in __init__
    self.hi_there = tk.Button(frame,text="打招呼",bg='black',fg="blue",command=self.say_hi)
AttributeError: 'App' object has no attribute 'say_hi'

isdkz 发表于 2022-2-15 22:29:08


import tkinter as tk
class App:
    def __init__(self,master):
      frame = tk.Frame(master)
      frame.pack()
      self.hi_there = tk.Button(frame,text="打招呼",bg='black',fg="blue",command=self.say_hi)
      self.hi_there.pack()

    def say_hi(self):                        # 这里要缩进,say_hi得在类里面
      print("gcygucgcgw")





root = tk.Tk()
app = App(root)
root.mainloop()
页: [1]
查看完整版本: 求解! 为什么这段代码在我这一直报错啊?(新手)