简体   繁体   中英

a tkinter program error i cant seem to get rid of

I am getting an error using tkinter and I cannot understand what I am doing incorrectly to cause this error.

Here is my code:

from tkinter import *

windows = Tk()
frame = Frame(windows,height = 200 ,width  = 700)
heading = Label(frame,text="COST CALCULATOR").grid(row = 0,column = 1,columnspan =7)
frame.pack()

def area(length,breadth):
    global area
    ftom(length,breadth)
    area = length*breadth
    return area

t1 = Label(frame,text = "enter the length:").grid(row = 1 ,column = 0)
e1 = Entry(frame)
e1.grid(row =1,column = 1)
ln = e1.get()
e1.delete(0,END)

t2 = Label(frame,text = "enter the breadth:").grid(row = 2 ,column = 0)
e2 = Entry(frame)
e2.grid(row = 2,column = 1)
br = e2.get()
e2.delete(0,END)

t3 = Label(frame,text = "total area covered").grid(row = 3 ,column = 0)
ln = int(ln)
br = int(br)
ar = area(ln,br)
e3 = Label(frame,text =f"{ar}")
e3.grid(row = 3 , column = 1)
windows.mainloop()

And this is the error I get:

Traceback (most recent call last):
  File "C:/Users/Allen Alex Abraham/allensworld/allensworld/trial2.py", line 55, in <module>
    ln = int(ln)
ValueError: **invalid literal for int() with base 10: ''**

The problem is that you're calling e1.get() about a millisecond after you create the entry widget, well before the user has a chance to input data.

GUI programming is not like non-GUI programming - widgets don't block until the user enters something the way that input does. Instead, you need to define a button or some other way for the user to perform the calculation when they are ready (menu item, keyboard binding, etc).

The simplest solution is to create a button to perform the calculation. When you click the button, the function tied to the button can gather the data it needs, call a function to compute the result, and then update the display with the results.

For example, start by defining a function that will get the data and compute the result:

def do_calculation():
    length = int(e1.get())
    breadth = int(e2.get())
    result = area(length, breadth)
    e3.configure(text=result)

Next, create a button that will call this function when clicked:

do_calc_button = Button(frame, command=do_calculation, text="Calculate")
do_calc_button.grid(row=4, column=0)

With that, the user can enter values, click the button, and see the result.

Your code and issue is easier enough to fix. That said you really need to add a description of your problem. It may not be clear to everyone what your issue is just based on code alone. It is considered a bad question and will likely be down-voted and get lest help like it is now.

That said I would change a few things here.

  1. do import tkinter as tk instead if importing * as this will help prevent overwriting of methods down the road.

  2. Your get() methods are executed the instant your code initiates so to fix this problem you need to move them into a function and then call that function with a button or a bind after something has been added to your entry fields.

  3. get() always returns a string so your math wont work until you convert your values to integers or floats. so something like int(e1.get()) will work here. That said to prevent errors when something other than a number is submitted you can use a try/except statement to handle errors.

  4. do not name functions and variables the same thing. This can cause problems so always have uniquer names for your variables/functions/class and so on.

Take a look at the below code and let me know if you have any questions:

import tkinter as tk


windows = tk.Tk()
frame = tk.Frame(windows, height=200, width=700)
tk.Label(frame, text="COST CALCULATOR").grid(row=0, column=1, columnspan=7)
frame.pack()

tk.Label(frame, text="enter the length:").grid(row=1, column=0)
tk.Label(frame, text="enter the breadth:").grid(row=2, column=0)
tk.Label(frame, text="total area covered").grid(row=3, column=0)
e1 = tk.Entry(frame)
e2 = tk.Entry(frame)
e3 = tk.Label(frame, text=f"")
e1.grid(row=1, column=1)
e2.grid(row=2, column=1)
e3.grid(row=3, column=1)


def area_func():
    ln = e1.get()
    br = e2.get()
    e1.delete(0, "end")
    e2.delete(0, "end")
    try:
        area = int(ln) * int(br)
        e3.config(text=f"{area}")
    except BaseException as e:
        print("Value error. Make sure you have only entered a valid integer.")
        print(e)


tk.Button(frame, text="Submit", command=area_func).grid(row=4, column=0, columnspan=2)
windows.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM