Stack Overflow на русском Asked on January 1, 2022
написал второе приложение,но не нажимаются кнопки,помогите
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,"Первый символ не число!")
messagebox.showerror('Ошибка!','Вы ввели не число')
#Счёт
try:
result = eval(calc_entry.get())
calc_entry.insert(END,'=' + str(result) )
except:
calc_entry.insert(END, 'Ошибка!')
messagebox.showerror('Проверь правильность данных')
#Clean
elif key == 'C':
calc_entry.delete(0,END)
#from + to -
elif key == '-/+':
if '=' in calc_entry.get():
calc_entry.delete (0, END)
try:
if calc_entry.get()[0] == '-':
calc_entry.delete(0)
else:
calc_entry.insert(0,'-')
except IndexError:
pass
else:
if '=' == calc_entry.get():
calc_entry.delete(0,END)
calc_entry.insert(END,key)
#Add btth
btth_list = [
'7','8','9','+','-',
'4','5','6','*','/',
'1','2','3','-/+','=',
'0','.','C'
]
r = 1
c = 0
for i in btth_list:
rel = ''
cmd=lambda x=i: calc(x)
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)
calc_entry.grid(row=0, column=0, columnspan=5)
root.mainloop()
Я смог решить проблему, ниже верный код
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
from functools import partial
root = Tk()
root.title('Калькулятор')
button_list=[]
#Логика калькулятора
def calc(key):
global memory
if key =='=':
#исключаем написание букв
strl ='-+0123456789.*/'
if calc_entry.get()[0] not in strl:
calc_entry.insert(END,"Первый символ не число!")
messagebox.showerror('Ошибка!','Вы ввели не число')
#Счёт
try:
result = eval(calc_entry.get()[0:len(calc_entry.get())])
calc_entry.insert(END,'=' + str(result) )
except:
calc_entry.insert(END, 'Ошибка!')
messagebox.showerror('Проверь правильность данных')
#Clean
elif key == 'C':
calc_entry.delete(0,END)
#from + to -
elif key == '-/+':
if '=' in calc_entry.get():
calc_entry.delete (0, END)
try:
if calc_entry.get()[0] == '-':
calc_entry.delete(0)
else:
calc_entry.insert(0,'-')
except IndexError:
pass
else:
calc_entry.insert(END,key)
#Add btth
btth_list = [
'7','8','9','+','-',
'4','5','6','*','/',
'1','2','3','-/+','=',
'0','.','C'
]
r = 1
c = 0
for i in btth_list:
rel = ''
button_list.append(ttk.Button(root, text=i, command=partial(calc,i)).grid(row=r,column=c))
c += 1
if c>4:
c=0
r += 1
calc_entry=Entry (root, width=33)
calc_entry.grid(row=0, column=0, columnspan=5)
root.mainloop()
Answered by cyr oil on January 1, 2022
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP