tieuanin 发表于 2020-10-6 09:49:18

tkinter 报错

import tkinter as tk

class App:
    def __init__(self, root):

      frame = tk.Frame(root)
      frame.pack()

      self.hi_there = tk.Button(frame, text='打招呼', bg='green', fg='red', command=self.say_hi)
      self.hi_there.pack(side=tk.TOP, padx=10, pady=10)

      def say_hi(self):
            print('我是小甲鱼!!!!')

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

root.mainloop()

这是我完全按书上抄下来的,然后系统就是报错
AttributeError: 'App' object has no attribute 'say_hi'

有大佬知道怎么处理吗{:10_269:}

疾风怪盗 发表于 2020-10-6 09:58:16

注意缩进

用pycharm之类的IDE,很容易就看出问题了

import tkinter as tk

class App:
    def __init__(self, root):

      frame = tk.Frame(root)
      frame.pack()

      self.hi_there = tk.Button(frame, text='打招呼', bg='green', fg='red', command=self.say_hi)
      self.hi_there.pack(side=tk.TOP, padx=10, pady=10)

    def say_hi(self):
      print('我是小甲鱼!!!!')

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

root.mainloop()

tieuanin 发表于 2020-10-6 10:06:49

疾风怪盗 发表于 2020-10-6 09:58
注意缩进

用pycharm之类的IDE,很容易就看出问题了

我刚刚自己找出来
钻了牛角就是出不来{:5_99:}

happy含笑 发表于 2020-10-6 13:26:21

页: [1]
查看完整版本: tkinter 报错