It's getting garbage collected.
You can just get rid of the function (you also shouldn't nest functions like this)
from tkinter import *
from tkinter.ttk import *
tk=Tk()
var=StringVar(value="default value")
Entry(tk, textvariable=var).pack()
Button(tk,text='OK').pack()
mainloop()
Or, if you want to keep the function.. set the stringvar as an attribute of tk or make it global.
Making it global:
from tkinter import *
from tkinter.ttk import *
tk=Tk()
var = StringVar(value="Default value")
def sub():
Entry(tk, textvariable=var).pack()
Button(tk,text='OK').pack()
sub()
mainloop()
As an attribute of tk:
from tkinter import *
from tkinter.ttk import *
tk=Tk()
def sub():
tk.var = StringVar(value="Default value")
Entry(tk, textvariable=tk.var).pack()
Button(tk,text='OK').pack()
sub()
mainloop()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…