如何将文本框中的输入存储在python中?

How to store the input from the text box in python?

我想制作一个程序,使用 python tkinter GUI 显示文本框中输入的数字的除数,并将结果存储到纯文本文件中。

我不知道如何从文本框中获取值。我知道那是与 get() 相关的东西,我读了一些东西,但我还是不明白。

代码如下:

from tkinter import *

def create_file():
    file_object = open("C:/Users/valis/Desktop/Divisors.txt","w+")


def evaluate():
    show_after= Label(text= "Check your Desktop !")
    show_after.pack(pady=2, anchor=SW)
    create_file()


#Windows size and Title
window = Tk()
window.title("Show Divisors")
window.geometry("300x100")

message = Label(window, text="Enter a number : ")
message.pack(pady=2, anchor=NW)

textbox_input = Text(window,height=1, width=11)
textbox_input.pack(padx= 2,pady= 2, anchor=NW)

window.mainloop()

代码不完整,怎么办?

如您所说,您将使用 get() 函数,但带有一些附加属性。

如果我们有一个文本框 textbox_input,那么您可以 return 使用此行输入:

test_input = textbox_input.get("1.0",END)

第一部分,"1.0" 意味着输入应该从第一行,字符零(即:第一个字符)开始读取。 END 是一个导入常量,设置为字符串 "end"。 END部分表示一直读到文本框末尾。

参考:This answer.