TypeError:“ Entry”对象无法解释为整数

时间:2018-11-17 07:21:37

标签: python tkinter

from tkinter import ttk, simpledialog
import tkinter as tk
from tkinter import *

root = Tk()
root.resizable(0, 0)
root.title("Sorting and Searching Algorithm")
root.configure(bg='#ff8080')
root.geometry("750x550")

def arrays():
    v = IntVar()
    for widget in root.winfo_children():
        widget.destroy()
    def close():
        for widget in root.winfo_children():
            widget.destroy()
        arrays()

    titleFrame = Frame(root)
    titleFrame.grid(row=0)

    radioFrame = Frame(root)
    radioFrame.grid(padx=350, pady=100)

    inputFrame = tk.Frame(root, bg='#ff8080')
    inputFrame.grid()

    buttonFrame = Frame(root)
    buttonFrame.grid()

    Title = tk.Label(titleFrame, bg='#ff8080', text="Enter The Number of Elements In The Array", font="-weight bold")
    Title.grid()

    global NUMBER_OF_ENTRIES
    NUMBER_OF_ENTRIES = Entry(inputFrame)
    NUMBER_OF_ENTRIES.grid(row=0, column=1, sticky=E, ipadx=10, ipady=10,padx=10, pady=10)

    if NUMBER_OF_ENTRIES == int:
        print("Working")
    else:
        print("Please Enter a Integer Value")

    global num
    num = 0

    #global NUMBER_OF_ENTRIES
    #NUMBER_OF_ENTRIES = simpledialog.askinteger("Please Enter", "Enter The Number of Elements In The Array")

    global alist
    alist = []

    for i in range (0, NUMBER_OF_ENTRIES):
        num = simpledialog.askinteger("Please Enter" ,"Enter The Entries In Array Element " + str(i))
        alist = alist + [ num ]

    calculate = ttk.Button(buttonFrame, text="Proceed", command=entries)
    calculate.grid(row=4, column=0, sticky=E + S, ipadx=10, ipady=10)

arrays()
root.mainloop()

我正在尝试这样做,以便当用户在“输入”输入框中输入一个整数时,它将存储在变量NUMBER_OF_ENTRIES中。在存储它之后,它将继续在其他条件中使用该值。

但是当我尝试编译它时遇到了一个问题。

1 个答案:

答案 0 :(得分:0)

因为它不是整数。 NUMBER_OF_ENTRIES的类型为<class 'tkinter.Entry'>

执行此操作的通常方法是将条目与StringVar()关联,该int会反映键入该条目的内容。

输入到条目中的文本仍然是文本,因此您必须将其显式转换为out.exe

请参见The Tkinter Entry Widget