【问题标题】:How can I make my 'Label' object have an attribute 'get'?如何使我的“标签”对象具有属性“获取”?
【发布时间】:2021-06-29 16:30:24
【问题描述】:

我的代码显示 AttributeError: 'Label' 对象没有属性 'get'。我已经使用了所有可以联系到的可用资源,但无事可做。如果有人能花一分钟时间帮助我,我会很高兴。错误在第 67 行,上面写着:Email = Label(root, text='Email/USER ID', font = ('consolas',13))。阿里加托

from tkinter import *
from tkinter import messagebox
import mysql.connector


global root
root = Tk()

root.geometry("350x450")
root.title("LOGIN")
root.resizable(0,0)

j=0
g=0
for i in range(100):
    c = str(222222+g)
    Frame(root, width=10, height=450,bg="gray").place(x=j, y=0)
    j = j + 10
    g = g + 1

Frame(root, width=250, height=350).place(x=50, y=50)

#============VARIABLES==========
Email = StringVar()
Password = StringVar()
#===============================

#Label Username/Email
Email = Label(root, text='Email/USER ID', font = ('consolas',13))
Email.place(x=80, y=170)
e = Entry(root, width=20, font = ('consolas',13))
e.place(x=80, y=200)

#Label password
Password = Label(root, text='Password', font = ('consolas',13))
Password.place(x=80, y=230)
e1 = Entry(root, width=20, font = ('consolas',13), show = '*')
e1.place(x=80, y=260)
stayButton = Checkbutton(root, text = "Remember Password")
stayButton.place(x=80, y=300)

def Database():
    global conn, cursor
    conn = mysql.connector.connect(host="localhost", user="root", passwd="", db="myPython")
    cursor = conn.cursor()

def login():
    Database()
    ee = Email.get()
    pp = Password.get()
    if ee == "" or pp =="":
        messagebox.showinfo("Alert!","Enter your Email/Password")
    else:
        cursor.execute("SELECT * FROM `user` WHERE 'email' = %s", [ee])
        if cursor.fetchone() is not None:
            messagebox.showinfo("Alert", "Username already in use")
        else:
            cursor.execute("INSERT INTO `user` (email, password) VALUES (%s, %s)", (str(ee)))
            conn.commit()
            Email.set("")
            Password.set("")
            messagebox.showinfo("Meassage", "Logged in Successfully")
        cursor.close()
        conn.commit()
Button(root, width=10, height=1, border=0, fg="white", bg="gray", command=login, text="SIGN IN").place(x=135, y=335)
b1=Button(root, width=10, height=1, border=0, fg="white", bg="gray", text="SIGN UP").place(x=135, y=370)

root.mainloop()

【问题讨论】:

  • 你希望这个get方法做什么?

标签: python python-3.x tkinter


【解决方案1】:

您将Email 创建为StringVar,但在您将其设置为Label 对象之后。您可能想要耦合 StringVarLabel,如

EmailVar = StringVar()
EmailLabel = Label(root, text='Email/USER ID', font = ('consolas',13))
EmailLabel["textvariable"] = EmailVar

【讨论】:

    【解决方案2】:

    您为StringVarLabel 使用了相同的变量Email,所以Email 最终将引用Label。同样适用于Password

    我想你想在那些Entry 小部件中使用StringVars

    #============VARIABLES==========
    Email = StringVar()
    Password = StringVar()
    #===============================
    
    # use another name for label
    email_lbl = Label(root, text='Email/USER ID', font = ('consolas',13))
    email_lbl.place(x=80, y=170)
    # use textvariable=Email in Entry(...)
    e = Entry(root, width=20, font = ('consolas',13), textvariable=Email)
    e.place(x=80, y=200)
    
    # use another name for label
    password_lbl = Label(root, text='Password', font = ('consolas',13))
    password_lbl.place(x=80, y=230)
    # use textvariable=Password in Entry(...)
    e1 = Entry(root, width=20, font = ('consolas',13), show = '*', textvariable=Password)
    e1.place(x=80, y=260)
    

    【讨论】:

      猜你喜欢
      • 2019-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-31
      • 2021-11-30
      • 2012-04-18
      • 2018-04-19
      • 2012-01-28
      相关资源
      最近更新 更多