| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 856 人关注过本帖
标题:关于tkinter中textvariable的问题,请帮我一下,谢谢了。
只看楼主 加入收藏
wangjx236007
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2021-3-2
结帖率:40%
收藏
已结贴  问题点数:10 回复次数:4 
关于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()
搜索更多相关主题的帖子: from tkinter username text import 
2022-10-16 15:38
wangjx236007
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2021-3-2
收藏
得分:0 
请帮我一下,我可以付费。
2022-10-17 09:51
厨师王德榜
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:199
帖 子:989
专家分:4946
注 册:2013-2-16
收藏
得分:5 
我作了如下尝试:
  test2中,定义 日期框 的语句,改为
self.lab=Label(self.loginwin,text="", font="宋体 10", relief="groove", state="disable")
把点击按钮的代码,最后一句,改为:
self.lab["text"] = bh2
可以成功.
2022-10-17 17:13
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:5 
回复 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)

DO IT YOURSELF !
2022-10-18 09:32
wangjx236007
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2021-3-2
收藏
得分:0 
非常感谢
2022-10-19 07:43
快速回复:关于tkinter中textvariable的问题,请帮我一下,谢谢了。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015775 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved