python calculator [closed]

2024/11/7 13:57:18

I found this calculator code in the web.

I want to add some function, like the pow() but it returns an error.

The error:

button(powF, LEFT, 'pow', lambda w=display: w.set(w.pow()))

AttributeError: StringVar instance has no attribute 'pow'


from Tkinter import * 
import mathdef frame(root, side):w = Frame(root)w.pack(side=side, expand=YES, fill=BOTH) return wdef button(root, side, text, command=None):w = Button(root, text=text, command=command)w.pack(side=side, expand=YES, fill=BOTH)return wclass Calculator(Frame):def __init__(self):Frame.__init__(self)self.option_add('*Font', 'Verdana 20 bold') self.pack(expand=YES, fill=BOTH)  self.master.title('tk') self.master.iconname("calcu1ator") display = StringVar()Entry(self,relief=SUNKEN,textvariable=display).pack(side=TOP,expand=YES,fill=BOTH)for key in ("123", "456", "789", "-0."):keyF = frame(self, TOP)for char in key:button(keyF, LEFT, char,lambda w=display, c=char: w.set(w.get() + c))opsF = frame(self, TOP)for char in "+-*/=":if char == '=':btn = button(opsF, LEFT, char)btn.bind('<ButtonRelease-1>',lambda e, s=self, w=display: s.calc(w), '+')else:btn = button(opsF, LEFT, char,lambda w=display, s=' %s '%char:w.set(w.get()+s))clearF = frame(self, BOTTOM)button(clearF, LEFT, 'Clr', lambda w=display: w.set(''))#powF = frame(self, BOTTOM)#button(powF, LEFT, 'pow', lambda w=display: pow(w,2))def calc(self, display):try:display.set(eval(display.get()))except:display.set("ERROR")if __name__ == '__main__':Calculator().mainloop() 
Answer

you can't give the display as parameter to the pow function, it expects a number. and don't forget to set the display.

   powF = frame(self, BOTTOM)button(powF, LEFT, 'pow', lambda w=display: w.set(pow(float(w.get()),2)))
https://en.xdnf.cn/q/120736.html

Related Q&A

How to sequence row based on another row?

I am trying to convert a formula from excel to pandas.The DataFrame looks like this: Column A Column B H H H J J J J K K I want to fill column B to increment while the value in co…

Multiclassification task using keras [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 3…

Clarification needed regarding immutability of strings in Python [closed]

Closed. This question is seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. It does not meet Stack Overflow guidelines. It is not currently accepting …

Please help me in solving Fractional Knapsack problem (Maximum Value of the Loot)

Maximum Value of the LootProblem Introduction: A thief finds much more loot than his bag can fit. Help him to find the most valuable combination of items assuming that any fraction of a loot item can b…

Python countdown clock with GUI [duplicate]

This question already has an answer here:Making a countdown timer with Python and Tkinter?(1 answer)Closed 8 years ago.Im having problems with a countdown clock that I was making in Python for a Raspb…

Confidence calculation in association rule [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, argum…

How to write the names that start with A - L to one file and the rest to another?

Hello my assignment is :Create a system that allows the user to enter their name, title, surname, Dob, email and phone number. Once details are submitted, they should be written to a file. Surnames tha…

How is it possible to use a while loop to print even numbers 2 through 100?

I am a beginner and I am stuck on this problem, "Write a python code that uses a while loop to print even numbers from 2 through 100. Hint ConsecutiveEven differ by 2."Here is what I came up …

Issue with buttons not functioning after start of program

I am new and learning python 3.6 and Ive almost completed my first code project. After doing an exhaustive search to resolve my problem I have not been able to find the answer to what I am sure is a si…

explanation of C implementation pythons len function [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…