I would like to make a calculator in python with interface that has sine, cosine, power and root

Asked

Viewed 21 times

-1

import Math import wx

class Calcframe(wx. Frame):

def __init__(self):

    no_resize = wx.DEFAULT_FRAME_STYLE & ~ (wx.RESIZE_BORDER | 
                                            wx.MAXIMIZE_BOX)
    
   
    super().__init__(
        None, title="Calculadora simples",
        size=(300, 500), style=no_resize)
    panel = CalcPanel(self)
    
    self.Show()

class Calcpanel(wx. Panel):

def __init__(self, parent):
    super().__init__(parent)
   
    self.last_button_pressed = None
   
    self.whitelist = ['0', '1', '2', '3', '4',
                      '5', '6', '7', '8', '9',
                      '-', '+', '/', '*', '.',
                      'sen','cos','pot','√']
   
    self.on_key_called = False
    self.empty = True
    
    self.create_ui()
    

def create_ui(self):
   
    main_sizer = wx.BoxSizer(wx.VERTICAL)
    
    font = wx.Font(12, wx.MODERN, wx.NORMAL, wx.NORMAL)

    self.solution = wx.TextCtrl(self, style=wx.TE_RIGHT)
   
    self.solution.SetFont(font)
    
    self.solution.Bind(wx.EVT_TEXT, self.on_key)
    

    main_sizer.Add(self.solution, 0, wx.EXPAND|wx.ALL, 5)
   
    self.running_total = wx.StaticText(self)
    
    main_sizer.Add(self.running_total, 0, wx.ALIGN_RIGHT)
    
    buttons = [['7', '8', '9', '/'],
               ['4', '5', '6', '*'],
               ['1', '2', '3', '-'],
               ['.', '0', '', '+'],
               ['sen','cos','pot','√']]
    #
    for label_list in buttons:

        btn_sizer = wx.BoxSizer()
        for label in label_list:
            
            button = wx.Button(self, label=label)
            
            btn_sizer.Add(button, 1, wx.ALIGN_CENTER, 0)
           
            button.Bind(wx.EVT_BUTTON, self.update_equation)
        main_sizer.Add(btn_sizer, 1, wx.ALIGN_CENTER)
    
  
    equals_btn = wx.Button(self, label='=')

    equals_btn.Bind(wx.EVT_BUTTON, self.on_total)

    main_sizer.Add(equals_btn, 0, wx.EXPAND|wx.ALL, 3)

    clear_btn = wx.Button(self, label='Clear')
    clear_btn.Bind(wx.EVT_BUTTON, self.on_clear)
    main_sizer.Add(clear_btn, 0, wx.EXPAND|wx.ALL, 3)

    self.SetSizer(main_sizer)

def update_equation(self, text):

    operators = ['/', '*', '-', '+']

    if not isinstance(text,str):
        btn = text.GetEventObject()
        text = btn.GetLabel()

    current_equation = self.solution.GetValue()

    if text not in operators:

        if self.last_button_pressed in operators:
            self.solution.SetValue(current_equation + ' ' + text)
        elif self.empty and current_equation:

            self.empty = False

        else:
            self.solution.SetValue(current_equation + text)

    elif text in operators and current_equation != '' \
        and self.last_button_pressed not in operators:
            
        self.solution.SetValue(current_equation + ' ' + text)



    self.last_button_pressed = text
    self.solution.SetInsertionPoint(-1)


    for item in operators:

        if item in self.solution.GetValue():
  
            self.update_solution()
            break
def cos(self):
    self.result = False
    self.current = math.cos(math.radians(float(GetValue())))

def update_solution(self):
    
 
    
    try:
       
        current_solution = str(eval(self.solution.GetValue()))
    
        self.running_total.SetLabel(current_solution)
   
        self.Layout()
     
        return current_solution
    except ZeroDivisionError:
        self.solution.SetValue('ZeroDivisionError')
    except:
        pass
    
def on_total(self, event):

    solution = self.update_solution()
    if solution:

        self.solution.SetValue(solution)
        self.running_total.SetLabel('')
        

def on_clear(self, event):

    self.solution.Clear()

    self.running_total.SetLabel('')

    self.empty = True

    self.solution.SetFocus()
    

def on_key(self, event):

    if self.on_key_called:
        self.on_key_called = False
        return
  
    key = event.GetString()

    self.on_key_called = True

    if key in self.whitelist:
        self.update_equation(key)

if name == 'main': app = wx. App(False) frame = Calcframe() mainloop app.()

cosine does not recognize the value

  • Hello. Please do the [tour]. To be useful to other users the question code must be reduced to a minimum capable of reproducing the problem (this is what we call [mcve]) and the description/explanation of the error must be detailed enough to help us understand what is happening. As it stands the question will be closed but can be reopened upon the requested adjustments.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.