注册 登录
编程论坛 Python论坛

关于tkinter中textvariable的问题,请帮我一下,谢谢了。

wangjx236007 发布于 2022-10-16 15:38, 1046 次点击
各位高手,我用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()
4 回复
#2
wangjx2360072022-10-17 09:51
请帮我一下,我可以付费。
#3
厨师王德榜2022-10-17 17:13
我作了如下尝试:
把  test2中,定义 日期框 的语句,改为
self.lab=Label(self.loginwin,text="", font="宋体 10", relief="groove", state="disable")
把点击按钮的代码,最后一句,改为:
self.lab["text"] = bh2
可以成功.
#4
wp2319572022-10-18 09:32
回复 2楼 wangjx236007
这样也可以成功

from tkinter import *
import datetime

class wp:
    def __init__(self):
        self.loginwin=Tk()
        self.loginwin.title("系统登录")
        self.username=Entry(self.loginwin,font="宋体 12")
        self.username.place(x=50,y=50,width=100,height=20)
        bnt1=Button(self.loginwin,text="登录",command=self.st_date).place(x=50,y=120,width=50,height=20)
    def st_date(self):
        date=datetime.datetime.now()
        bh2 = date.strftime("%Y-%m-%d")
        self.username.delete(0,END)
        self.username.insert(0,bh2)
#5
wangjx2360072022-10-19 07:43
非常感谢
1