我的代码实际上是这样的:更多代码
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.内部错误:找到未读结果“
1条答案
按热度按时间v8wbuo2f1#
这就是我所做的
并制作了这些函数来从sql数据库中获取信息