Tkinter输出内容到文本框内

img

  • img

img


tkinter如何输出函数结果(如图三)到文本框内
用了text但报错(如图三)

你的print默认都打印到控制台了,main函数返回空,text里怎么会有东西?你把print改成return,然后在main里再把结果return回去试试

回答不易,求求您采纳哦

insert方法Text小部件将结果添加到文本框的末尾。以下是如何执行此操作的示例:

import tkinter as tk

def calculate():
    result = some_function() # Replace this with your function
    text_box.insert(tk.END, result)

root = tk.Tk()

text_box = tk.Text(root)
text_box.pack()

button = tk.Button(root, text="Calculate", command=calculate)
button.pack()

root.mainloop()

在此示例中,calculate单击“计算”按钮时将调用该函数。该函数使用函数计算结果(您将用您自己的函数替换它),然后使用该方法some_function将结果插入文本框。insert

该insert方法有两个参数:插入文本的索引(在本例中,tk.END意味着在文本框的末尾插入文本)和要插入的文本。