关于tkinter中textvariable的问题,请帮我一下,谢谢了。
各位高手,我用tkinter做了一个窗体test2,单独运行没有问题。但通过窗体test1进入窗体test2时,就不能自动填写日期,请帮我,非常感谢。test2.py
import datetime
from tkinter import *
from tkinter.ttk import *
from tkinter import ttk
# from datetime import datetime
class mainWin:
def __init__(self):
self.loginwin=Tk()
self.loginwin.title("主窗体")
self.width = self.loginwin.winfo_screenwidth() * 0.2
self.height = self.loginwin.winfo_screenheight() * 0.2
self.screenwidth = self.loginwin.winfo_screenwidth()
self.screenheight = self.loginwin.winfo_screenheight()
self.alignstr = '%dx%d+%d+%d' % (
self.width, self.height, (self.screenwidth - self.width) / 2, (self.screenheight - self.height) / 2)
self.loginwin.geometry(self.alignstr)
self.date_now=StringVar()
self.riqi=Label(self.loginwin,text="自动填当前日期:").place(relx=0.01,rely=0.2)
self.lab=Label(self.loginwin,textvariable=self.date_now, font="宋体 10", relief="groove", state="disable")
self.lab.place(relx=0.3,rely=0.2,relwidth=0.3)
self.btn=Button(self.loginwin,text="填日期",command=self.st_date).place(relx=0.4,rely=0.4)
self.loginwin.mainloop()
def st_date(self):
date=datetime.datetime.now()
bh2 = date.strftime("%Y-%m-%d")
self.date_now.set(bh2)
test1.py
from tkinter import *
from tkinter.ttk import *
from tkinter import ttk
from tkinter.messagebox import *
import test2
class Win:
def __init__(self):
self.loginwin=Tk()
self.loginwin.title("系统登录")
self.width = self.loginwin.winfo_screenwidth() * 0.2
self.height = self.loginwin.winfo_screenheight() * 0.2
self.screenwidth = self.loginwin.winfo_screenwidth()
self.screenheight = self.loginwin.winfo_screenheight()
self.alignstr = '%dx%d+%d+%d' % (
self.width, self.height, (self.screenwidth - self.width) / 2, (self.screenheight - self.height) / 2)
self.loginwin.geometry(self.alignstr)
self.litl=Label(self.loginwin,text="延伸服务系统",font="宋体 14 bold").place(relx=0.18,rely=0.14)
self.separator = ttk.Separator(self.loginwin).place(relx=0, rely=0.3, relwidth=1)
self.lab=Label(self.loginwin,text="用 户 名:",font="宋体 12").place(relx=0.15,rely=0.4)
self.username=Entry(self.loginwin,font="宋体 12")
self.username.place(relx=0.4,rely=0.4)
self.labb2 = Label(self.loginwin, text="用 户 名:", font="宋体 12").place(relx=0.15, rely=0.6)
self.password = Entry(self.loginwin, font="宋体 12",show="*")
self.password.place(relx=0.4, rely=0.6)
self.bnt1=Button(self.loginwin,text="登录",command=self.login).place(relx=0.28,rely=0.8)
self.bnt1 = Button(self.loginwin, text="退出", command= self.loginwin.destroy).place(relx=0.58, rely=0.8)
self.loginwin.mainloop()
# main_win.mainWin()
def login(self):
username=self.username.get()
password=self.password.get()
if username=="" and password=="":
showinfo("错误","请输入用户名和密码")
else:
if username=="wjx" and password=="123456":
test2.mainWin()
else:
self.username.delete(0,END)
self.password.delete(0,END)
showinfo("警告","请输入正确的用户名和密码!")
Win()