tkinter中的属性错误,我无法使用链接到按钮的方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tkinter中的属性错误,我无法使用链接到按钮的方法相关的知识,希望对你有一定的参考价值。

我试图将按钮“calculate_button”链接到我创建的“calculate()”方法,该方法应该只是打印“你刚刚点击”,虽然提出了一个错误,说没有属性计算我真的不明白究竟应该在哪里写入方法计算以及如何正确地将按钮链接到方法

这是错误

AttributeError: '_tkinter.tkapp' object has no attribute 'calculate
line 2095, in __getattr__
    return getattr(self.tk, attr)
File "deathcalculatorapp.py", line 102, in <lambda>
command=lambda: controller.calculate())

这是代码

import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk
import datetime

LARGE_FONT = ("VERDANA", 12)


#user's information manager(classes and method)

class person:
    def __init__(self, name, birthday, sex, age, diet, smoker, physical_activity_frequency):
    self.name = name
    self.birthday = birthday
    self.sex = sex
    self.age = age
    self.diet = diet
    self.smoker = smoker
    self.physical_activity_frequency = physical_activity_frequency

    def age():
    today = datetime.today()
    person.age = person.birthday - today.year
    return person.age






#main windows

class deathCalculatorapp(tk.Tk):

def __init__(self, *args, **kwargs):
    tk.Tk.__init__(self, *args, **kwargs)

    tk.Tk.wm_title(self, "age calculator app")

    container = tk.Frame(self)
    container.pack(side="top", fill="both", expand=True)
    container.grid_rowconfigure(0, weight=1)
    container.grid_columnconfigure(0, weight=1)

    self.frames = {}
    for F in (StartPage, PageOne, PageTwo):
        page_name = F.__name__
        frame = F(parent=container, controller=self)
        self.frames[page_name] = frame

        # put all of the pages in the same location;
        # the one on the top of the stacking order
        # will be the one that is visible.
        frame.grid(row=0, column=0, sticky="nsew")

    self.show_frame("StartPage")

def show_frame(self, page_name):
    '''Show a frame for the given page name'''
    frame = self.frames[page_name]
    frame.tkraise()

def calculate():
        print("you've jsut clicked the button")


#all of the frames

class StartPage(tk.Frame):

def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    self.controller = controller


    label = ttk.Label(self, text="deathcalculatorapp", font=LARGE_FONT)
    label.grid(column=1, row=0)

    first_label = ttk.Label(self, text = "insert your data")
    year_label = tk.Label(self, text="year", bg ="lightblue", padx=9)
    month_label = tk.Label(self, text= "month", bg = "lightblue", padx=3)
    day_label = tk.Label(self, text ="day", bg= "lightblue", padx=11)

    first_label.grid(column=1, row=3)
    year_label.grid(column=0, row =4)
    month_label.grid(column=0, row =5)
    day_label.grid(column=0, row = 6)

    year_entry = tk.Entry(self,text = "", bg = "lightblue")
    month_entry = tk.Entry(self, text = "", bg= "lightblue")
    day_entry = tk.Entry(self, text= "", bg = "lightblue")

    year_entry.grid(column=1,row=4)
    month_entry.grid(column=1, row= 5)
    day_entry.grid(column=1, row=6)

    sex = tk.IntVar()

    female= ttk.Radiobutton(self, text="female",variable= sex, value="female")
    male=ttk.Radiobutton(self, text="male", variable= sex, value="male")
    female.grid(column=2, row=4)
    male.grid(column=2, row=5)

    calculate_button= ttk.Button(self, text="calculate your lifespawn",
                                 command= lambda: controller.calculate())
    calculate_button.grid(column=1, row=7)
    button1 = ttk.Button(self, text="Go to Page One",
                        command=lambda: controller.show_frame("PageOne"))
    button2 = ttk.Button(self, text="Go to Page Two",
                        command=lambda: controller.show_frame("PageTwo"))
    button1.grid(column=0, row=0)
    button2.grid(column=0, row=1)

    image = Image.open(r"/"
                       r"Users/tommasomasaracchio/Documents/pythonfolder/kushina4.jpg")
    image.thumbnail((500,300), Image.ANTIALIAS)
    photo = ImageTk.PhotoImage(image)

    Photo_label= ttk.Label(self, image=photo)
    Photo_label.image = photo
    Photo_label.grid(row= 2, column = 1)




class PageOne(tk.Frame):

def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    self.controller = controller
    label = ttk.Label(self, text="This is page 1", font=LARGE_FONT)
    label.grid(column=0, row=0)
    button = ttk.Button(self, text="Go to the start page",
                       command=lambda: controller.show_frame("StartPage"))
    button.grid(column=0, row=0)


class PageTwo(tk.Frame):

def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    self.controller = controller
    label = ttk.Label(self, text="This is page 2", font=LARGE_FONT)
    label.grid(column=0, row=0)
    button = ttk.Button(self, text="Go to the start page",
                       command=lambda: controller.show_frame("StartPage"))
    button.grid(column=0, row=0)






if __name__ == "__main__":
    app = deathCalculatorapp()
    app.mainloop()

非常感谢你的帮助,我很感激

答案

我已经修正了缩进并添加了self作为你的计算函数参数,它现在可以工作了。

import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk
import datetime

LARGE_FONT = ("VERDANA", 12)


#user's information manager(classes and method)

class person:
    def __init__(self, name, birthday, sex, age, diet, smoker, physical_activity_frequency):
        self.name = name
        self.birthday = birthday
        self.sex = sex
        self.age = age
        self.diet = diet
        self.smoker = smoker
        self.physical_activity_frequency = physical_activity_frequency

    def age():
        today = datetime.today()
        person.age = person.birthday - today.year
        return person.age

#main windows

class deathCalculatorapp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        tk.Tk.wm_title(self, "age calculator app")

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (StartPage, PageOne, PageTwo):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()

    def calculate(self):
            print("you've jsut clicked the button")


    #all of the frames

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller


        label = ttk.Label(self, text="deathcalculatorapp", font=LARGE_FONT)
        label.grid(column=1, row=0)

        first_label = ttk.Label(self, text = "insert your data")
        year_label = tk.Label(self, text="year", bg ="lightblue", padx=9)
        month_label = tk.Label(self, text= "month", bg = "lightblue", padx=3)
        day_label = tk.Label(self, text ="day", bg= "lightblue", padx=11)

        first_label.grid(column=1, row=3)
        year_label.grid(column=0, row =4)
        month_label.grid(column=0, row =5)
        day_label.grid(column=0, row = 6)

        year_entry = tk.Entry(self,text = "", bg = "lightblue")
        month_entry = tk.Entry(self, text = "", bg= "lightblue")
        day_entry = tk.Entry(self, text= "", bg = "lightblue")

        year_entry.grid(column=1,row=4)
        month_entry.grid(column=1, row= 5)
        day_entry.grid(column=1, row=6)

        sex = tk.IntVar()

        female= ttk.Radiobutton(self, text="female",variable= sex, value="female")
        male=ttk.Radiobutton(self, text="male", variable= sex, value="male")
        female.grid(column=2, row=4)
        male.grid(column=2, row=5)

        calculate_button= ttk.Button(self, text="calculate your lifespawn",
                                     command= lambda: controller.calculate())
        calculate_button.grid(column=1, row=7)
        button1 = ttk.Button(self, text="Go to Page One",
                            command=lambda: controller.show_frame("PageOne"))
        button2 = ttk.Button(self, text="Go to Page Two",
                            command=lambda: controller.show_frame("PageTwo"))
        button1.grid(column=0, row=0)
        button2.grid(column=0, row=1)

        image = Image.open(r"/"
                           r"Users/tommasomasaracchio/Documents/pythonfolder/kushina4.jpg")
        image.thumbnail((500,300), Image.ANTIALIAS)
        photo = ImageTk.PhotoImage(image)

        Photo_label= ttk.Label(self, image=photo)
        Photo_label.image = photo
        Photo_label.grid(row= 2, column = 1)



class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = ttk.Label(self, text="This is page 1", font=LARGE_FONT)
        label.grid(column=0, row=0)
        button = ttk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.grid(column=0, row=0)


class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = ttk.Label(self, text="This is page 2", font=LARGE_FONT)
        label.grid(column=0, row=0)
        button = ttk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.grid(column=0, row=0)


if __name__ == "__main__":
    app = deathCalculatorapp()
    app.mainloop()

以上是关于tkinter中的属性错误,我无法使用链接到按钮的方法的主要内容,如果未能解决你的问题,请参考以下文章

带进度条的Tkinter链接按钮

无法使用按钮和自定义数据更新 Tkinter matplotlib 图

Tkinter 错误:无法识别图像文件中的数据

在 Tkinter 中按下按钮后如何清除 Entry 小部件?

单击时如何将按钮值从自定义小部件传递到 tkinter 中的主应用程序

我如何使用python将终端输出到网格中的tkinter框架?