如何获取条目的值并在Ssol.txt中输入?

时间:2017-05-31 10:50:45

标签: python python-3.x tkinter pygame

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()

我想获取条目的值并在Ssol.txt中输入它。

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

我能理解这一点,但是当我在谷歌搜索输入时我不明白 所以我想知道如何获取Entry值并在Ssol.txt中输入它。 :)

1 个答案:

答案 0 :(得分:1)

您需要使用.get()来访问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()