属性错误:'无类型'对象没有属性'插入'。插入到文本框中

2022-03-11 00:00:00 python python-3.x tkinter text-files

问题描述

因此,我已经获得了将一些数据打印到文本文件的基本函数。我想将此数据放入Tkinter的文本框中。我遇到的问题是,它将所有数据保存到文件中,然后再次读取所有数据,但没有将其插入到文本框中。当我单击Calculate(触发函数的按钮)时,它返回错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:Python33lib	kinter\__init__.py", line 1475, in __call__
    return self.func(*args)
  File "D:Python ProjectRoof Pitch Calculator.py", line 52, in Calculate
    ResultsBox.insert(END, contents)
AttributeError: 'NoneType' object has no attribute 'insert'

该函数包含以下代码:

def Calculate():
    h1 = float(LongHeight.get())
    h2 = float(ShortHeight.get())
    aj = float(Width.get())
    d = float(Depth.get())

    opc = h1 - h2
    Opc_Width = opc / aj
    Radians = math.atan(opc / aj)
    Pitch = math.degrees(math.atan(opc / aj))
    Width_And_Height_Squared = (opc * opc) + (aj * aj)
    Square_Root = math.sqrt((opc * opc) + (aj * aj))
    Roof_Area = math.sqrt((opc * opc) + (aj * aj))*d

    text_file = open("temp.txt", "a")
    text_file.write("Longest Height: ")
    text_file.write(str(h1))
    text_file.write("
")
    text_file.write("Shortest Height: ")
    text_file.write(str(h2))
    text_file.write("
")
    text_file.write("Width: ")
    text_file.write(str(aj))
    text_file.write("
")
    text_file.write("Depth: ")
    text_file.write(str(d))
    text_file.write("
")
    text_file.write("Pitch: ")
    text_file.write(str(Pitch))
    text_file.write("
")
    text_file.write("Roof Area: ")
    text_file.write(str(Roof_Area))
    text_file.write("
")
    text_file.write("
")
    text_file.close()

    text_file = open("temp.txt")
    contents = ""

    for i in text_file:
        contents += i

    ResultsBox.insert(END, contents)
    text_file.close()
此外,有没有一种方法可以将数据直接放入文本框中,而不是先放入文本文件,然后放入文本框?如果是这样的话,能不能请谁告诉我怎么做,因为我怎么也想不出来。

编辑:完整代码如下:

from tkinter import *
from tkinter.ttk import *
import math, os

try:
    os.remove("temp.txt")
except OSError:
    pass

def Calculate():
    h1 = float(LongHeight.get())
    h2 = float(ShortHeight.get())
    aj = float(Width.get())
    d = float(Depth.get())

    opc = h1 - h2
    Opc_Width = opc / aj
    Radians = math.atan(opc / aj)
    Pitch = math.degrees(math.atan(opc / aj))
    Width_And_Height_Squared = (opc * opc) + (aj * aj)
    Square_Root = math.sqrt((opc * opc) + (aj * aj))
    Roof_Area = math.sqrt((opc * opc) + (aj * aj))*d

    text_file = open("temp.txt", "a")
    text_file.write("Longest Height: ")
    text_file.write(str(h1))
    text_file.write("
")
    text_file.write("Shortest Height: ")
    text_file.write(str(h2))
    text_file.write("
")
    text_file.write("Width: ")
    text_file.write(str(aj))
    text_file.write("
")
    text_file.write("Depth: ")
    text_file.write(str(d))
    text_file.write("
")
    text_file.write("Pitch: ")
    text_file.write(str(Pitch))
    text_file.write("
")
    text_file.write("Roof Area: ")
    text_file.write(str(Roof_Area))
    text_file.write("
")
    text_file.write("
")
    text_file.close()

    text_file = open("temp.txt")
    contents = ""

    for i in text_file:
        contents += i

    ResultsBox.insert(END, contents)
    text_file.close()

    ShortHeight.set("")
    LongHeight.set("")
    Depth.set("")
    Width.set("")


window = Tk()
window.geometry("600x400+200+200")
window.title("Roof Pitch Calculator")
window.wm_resizable(0,0)

TitleLabel = Label(window, font = "Comic 30", foreground = "deep sky blue", text = "Roof Pitch Calculator").pack()

CalculateButton = Button(window, text = "Calculate", command = Calculate).pack(side = BOTTOM, pady = 5)
ResultsBox = Text(window, height = 13, width = 70).pack(side = BOTTOM)
ShortHeightLabel = Label(window, text = "Short Height").place(x = 190, y = 50)
LongHeightLabel = Label(window, text = "Long Height").place(x = 330, y = 50)
DepthLabel = Label(window, text = "Depth").place(x = 207, y = 100)
WidthLabel = Label(window, text = "Width").place(x = 345, y = 100)

ShortHeight = StringVar()
ShortHeightEntry = Entry(window, textvariable = ShortHeight)
ShortHeightEntry.place(x = 161, y = 71)

LongHeight = StringVar()
LongHeightEntry = Entry(window, textvariable = LongHeight)
LongHeightEntry.place(x = 300, y = 71)

Depth = StringVar()
DepthEntry = Entry(window, textvariable = Depth)
DepthEntry.place(x = 161, y = 120)

Width = StringVar()
WidthEntry = Entry(window, textvariable = Width)
WidthEntry.place(x = 300, y = 120)

window.mainloop()

解决方案

您正在将.pack()的结果分配给ResultsBox,请尝试在 两行:

ResultsBox = Text(window, height = 13, width = 70)
ResultsBox.pack(side = BOTTOM)
此外,您也不需要编写文本文件,然后重新打开它来加载 内容。我会使用列表来存储内容,然后我会使用 保存和填写文本框的列表:

contents_list = ["Longest Height: ",
                 str(h1),
                 "
",
                 ...]
contents = "".join(contents_list)
text_file.write(contents)
ResultsBox.insert(END, contents)

相关文章