Как сделать кнопку процент на калькулятор?

IT Денис

Новичок
Пользователь
Май 5, 2020
18
1
3
Вот не могу сам сделать подумал обратиться
вот сам код
Python:
from tkinter import *
from tkinter import messagebox
from tkinter import ttk

root=Tk()
root.title('КАЛЬКУЛЯТОР ОТ АМИРА')


def calc(key):
    global memory
    if key == '=':
        strl= '-+0123456789/*'
        
        if calc_entry.get()[0] not in strl:
            calc_entry.insert(END, 'First number is not number!')
            messagebox.showerror('Error!', 'Its not number')
            
        try:
            result=eval(calc_entry.get())
            calc_entry.insert(END, '=' + str(result))
        except:
            calc_entry.insert(END, 'ERROR')
            messagebox.showerror('ERROR', 'CHECK AGAIN')
    elif key == 'C':
        calc_entry.delete(0, END)
    elif key == 'A':
        calc_entry.insert(END, 5)
        
  
        
        
        
    else:
            if '=' in calc_entry.get():
                calc_entry.delete(0, END)
            calc_entry.insert(END, key)
            
bttn_pack=[
    '7','8','9','+','-',
    '4','A','6','*','/',
    '1','2','3','=',
    '0','C','.','%']
r=1
c=0

for i in bttn_pack:
    rel= ''
    cmd=lambda x=i: calc(x)
    
        


    if i == 'A':
        Button(root, text=i, command=cmd, bg='red', width=9).grid(row=r, column=c)
    else:
        ttk.Button(root, text=i, command=cmd).grid (row=r, column=c)
        
    
        
    
  
    c += 1
    if c>4:
          c=0
          r += 1
 
calc_entry= Entry(root, width=33, bg='white', fg='blue' )
calc_entry.grid(row=0, column=0 , columnspan=5)

    
root.mainloop()
 

stud_55

Модератор
Команда форума
Модератор
Апр 3, 2020
1 522
672
113
Вот пример:
Python:
from tkinter import *
from tkinter import messagebox
from tkinter import ttk

root=Tk()
root.title('КАЛЬКУЛЯТОР ОТ АМИРА')


def calc(key):
    global memory
    if key == '=':
        strl= '-+0123456789/*'
        
        if calc_entry.get()[0] not in strl:
            calc_entry.insert(END, 'First number is not number!')
            messagebox.showerror('Error!', 'Its not number')
            
        try:
            result=eval(calc_entry.get())
            calc_entry.insert(END, '=' + str(result))
        except:
            calc_entry.insert(END, 'ERROR')
            messagebox.showerror('ERROR', 'CHECK AGAIN')
    elif key == 'C':
        calc_entry.delete(0, END)
    elif key == 'A':
        calc_entry.insert(END, 5)
        
    # 55 + 10% = 60.5
    elif key == '%':
        s = ''
        last_op = None
        last_num = None
        for i in calc_entry.get()[::-1]:
            if i.isdigit():
                s += i
            else:
                last_op = i
                break
        if s != '':
            last_num = int(s[::-1])
        if last_op and last_num:
            result = eval(calc_entry.get()[:-len(str(last_num))][:-1])
            result = eval(str(result) + last_op + str(result * last_num * 0.01))
            calc_entry.delete(0, END)
            calc_entry.insert(0, str(result))
        else:
            calc_entry.delete(0, END)
        
    else:
        if '=' in calc_entry.get():
            calc_entry.delete(0, END)
        calc_entry.insert(END, key)


            
bttn_pack=[
    '7','8','9','+','-',
    '4','A','6','*','/',
    '1','2','3','=',
    '0','C','.','%']
r=1
c=0

for i in bttn_pack:
    rel= ''
    cmd=lambda x=i: calc(x)
    
        


    if i == 'A':
        Button(root, text=i, command=cmd, bg='red', width=9).grid(row=r, column=c)
    else:
        ttk.Button(root, text=i, command=cmd).grid (row=r, column=c)
          
    
 
    c += 1
    if c>4:
        c=0
        r += 1

calc_entry= Entry(root, width=33, bg='white', fg='blue' )
calc_entry.grid(row=0, column=0 , columnspan=5)

    
root.mainloop()
 

IT Денис

Новичок
Пользователь
Май 5, 2020
18
1
3
Вот пример:
Python:
from tkinter import *
from tkinter import messagebox
from tkinter import ttk

root=Tk()
root.title('КАЛЬКУЛЯТОР ОТ АМИРА')


def calc(key):
    global memory
    if key == '=':
        strl= '-+0123456789/*'
       
        if calc_entry.get()[0] not in strl:
            calc_entry.insert(END, 'First number is not number!')
            messagebox.showerror('Error!', 'Its not number')
           
        try:
            result=eval(calc_entry.get())
            calc_entry.insert(END, '=' + str(result))
        except:
            calc_entry.insert(END, 'ERROR')
            messagebox.showerror('ERROR', 'CHECK AGAIN')
    elif key == 'C':
        calc_entry.delete(0, END)
    elif key == 'A':
        calc_entry.insert(END, 5)
       
    # 55 + 10% = 60.5
    elif key == '%':
        s = ''
        last_op = None
        last_num = None
        for i in calc_entry.get()[::-1]:
            if i.isdigit():
                s += i
            else:
                last_op = i
                break
        if s != '':
            last_num = int(s[::-1])
        if last_op and last_num:
            result = eval(calc_entry.get()[:-len(str(last_num))][:-1])
            result = eval(str(result) + last_op + str(result * last_num * 0.01))
            calc_entry.delete(0, END)
            calc_entry.insert(0, str(result))
        else:
            calc_entry.delete(0, END)
       
    else:
        if '=' in calc_entry.get():
            calc_entry.delete(0, END)
        calc_entry.insert(END, key)


           
bttn_pack=[
    '7','8','9','+','-',
    '4','A','6','*','/',
    '1','2','3','=',
    '0','C','.','%']
r=1
c=0

for i in bttn_pack:
    rel= ''
    cmd=lambda x=i: calc(x)
   
       


    if i == 'A':
        Button(root, text=i, command=cmd, bg='red', width=9).grid(row=r, column=c)
    else:
        ttk.Button(root, text=i, command=cmd).grid (row=r, column=c)
         
   

    c += 1
    if c>4:
        c=0
        r += 1

calc_entry= Entry(root, width=33, bg='white', fg='blue' )
calc_entry.grid(row=0, column=0 , columnspan=5)

   
root.mainloop()
Спасибо огромное! Очень благодарен!
 

Форум IT Специалистов