Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
'm struggling with a bit of code for my final Intro to programming in Python homework. The goal is to build a small GUI that calculates the hypotenuse of a right triangle after the user enters the length of 2 sides and hits calculate. I've written a lot of the code but i'm struggling in 2 ways. 1. The "Side C" box shouldn't be a box you can enter stuff into, instead it should be blank until the answer comes up in it, but i'm not sure how to do that. 2. I keep getting an error:
> AttributeError: 'Pythagorean' object has no attribute 'ans'

import tkinter
import math
class Pythagorean:
    def __init__(self):
        # Create the main window.
        self.main_window = tkinter.Tk()

        # Create the five frames.
        self.SideA_frame = tkinter.Frame(self.main_window)
        self.SideB_frame = tkinter.Frame(self.main_window)
        self.SideC_frame = tkinter.Frame(self.main_window)
        self.ans_frame = tkinter.Frame(self.main_window)
        self.button_frame = tkinter.Frame(self.main_window)

        
        self.SideA_label = tkinter.Label(self.SideA_frame,
                                         text='   Side A:')
        self.SideA_entry = tkinter.Entry(self.SideA_frame,
                                         width=30)
        self.SideA_label.pack(side='left')
        self.SideA_entry.pack(side='left')

        
        self.SideB_label = tkinter.Label(self.SideB_frame,
                                         text='   Side B:')
        self.SideB_entry = tkinter.Entry(self.SideB_frame,
                                         width=30)
        self.SideB_label.pack(side='left')
        self.SideB_entry.pack(side='left')
        
        
        self.SideC_label = tkinter.Label(self.SideC_frame,
                                         
                                         text='   Side C:')
        self.SideC_entry = tkinter.Entry(self.SideC_frame,
                                         width=30)
        self.SideC_label.pack(side='left')
        self.SideC_entry.pack(side='left')


        
        self.calc_button = tkinter.Button(self.button_frame,
                                          text='Calculate',
                                          command=self.calc_ans)
        self.quit_button = tkinter.Button(self.button_frame,
                                          text='Exit',
                                          command=self.main_window.destroy)
        self.calc_button.pack(side='right')
        self.quit_button.pack(side='right')

        
        self.SideA_frame.pack()
        self.SideB_frame.pack()
        self.SideC_frame.pack()
        self.ans_frame.pack()
        self.button_frame.pack()

        
        tkinter.mainloop()
    
    def calc_ans(self):
        
        self.SideA = float(self.SideA_entry.get())
        self.SideB = float(self.SideB_entry.get())
        
        self.Pyth =  math.sqrt(self.SideA**2 + self.SideB**2)       
        self.ans.set(self.Pyth)
        
Pyth_The = Pythagorean()


What I have tried:

I've tried modifying various parts of the code, looking up guides on Tkinter and classes, but to no avail i still cant figure out these issues. I feel really lost on this project.
Posted
Updated 30-Jul-19 17:56pm
v2

For your first problem, you could make the field read only:
self.SideC_entry.configure(state='readonly')
 
Share this answer
 
Comments
Member 13796964 25-Apr-18 3:39am    
Thank you very much for your help! Now just to keep playing around till i crack the AttributeError
Python
self.ans.set(self.Pyth)

You are trying to call the set method on an object that has never been declared.
 
Share this answer
 
Comments
Member 13796964 25-Apr-18 3:53am    
Thank you, i'm still new to Object Oriented programming, and adding in Tkinter hasn't been helping. My prof didn't really go into Tkinter much, so I've been very lost.
Richard MacCutchan 25-Apr-18 4:16am    
That is nothing to do with Tkinter. You cannot call a method on an object that does not exist. self.ans is not a class object so does not have a set method.
For side c this needs to be added
self.SideC = tkinter.StringVar() To update the side C value

Along with textvariale in Entry
self.SideC_entry = tkinter.Entry(self.SideC_frame,
                                       width=30, textvariable = self.SideC)

Then at the calc_ans part

self.Pyth =  math.sqrt(self.SideA**2 + self.SideB**2)
        
self.SideC.set(self.Pyth)
This is the object that was created and set can be called on it.

With these changes the program should run fine.

P.S:-To make it read only
self.SideC_entry.configure(state='readonly')
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900