0

So basically i am going to write a program of python tkinter to show weight, however the variables and the objects are distracting me.

My code is like this:

from tkinter import *
import tkinter as tk

#object oriented BMI project
class Root(Tk):
    def __init__(self): 
        super(Root, self).__init__()


        #Info Bar
        self.title("Shushu BMI Calculator")
        self.geometry("1100x600")
        #self.resizable("False")
    
        #var
        self.weight = tk.StringVar()
        #self.w = tk.StringVar()

        #Caption
        self.caption = tk.Label(text = "BMI Calculator - Beta 2.0",
                             fg = "brown",
                             font="Arial 40")
        self.caption.grid(column = 0, row = 0, sticky = "N")

        #Copyright
        self.copyright = tk.Label(text = "Powered by Shushu Studio",
                                   fg = "green",
                                   font = "Arial 20")
        self.copyright.grid(column = 1, row = 0, sticky = "E")
    
        #Weight input Row
        self.weightInputTag = tk.Label(text="Please input your weight here (kg)")
        self.weightInputTag.grid(column = 0, row = 1, sticky = "W")

        self.weightEntry = tk.Entry(textvariable = self.weight)
        self.weightEntry.grid(column = 0, row = 1)
    
        self.weightSubmit = tk.Button(text="Submit",
                                     COMMAND=self.weightget)
        self.weightSubmit.grid(column = 0, row = 2)

        self.showWeight = tk.Label(text="")
        self.showWeight.grid(column = 0, row = 3)

        def weightget(self):
            weight = self.weightEntry.get()
            self.showWeight.configure(text=weight)


root = Root()
root.mainloop()

And the console show this:

Resetting Python state.
Running P:\2020\09\25\bmi.py
The interactive Python process has exited.
Traceback (most recent call last):
  File "P:\2020\09\25\bmi.py", line 52, in <module>
    root = Root()
  File "P:\2020\09\25\bmi.py", line 41, in __init__
    COMMAND=self.weightget)
  File "P:\Language\lib\tkinter\__init__.py", line 2345, in __getattr__
    return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'weightget'

Please help, thanks very much!

2 Answers 2

1

The indention of your function was wrong.

def weightget(self):
    weight = self.weightEntry.get()
    self.showWeight.configure(text=weight)

If you want this function working as class method and not as a normal function in your local namespace of your class. Which I assume, since you put the parameter self in the function, then you need to make sure its on the right spot by the indentation.

import tkinter as tk
class Root(tk.Tk):
    def __init__(self): 
        super(Root, self).__init__()


        #Info Bar
        self.title("Shushu BMI Calculator")
        self.geometry("1100x600")
        #self.resizable("False")
    
        #var
        self.weight = tk.StringVar()
        #self.w = tk.StringVar()

        #Caption
        self.caption = tk.Label(text = "BMI Calculator - Beta 2.0",
                             fg = "brown",
                             font="Arial 40")
        self.caption.grid(column = 0, row = 0, sticky = "N")

        #Copyright
        self.copyright = tk.Label(text = "Powered by Shushu Studio",
                                   fg = "green",
                                   font = "Arial 20")
        self.copyright.grid(column = 1, row = 0, sticky = "E")
    
        #Weight input Row
        self.weightInputTag = tk.Label(text="Please input your weight here (kg)")
        self.weightInputTag.grid(column = 0, row = 1, sticky = "W")

        self.weightEntry = tk.Entry(textvariable = self.weight)
        self.weightEntry.grid(column = 0, row = 1)
    
        self.weightSubmit = tk.Button(self,text="Submit",
                                      command=self.weightget)
        self.weightSubmit.grid(column = 0, row = 2)

        self.showWeight = tk.Label(text="")
        self.showWeight.grid(column = 0, row = 3)

    def weightget(self):
        weight = self.weightEntry.get()
        self.showWeight.configure(text=weight)


root = Root()
root.mainloop()
Sign up to request clarification or add additional context in comments.

1 Comment

Also the command argument in the OP code was COMMAND you changed this to command maybe keep a reference to that change, so OP dont get confused. Wouldnt be a problem if the OP copy-pastes your code, but ya
0
from tkinter import *
import tkinter as tk

#object oriented BMI project
class Root(Tk):
    def __init__(self): 
        super(Root, self).__init__()

        def weightget(self):
            weight = self.weightEntry.get()
            self.showWeight.configure(text=weight)

        #Info Bar
        self.title("Shushu BMI Calculator")
        self.geometry("1100x600")
        #self.resizable("False")
    
        #var
        self.weight = tk.StringVar()
        #self.w = tk.StringVar()

        #Caption
        self.caption = tk.Label(text = "BMI Calculator - Beta 2.0",
                             fg = "brown",
                             font="Arial 40")
        self.caption.grid(column = 0, row = 0, sticky = "N")

        #Copyright
        self.copyright = tk.Label(text = "Powered by Shushu Studio",
                                   fg = "green",
                                   font = "Arial 20")
        self.copyright.grid(column = 1, row = 0, sticky = "E")
    
        #Weight input Row
        self.weightInputTag = tk.Label(text="Please input your weight here (kg)")
        self.weightInputTag.grid(column = 0, row = 1, sticky = "W")

        self.weightEntry = tk.Entry(textvariable = self.weight)
        self.weightEntry.grid(column = 0, row = 1)
    
        self.weightSubmit = tk.Button(text="Submit", command=lambda:weightget(self))
        self.weightSubmit.grid(column = 0, row = 2)

        self.showWeight = tk.Label(text="")
        self.showWeight.grid(column = 0, row = 3)

        


root = Root()
root.mainloop()

This works properly. Define the function before the process.

self.weightSubmit = tk.Button(text="Submit", command=lambda:weightget(self))

Give the command for the button like this as you are using object oriented programming. Make sure to use lambda

Read this https://www.w3schools.com/python/python_lambda.asp and you will get a better understanding about lambda

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.