python 生成包含多行代码的函数

hec6srdp  于 2023-04-28  发布在  Python
关注(0)|答案(1)|浏览(99)

我的代码实际上是这样的:更多代码

names = aoracandlestudiodb.cursor()
        names.execute('SELECT _name FROM items')
        item_name = names.fetchall()
        i = 0
        distance = 0
        name = 1
        for item in item_name:
            for j in range(len(item)):
                buttoncommandstr = "generatebuttoncommand{}".format(i)
                print(buttoncommandstr)
                result_button = eval('self.' + buttoncommandstr)

                fontanierabutton = ctk.CTkButton(self, text=str(item[j]), width=200, height=20,
                                                 fg_color='grey',
                                                 bg_color='white',
                                                 corner_radius=5,
                                                 text_color='black', font=("Arial bold", 20),
                                                 command=lambda: result_button(name))
                fontanierabutton.place(x=80, y=distance)
                print(name)
            i = i + 1
            name = name + 1
            distance = distance + 40

    def generatebuttoncommand0(self, x):
        print(x)
        return x

    def generatebuttoncommand1(self, x):
        print(x)

    def generatebuttoncommand2(self, x):
        print(x)

    def generatebuttoncommand3(self, x):

等等
但是我必须根据SQL数据库中的项目来生成“def generatebuttoncommand{num}”。但是!你会说我可以很容易地成功,通过做第二个新的“为”一样

names = aoracandlestudiodb.cursor()
   names.execute('SELECT _name FROM items')
   item_name = names.fetchall()
   i = 0
   distance = 0
   name = 1
   for item in item_name:
        for j in range(len(item)):
             exec(f"def generatebuttoncommand{i}(): ...")
        i = i + 1

这将生成函数,但我只能添加,最多10行,但我想在每个按钮上添加的代码更像是80行,上面有新的按钮和新的“命令”调用。
我希望得到的结果是:(在我的SQL表中有10个项目)
(item 1按钮)---〉标签(_name,_cost,_price,_category)在单独的框,并获得一个新的按钮---〉更新sql数据,如果我想改变这些列中的任何一个
(item 2按钮)---〉标签与(_name2,_cost2,_price2,_category2)在单独的框,并得到一个新的按钮---〉更新sql数据,如果我想改变这些列中的任何一个
然后,如果我添加一个新的项目,它将自动生成所有这些功能和按钮。
我确实尝试了另一种方式生成按钮取决于sql数据库,但我得到了其他错误

CallDB = aoracandlestudiodb.cursor()
        CallDB.execute('SELECT * FROM items')
        items_ID3 = CallDB.fetchall()
        
        i = 1
        distance = 0
        for item in items_ID3:
            for j in range(len(item)):
                print("hi")
                query_name = 'SELECT _name FROM items WHERE iditems=%s'
                query_ID = 'SELECT iditems FROM items WHERE iditems=%s'

                item_NAME = CallDB.execute(query_name, (i,))
                print(item_NAME)
                items_ID = CallDB.execute(query_ID, (i,))
                self.MakeButtonsForItems(item_NAME, distance, items_ID)

            i = i + 1
            distance = distance + 40

    def MakeButtonsForItems(self, buttonname, distance, buttonid):
        buttonGenerator = ctk.CTkButton(self, text=buttonname, width=200, height=20,
                                         fg_color='grey',
                                         bg_color='white',
                                         corner_radius=5,
                                         text_color='black', font=("Arial bold", 20),
                                         command=lambda: self.genaratebuttoncommand0(buttonid))
        buttonGenerator.place(x=80, y=distance)

错误是“Traceback(most recent call last):文件“C:\Users\katsi\PycharmProjects\PycharmProjects\pythonProject\test3.py”,line 173,in app = BaseWindow()File“C:\Users\katsi\PycharmProjects\PycharmProjects\pythonProject\test3.py”,第63行,在inititems_ID = CallDB。execute(query_ID,(i,))File“C:\Users\katsi\PycharmProjects\venv\lib\site-packages\mysql\connector\cursor_cext.py”,第303行。_cnx.handle_unread_result()文件“C:\Users\katsi\PycharmProjects\venv\lib\site-packages\mysql\connector\connection_cext.py”,line 921,in handle_unread_result raise InternalError(“Unread result found”)mysql.connector.errors.内部错误:找到未读结果“

v8wbuo2f

v8wbuo2f1#

这就是我所做的

CallDB = aoracandlestudiodb.cursor()
        CallDB.execute('SELECT * FROM items')
        items_ID3 = CallDB.fetchall()
        print(items_ID3)
        i = 1
        distance = 0
        for item in items_ID3:
            get_name = get_NameOfItem(i)
            get_ID = get_IDOfItem(i)
            self.MakeButtonsForItems(get_ID, get_name, distance)

            i = i + 1
            distance = distance + 40

    def MakeButtonsForItems(self, buttonid, buttonname, distance):
        ButtonVariable = buttonid
        buttonGenerator = ctk.CTkButton(self, text=buttonname, width=200, height=20,
                                        fg_color='grey',
                                        bg_color='white',
                                        corner_radius=5,
                                        text_color='black', font=("Arial bold", 20),
                                        command=lambda: self.genaratebuttoncommand0(ButtonVariable))
        buttonGenerator.place(x=80, y=distance)

    def genaratebuttoncommand0(self, Item_id):
        ...

并制作了这些函数来从sql数据库中获取信息

def get_IDOfItem(VarForPick):
    CallDB = aoracandlestudiodb.cursor()

    query = 'SELECT iditems FROM items WHERE iditems= %s '
    CallDB.execute(query, (VarForPick,))

    stage_get = CallDB.fetchall()
    stage_set = str(stage_get)
    Item_ID = stage_set[2:-3]
    return int(Item_ID)

def get_NameOfItem(ID_item):
    CallDB = aoracandlestudiodb.cursor()

    query = 'SELECT _name FROM items WHERE iditems= %s '
    CallDB.execute(query, (ID_item,))

    stage_get = CallDB.fetchall()
    stage_set = str(stage_get)
    Item_Name = stage_set[3:-4]
    return str(Item_Name)

def get_PriceOfItem(Price_item):
    CallDB = aoracandlestudiodb.cursor()

    query = 'SELECT _price FROM items WHERE iditems= %s '
    CallDB.execute(query, (Price_item,))
    stage_get = CallDB.fetchall()
    stage_set = str(stage_get)
    Item_ID = stage_set[2:-3]
    return float(Item_ID)

def get_CostOFItem(Cost_item):
    CallDB = aoracandlestudiodb.cursor()

    query = 'SELECT _cost FROM items WHERE iditems= %s '
    CallDB.execute(query, (Cost_item,))
    stage_get = CallDB.fetchall()
    stage_set = str(stage_get)
    Item_Cost = stage_set[2:-3]
    return float(Item_Cost)

def get_CategoryOfItem(Category_item):
    CallDB = aoracandlestudiodb.cursor()

    query = 'SELECT _category FROM items WHERE iditems= %s '
    CallDB.execute(query, (Category_item,))
    stage_get = CallDB.fetchall()
    stage_set = str(stage_get)
    Item_Category = stage_set[3:-4]
    return str(Item_Category)

def get_StockOfItem(Stock_item):
    CallDB = aoracandlestudiodb.cursor()

    query = 'SELECT _stock FROM items WHERE iditems= %s '
    CallDB.execute(query, (Stock_item,))
    stage_get = CallDB.fetchall()
    stage_set = str(stage_get)
    Item_Stock = stage_set[2:-3]
    return int(Item_Stock)

相关问题