如何修复AttributeError:'StringVar'对象没有属性'\u name'

2024-10-04 07:26:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python新手,我正在尝试使用tkinter创建一个计费系统,但是我遇到了这个错误

def showbillingsystem():
    global tp
    tp = Toplevel()
    tp.geometry("890x580+0+0")
    tp.title(" BILLING SYSTEM")

    Tops = Frame(tp, bg="white", width=1600, height=50, relief=SUNKEN)
    Tops.pack(side=TOP)

    f1 = Frame(tp, width=900, height=700, relief=SUNKEN)
    f1.pack(side=LEFT)

    f2 = Frame(tp, width=400, height=700, relief=SUNKEN)
    f2.pack(side=RIGHT)
    # ------------------TIME--------------
    localtime = time.asctime(time.localtime(time.time()))
    # -----------------INFO TOP------------
    rand = StringVar()



    lblinfo = Label(Tops, font=('aria', 30, 'bold'), text=" BILLING SYSTEM", fg="Black", bd=10, anchor='w')
    lblinfo.grid(row=0, column=0)
    lblinfo = Label(Tops, font=('aria', 20,), text=localtime, fg="steel blue", anchor=W)
    lblinfo.grid(row=1, column=0)
    lblreference = Label(f1, font=('aria', 16, 'bold'), text="Order No.", fg="brown", bd=20, anchor='w')
    lblreference.grid(row=0, column=0)
    txtreference = Entry(f1, font=('ariel', 16, 'bold'), textvariable = rand, bd=6, insertwidth=6, bg="yellow",justify = RIGHT)
    txtreference.grid(row=0, column=1)

这些是错误信息

File "C:/Users/rajat/PycharmProjects/untitled1/venv/MAINPROJECT.py", line 278, in showbillingsystem
    txtreference = Entry(f1, font=('ariel', 16, 'bold'), textvariable = rand, bd=6, insertwidth=6, bg="yellow",justify = RIGHT)
  File "C:\Users\rajat\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2676, in __init__
    Widget.__init__(self, master, 'entry', cnf, kw)
  File "C:\Users\rajat\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2299, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
  File "C:\Users\rajat\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 341, in __str__
    return self._name
AttributeError: 'StringVar' object has no attribute '_name'

Tags: timeinittkintercolumnusersbdgridfile