这是我的代码,当本来想实现通过stringvar()创建变量然后绑定到entry的textvarible中实现entry中初始化的默认显示文字,但是现在当我把这个frame封进函数中并最终只返回挂载的最低层的frame时,并不能显示这些文字出来,但是不用函数封装直接在外层执行是可以的,下面就是对比效果,看起来封装进去之后变量的set并没有生效并且传递到值里面,有没有人知道怎么回事?
不封装:
封装进函数后:
def date_sign_frame(self):
#为什么封装在函数中调用则textvarible无效?,因为没有使用变量再赋值-sd=sd.set()
sd = ttk.StringVar()
ed = ttk.StringVar()
sd.set(time.strftime("%Y-%m-%d"))
ed.set(time.strftime("%Y-%m-%d"))
datediff = ttk.IntVar()
datediff=datediff.set(2)
username=ttk.StringVar()
password=ttk.StringVar()
username.set("123123")
password.set("456")
print(sd.get())
init_frame = ttk.Frame(self.mywindow, style="secondary",)
start_date = ttk.Entry(init_frame, style="light", textvariable=str(sd), width=12,)
end_date = ttk.Entry(init_frame, style="light", textvariable=str(ed), width=12)
dated = ttk.Entry(init_frame, style="light", textvariable=datediff, width=4)
l_sd = ttk.Label(init_frame, text="起始日期: ", style="secondary", foreground="#ADB5BD", background="#444444")
l_ed = ttk.Label(init_frame, text="结束日期: ", style="secondary", foreground="#ADB5BD", background="#444444")
l_sd.grid(row=1,column=1,padx=(30,5),pady=(0))
start_date.grid(row=1,column=3,columnspan=5)
l_ed.grid(row=2,column=1,padx=(30,5),pady=(0))
end_date.grid(row=2,column=3,columnspan=5)
_username=ttk.Entry(init_frame,style="light", textvariable=str(username), width=20)
_password=ttk.Entry(init_frame,style="light", textvariable=str(password),show="*", width=20)
l_un= ttk.Label(init_frame, text="账户名: ", style="secondary", foreground="#ADB5BD", background="#444444")
l_pw= ttk.Label(init_frame, text="密 码: ", style="secondary", foreground="#ADB5BD", background="#444444")
l_un.grid(row=1,column=20,padx=(45,5),pady=(15,5))
_username.grid(row=1,column=24,columnspan=5)
l_pw.grid(row=2,column=20,padx=(45,5),pady=(5,15))
_password.grid(row=2,column=24,columnspan=5)
signin_butt=ttk.Button(init_frame,text="登 录", bootstyle="success",width=12,command=lambda : self.func._login())
signin_butt.grid(row=1,rowspan=2,column=32,columnspan=3,padx=(30,5))
return init_frame,sd,ed,username,password