我有一个拖放列表框(found here),可让用户向上或向下拖动/移动项目以重新排序。
我的db.sqlite
文件最初看起来像这样:
>>> make_df()
ordering fruit
0 0 apples
1 1 oranges
2 2 blueberries
3 3 watermelon
4 4 cantaloupe
5 5 pears
6 6 pomegranate
7 7 raspberries
8 8 blackberries
9 9 boysenberries
10 10 nectarines
我要做的是重新排序列表框中的项目并将该订单保存到数据库中,这样当我重新启动程序时,这些项目的顺序与我上次使用它时的顺序相同。
问题:
每次重新启动程序时,列表框中的项目始终处于相同的顺序。但是,当我重新排序项目时,运行查看数据库的make_df
以查看排序并返回数据帧,它会在该会话期间显示数据库更新。 它不是永久保存它,而是我无法弄清楚的。
import tkinter as tk
import pandas as pd
import sqlite3
root = tk.Tk()
def make_sqlite_db_for_stackoverflow():
connection = sqlite3.connect('db.sqlite')
db_file_name = 'db.sqlite'
df = pd.DataFrame(
[(0, 'apples'), (1, 'oranges'), (2, 'blueberries'), (3, 'watermelon'),
(4, 'cantaloupe'), (5, 'pears'), (6, 'pomegranate'), (7, 'raspberries'),
(8, 'blackberries'), (9, 'boysenberries'), (10, 'nectarines')],
columns=['ordering', 'fruit']
)
df.to_sql('columns', connection, index=False, if_exists='replace')
connection.close()
make_sqlite_db_for_stackoverflow()
def make_df():
print(pd.DataFrame(sqlite3.connect('db.sqlite').cursor().execute(
'SELECT * FROM columns ORDER BY "ordering";').fetchall(), columns=['ordering', 'fruit']))
class Drag_and_Drop_Listbox(tk.Listbox):
""" A tk listbox with drag'n'drop reordering of entries. """
def __init__(self, master, **kw):
kw['selectmode'] = tk.EXTENDED
tk.Listbox.__init__(self, master, kw)
self.bind('<Button-1>', self.setCurrent)
self.bind('<B1-Motion>', self.shiftSelection)
self.curIndex = None
def setCurrent(self, event):
self.curIndex = self.nearest(event.y)
def shiftSelection(self, event):
i = self.nearest(event.y)
if i < self.curIndex:
x = self.get(i)
self.delete(i)
self.insert(i+1, x)
self.curIndex = i
elif i > self.curIndex:
x = self.get(i)
self.delete(i)
self.insert(i-1, x)
self.curIndex = i
def update_ordering(*args):
connect = sqlite3.connect('db.sqlite')
cursor = connect.cursor()
field_ordering = [(order,fruit) for order,fruit in enumerate(ddlistbox.get(0, 'end'))]
print(field_ordering)
for field in field_ordering:
cursor.execute("UPDATE columns SET 'ordering'="+str(field[0])+" WHERE fruit='"+field[1]+"';")
connect.commit()
connect.close()
print(ddlistbox.curselection())
scrollbar = tk.Scrollbar(root, orient="vertical")
ddlistbox = Drag_and_Drop_Listbox(root, yscrollcommand=scrollbar.set)
scrollbar.grid(row=0, column=1, sticky='ns')
scrollbar.config(command=ddlistbox.yview)
def get_sqlite_report_fields():
conn = sqlite3.connect('db.sqlite')
conn.row_factory = lambda cursor, row: row[0]
cursor = conn.cursor()
fetch = cursor.execute("SELECT fruit FROM columns ORDER BY 'ordering';").fetchall()
conn.close()
return fetch
for fruit in get_sqlite_report_fields():
ddlistbox.insert(0, fruit)
ddlistbox.config(width=30)
ddlistbox.bind('<Double-Button-1>' , func=update_ordering)
ddlistbox.grid(row=0, column=0)
button = tk.Button(root, text='Check', command=update_ordering)
button.grid(row=1, column=0)
root.mainloop()
答案 0 :(得分:1)
非常怀疑任何人都会回答我的问题,所以我发布了我的解决方案。它使用拖放列表框允许用户在列表框中排列项目,并将其项目排序保存到数据库中以供下次使用。
<form method="POST" action="index.php">
<input type="button" class="btn btn-danger" name="Delete" value="Delete"/>
<table class="table table-striped table-hover table-responsive" id="inventoryTable">
<thead>
<tr>
<td>Serial ID</td>
<td>Name</td>
<td>Manufacturer</td>
<td>Keys</td>
<td>Description</td>
<td>Category</td>
<td>Block</td>
<td>Floor</td>
<td>Room</td>
</tr>
</thead>
<tbody>
<?php while($row = mysqli_fetch_array($allresult)) { ?>
<tr>
<td><input name="checkbox[]" type="checkbox" value="<?php echo $row['serialid']; ?>"></td>
<td><?php echo $row['serialid']?></td>
<td><?php echo $row['name'] ?></td>
<td><?php echo $row['manufacturer'] ?></td>
<td><?php echo $row['licensekeys'] ?></td>
<td><?php echo $row['description'] ?></td>
<td><?php echo $row['categoryname'] ?></td>
<td><?php echo $row['block'] ?></td>
<td><?php echo $row['floor'] ?></td>
<td><?php echo $row['room'] ?></td>
</tr>
<?php }?>
</tbody>
</table>
</form>