鱼C论坛

 找回密码
 立即注册
查看: 6157|回复: 3

[已解决]entry的get方法出现_tkinter.TclError: invalid command name ".!label....

[复制链接]
发表于 2019-3-23 00:14:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
完整代码如下:
from tkinter import *
from tkinter import messagebox

root = Tk()
def sigin_interface():
    root.destroy()
    global root1
    root1 = Tk()
    def on_click():
        messagebox.showinfo(parent=root1,title="success demo",message="恭喜您注册成功,请关闭程序重新登陆")
    root1.title("sigin demo")
    account_lable = Label(root1,text="账号:")
    account_lable.grid(row=0,column=0,padx=5,pady=5)
    password_lable = Label(root1,text="密码:")
    password_lable.grid(row=1,column=0,padx=5,pady=5)
    repassword_lable = Label(root1,text="再次确认密码:")
    repassword_lable.grid(row=2,column=0,padx=5,pady=5)

    account_entry = Entry(root1,width='25')
    account_entry.grid(row=0,column=1,padx=5,pady=5)
    password_entry = Entry(root1,width='25')
    password_entry.grid(row=1,column=1,padx=5,pady=5)
    repassword_entry = Entry(root1,width='25')
    repassword_entry.grid(row=2,column=1,padx=5,pady=5)
   
    success_button = Button(root1,text="注册",width=10,command=on_click)
    success_button.grid(row=3,column=0,sticky=W,padx=10,pady=5)
    quit_button = Button(root1,text="退出",width=10,command=root1.quit)
    quit_button.grid(row=3,column=1,sticky=E,padx=10,pady=5)
    root1.mainloop()
def backstage_interface():
    root.destroy()
    global root2
    root2 = Tk()
    def buy():
        pass
    root2.title("login demo")
    act = str(input_account.get())                                    #问题在此:调用get方法提示错误_tkinter.TclError: invalid command name ".!label.!entry"
    a = act[:1]
    if  a == '1' :
        label1 = Label(root2,text="user-login")
        label1.pack()
        button = Button(root2,text="login",command=buy)
        button.pack()
        print("user-login")
    elif a == '2':
        label2 = Label(root2,text="worker-login")
        label2.pack()
        print("worker-login")
    else:
        label3 = Label(root2,text="miatake")
        label3.pack()
        print("miatake")
    root2.mainloop()
   
root.title("**医药系统 demo")
root.geometry('900x600')

bg_image = PhotoImage(file='cloud.png')
bg_label = Label(root,image=bg_image)
bg_label.place(x=0,y=0,relwidth=1,relheight=1)

canvas = Canvas(bg_label,height=150,width=300)
image_file = PhotoImage(file='cd.gif')
image = canvas.create_image(0,0,anchor='w',image=image_file)
canvas.pack(side='top')

label_account = Label(bg_label,text='account:   ',)
label_account.place(x=310,y=340)
label_password = Label(bg_label,text='password:')
label_password.place(x=310,y=380)

input_account = Entry(bg_label,width='30')
input_account.place(x=380,y=340)
input_passeord = Entry(bg_label,width='30',show='*')
input_passeord.place(x=380,y=380)

login_button = Button(bg_label,text="login",command=backstage_interface)
login_button.place(x=340,y=450)
sigin_button = Button(bg_label,text="sigin",command=sigin_interface)
sigin_button.place(x=520,y=450)

root.mainloop()
最佳答案
2019-3-23 01:10:14
你点击login按钮后,会调用backstage_interface方法
backstage_interface方法中你调用了root的destroy方法
整个root窗口包括root里面的组件都没了
所以,input_account.get()会报错

解决方法:绑定 Entry 组件到 Tkinter的StringVar变量,并通过该变量设置和获取输入框的文本
具体做法就是
加一行代码  v = StringVar()
input_account = Entry(bg_label,width='30')  改成 input_account = Entry(bg_label,width='30', textvariable=v)
再把input_account.get() 改成 v.get()
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-3-23 00:15:36 | 显示全部楼层
请问这个错误发生的原因是啥,该怎么改呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-23 01:10:14 | 显示全部楼层    本楼为最佳答案   
你点击login按钮后,会调用backstage_interface方法
backstage_interface方法中你调用了root的destroy方法
整个root窗口包括root里面的组件都没了
所以,input_account.get()会报错

解决方法:绑定 Entry 组件到 Tkinter的StringVar变量,并通过该变量设置和获取输入框的文本
具体做法就是
加一行代码  v = StringVar()
input_account = Entry(bg_label,width='30')  改成 input_account = Entry(bg_label,width='30', textvariable=v)
再把input_account.get() 改成 v.get()
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-23 14:26:23 | 显示全部楼层
在东边 发表于 2019-3-23 01:10
你点击login按钮后,会调用backstage_interface方法
backstage_interface方法中你调用了root的destroy方法 ...

亲测正确有效,谢谢大佬。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-9 03:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表