How do I get the value of an entry and enter it in Ssol.txt? [duplicate]

让人想犯罪 __ 提交于 2021-02-05 12:17:34

问题


from tkinter import *
from tkinter import ttk

win = Tk()
win.title("hello")
win.geometry('510x50+200+100')
Block = IntVar()

def x():
    k = open("Ssol.txt", 'w')

Entry(win, width=5, textvariable=Block).grid(column=1, row=0,sticky=(N,W,E))
ttk.Button(win, text="실행", command=x).grid(column=0, row=1,sticky=(W,E))

mainloop()

I want to get the value of entry and input it in Ssol.txt.

def x():
    k = open("write_Value.txt", 'w')

I can understand this but I do not understand when I search for input in Google so then I want to know how to get the Entry value and enter it in Ssol.txt. :)


回答1:


You need to use .get() to access the value of your IntVar:

from tkinter import *

win = Tk()
Block = IntVar()

def Block_to_file():
    contents = str(Block.get())
    with open("Ssol.txt", 'w') as f:
        f.write(contents)

Entry(win, width=15, textvariable=Block).grid(column=0, row=0, sticky=(N, W, E))
Button(win, text='Write to file', command=Block_to_file).grid(column=0, row=1, sticky=(W, E))

mainloop()


来源:https://stackoverflow.com/questions/44282757/how-do-i-get-the-value-of-an-entry-and-enter-it-in-ssol-txt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!