python - 收到错误 AttributeError : '_tkinter.tkapp' object has no attribute 'getitems'

标签 python sqlite object tkinter attributeerror

我一直收到错误 AttributeError :

'_tkinter.tkapp' object has no attribute 'getitems'

我认为这是因为子例程无法相互通信我曾尝试将 get_items 分配为全局变量但没有结果。我正在尝试制作一个按钮,用于收集 GUI 中的条目,使用 Sql Lite 将它们输入数据库,用于汽车经销商的库存系统。

from tkinter import* 
import sqlite3
conn = sqlite3.connect("E:\computing project\database of cars.db")
c=conn.cursor()



class Database:
    def __init__ ( kwargs , self, master=None ,*args):
        self.master = master
        self.heading = Label(master , text = "add to the database ", font=("ariel 35 bold") )
        self.heading.place (x=250 , y=0)

        self.name= Label(master, text= "enter product name", font=("arial 18 bold"))
        self.name.place(x=0,y=50)



        self.make_1=  Label(master , text = "enter car make ", font=("ariel 18 bold"))
        self.make_1.place(x=0,y=100)

        self.model_1=  Label(master , text = "enter car model ", font=("ariel 18 bold"),)
        self.model_1.place(x=0,y=150)

        self.regi_1=  Label(master , text = "enter registration plate", font=("ariel 18 bold"))
        self.regi_1.place(x=0,y=200)

        self.colour_1=  Label(master , text = "enter car colour ", font=("ariel 18 bold"))
        self.colour_1.place(x=0,y=250)


        self.cost_1=  Label(master , text = "enter cost price ", font=("ariel 18 bold"))
        self.cost_1.place(x=0,y=300)

        self.tcost_1=  Label(master , text = "enter total cost price ", font=("ariel 18 bold"))
        self.tcost_1.place(x=0,y=350)

        self.sell_1=  Label(master , text = "selling price ", font=("ariel 18 bold"))
        self.sell_1.place(x=0,y=400)

        self.tsell_1=  Label(master , text = "enter total selling price", font=("ariel 18 bold"))
        self.tsell_1.place(x=0,y=450)

        self.assprof_1=  Label(master , text = "enter total selling price", font=("ariel 18 bold"))
        self.assprof_1.place(x=0,y=500)




                #button to add to db

      #  b = Button( command=self.get)
       # b.place(x=0,y=0)

                #entry boxes

        self.name_e = Entry(master, width=25, font=("arial 18 bold"))
        self.name_e.place(x=300, y=50)

        self.name_e = Entry(master, width=25, font=("arial 18 bold"))
        self.name_e.place(x=300, y=100)

        self.name_e = Entry(master, width=25, font=("arial 18 bold"))
        self.name_e.place(x=300, y=150)

        self.name_e = Entry(master, width=25, font=("arial 18 bold"))
        self.name_e.place(x=300, y=200)

        self.name_e = Entry(master, width=25, font=("arial 18 bold"))
        self.name_e.place(x=300, y=250)

        self.name_e = Entry(master, width=25, font=("arial 18 bold"))
        self.name_e.place(x=300, y=300)

        self.name_e = Entry(master, width=25, font=("arial 18 bold"))
        self.name_e.place(x=300, y=350)

        self.name_e = Entry(master, width=25, font=("arial 18 bold"))
        self.name_e.place(x=300, y=400)

        self.name_e = Entry(master, width=25, font=("arial 18 bold",))
        self.name_e.place(x=300, y=450)

        self.name_e = Entry(master, width=25, font=("arial 18 bold"))
        self.name_e.place(x=300, y=500)

        self.b = Button(command=self.getitems)
        self.b.place(x=0,y=0)


    def get_items(self,*args,**kwargs):  #this function gets the items from the entry boxes

        self.carmake=self.carmake_e.get()
        self.carmodel=self.carmodel_e.get()
        self.registrationplate=self.registrationplate_e.get()
        self.carcolour=self.carcolour_e.get()
        self.costprice=self.costprice_e.get()
        self.totalprice=self.totalprice_e.get()
        self.sellingprice=self.sellingprice_e.get()
        self.assumedprofit=self.assumedprofit_e.get()


        if self.carmake == '' or self.carmodel == '' == self.carcolour == '':
            print ("WRONG")

        else:
            print("solid m8 ")




root = Tk()
b = Database(root)
frame = Frame(root,width=1920,height=1080)
root.geometry("1920x1080")
root.title("add to database")
root.configure(background="#b7b7e5")
root.mainloop()

最佳答案

我看到两个错误:

首先:您定义了方法get_items(名称中带有_)但是您使用了command=self.getitems (名称中没有 _)

 self.b = Button(command=self.get_items)

第二:你的参数顺序错误是

__init__(kwargs, self, master=None, *args)

因此它将类实例分配给变量 kwargs 而不是 self 并将 root 分配给 self 及以后找到 self.get_items 有问题,因为它意味着 root.get_items

kwargs应该在最后,self在开头

__init__(self, master=None, *args, **kwargs)

关于python - 收到错误 AttributeError : '_tkinter.tkapp' object has no attribute 'getitems' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56894653/

相关文章:

Python 3 导入错误 : No module named 'ConfigParser'

sqlite - 什么时候应该在 sqlite 中使用数据类型 REAL 与 NUMERIC?

JavaScript。为什么创建新对象时会覆盖私有(private)函数?

c# - 在 C# 中,当括号表示法不起作用时如何访问对象的对象?

java - 有一个 Java 类,其中一个字段是一个方法?

python - 动态创建嵌套 for 循环?

python - Bokeh 直方图不会绘制

python - 从印度专利网站上抓取专利数据

SQLite:在启用 FTS4 的表中使用比较运算符进行查询

android - 每次调用 rawQuery() 时是否必须关闭 Cursor 对象?