简体   繁体   English

我似乎无法摆脱的 tkinter 程序错误

[英]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.我在使用 tkinter 时遇到错误,我无法理解我做错了什么导致了这个错误。

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.问题是您在创建条目小部件后大约一毫秒调用e1.get() ,远在用户有机会输入数据之前。

GUI programming is not like non-GUI programming - widgets don't block until the user enters something the way that input does. GUI 编程与非 GUI 编程不同 - 小部件不会阻塞,直到用户以输入方式input某些内容。 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.如果导入 *,请不要import tkinter as tk因为这将有助于防止在路上覆盖方法。

  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.您的get()方法在您的代码启动时立即执行,因此要解决此问题,您需要将它们移动到一个函数中,然后在将某些内容添加到您的输入字段后使用按钮或绑定调用该函数。

  3. get() always returns a string so your math wont work until you convert your values to integers or floats. get()始终返回一个字符串,因此在将值转换为整数或浮点数之前,您的数学不会起作用。 so something like int(e1.get()) will work here.所以像int(e1.get())这样的东西可以在这里工作。 That said to prevent errors when something other than a number is submitted you can use a try/except statement to handle errors.也就是说,为了防止在提交数字以外的内容时出现错误,您可以使用try/except语句来处理错误。

  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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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