As I mentioned, you only calculate the area once you run the code. To recalculate the area when items are selected you must associate a signal to an event.
You do not clarify very well what you intend to do, you could create a button so that when the user selects the appropriate values, press it and calculate the area. Another option is that when one or both of the ComboBox values are changed, the area is automatically recalculated without the need to use anything else.
Following this last idea we just have to create a function ( calcular_area
) that will be called every time an item is selected in one of the comboboxs:
import tkinter as tk
from tkinter import ttk
def calcular_area(*event):
area = (float(od_cbox.get()) ** 2 - float(id_cbox.get()) ** 2) * 0.7854
print(area)
ven = tk.Tk()
ven.geometry("1600x900+1+0")
ven.title("ENVOLVENTE OPERATIVA DE COMPLETAMIENTO")
top = tk.Frame(ven, width=1600, height=50, relief="raise", bg="powder blue", bd=10)
top.pack(side=tk.TOP)
f1 = tk.Frame(ven, width=800, height=500, relief="raise", bd=10)
f1.pack(side = tk.LEFT)
f2 = tk.Frame(ven, width=700, height=700, relief="raise", bd=10)
f2.pack(side=tk.RIGHT)
titulo = tk.Label(top, font=('arial', 50, 'bold'), text="ENVOLVENTE OPERATIVA ", fg="black", bd=10, anchor='w')
titulo.grid(row=0, column=0)
#Datos de entrada
od_lbl = tk.Label(f1, font=('arial', 10, 'bold'), text="Diametro externo (in) ",fg="red", bd=16, anchor='w')
od_lbl.grid(row=0, column=0)
od_cbox = ttk.Combobox(f1, textvariable=tk.DoubleVar, font=('arial', 10, 'bold'), width=25)
od_cbox['value'] = ('2.375', '2.875', '3.5', '4', '4.5', '5.5', '6.5')
od_cbox.current(0)
od_cbox.grid(row=0, column=1)
od_cbox.bind("<<ComboboxSelected>>", calcular_area)
id_lbl = tk.Label(f1, font=('arial', 10, 'bold'), text="Diametro interno (in) ", fg="red", bd=16, anchor='w')
id_lbl.grid(row=1, column=0)
id_cbox = ttk.Combobox(f1, textvariable=tk.DoubleVar, font=('arial',10,'bold'), width=25)
id_cbox['value'] = ('1.992', '1.995', '2.041', '2.259', '2.441', '2.992', '4', '4.09', '4.184')
id_cbox.current(0)
id_cbox.grid(row=1, column=1)
id_cbox.bind("<<ComboboxSelected>>", calcular_area)
calcular_area() # Llamamos a la función si queremos que calcule el area con los valores por defecto al iniciar la GUI.
Now every time an item is selected in one of the comboboxes, the area is recalculated and printed.
I have modified some things, the most important thing is:
-
It is a bad practice, as well as dangerous, to make an import of the form from módulo import *
. I have modified the code using import tkinter as tk
which is a correct way to import, in addition to making the code much more readable.
-
You use a variable with the name 'id', id
is a reserved word in Python and you should avoid overwriting it since you can cause errors if a module of those you use comes to use it.