¿Cómo mostrar el contenido de un botón haciendo clic en la etiqueta en Python?
# Calculadora
de tkinter import *
app = Tk()
app.geometry ( '400x400')
app.wm_title("Calculadora")
button_txt = [
['7', '8', '9', '+'] ,
['4', '5', '6', '-'],
['1', '2', '3', '*'] ,
['(', ')', '0', '/'],
['borrar', 'retroceso', '.'
]
display = StringVar()
# Contenido de entrada
def set_content(texto):
contenido = display.get() + texto
display.set(content)
# clear
def clear():
display.set('')
# retroceso
def bck():
display.set(str(display.get()[:- 1 ]) )
# es igual a calcular
def calcular():
prueba:
num = display.get()< / p>
res = eval(num)
display.set(num+'='+str(res))
excepto:
display . set('error!')
# Cuadro de visualización
label = Etiqueta(aplicación, textvariable=display, relieve='ridge',
font= ( 'Arail', ' 15'), anclaje=E)
label.place(x=0, y=0, ancho=400, alto=60,)
# UI diseño de la interfaz
para i en el rango(5):
para j en el rango(4):
If button_txt[i][j] == " =":
botón = Botón(
aplicación,
texto=button_txt[i][j],
fuente= ( 'Arial', '14'),
comando= calcular
)
botón.place(x=100*j, y=60*i + 60, ancho=100, alto=60)
elif button_txt[i][j] == "clear":
botón = Botón(
aplicación ,
text=button_txt[i][j],
font=('Arial', '14'),
comando=claro p >
)
botón.place(x=100*j, y=60*i+ 60, ancho=1
00, altura=60)
elif button_txt[i][j] == "retroceso":
botón = Botón(
aplicación,
text=button_txt[i][j],
font= ('Arial', '14'),
comando=bck
)
botón.place(x=100*j, y=60*i+60, ancho=100, alto=60)
más:
botón = Botón(
aplicación,
texto=button_txt[i][j],
fuente=('Arial', '14'),
comando=lambda texto=button_txt[i][j]: set_content( texto)
)
botón.place(x=100*j, y= 60*i+60, ancho=100, alto=60)
#loop
app.mainloop()
Botón.