我正在制作一个程序,该程序使用PyMySQL模块获取数据库记录,然后在Tkinter Entries中显示这些记录。我已经有执行此操作的代码,但是我想添加一个功能。
我想为用户提供对行的条目进行 doubleclick 的功能,并获取该行的所有Entry文本并将其解析为我在For循环之前声明的文本框中(UserID,First_Name等等)。 假设我每行有5个条目。例如,如果用户双击在第1行的条目2上,我想从第1行的所有条目中获取文本并将其分别解析到文本框中。
这是我当前拥有的代码:
window = tk.Tk()
window.title("Flexbase") #Give window a name
window.update_idletasks()
width = 640
height = 480
x = (window.winfo_screenwidth() // 2) - (width // 2)
y = (window.winfo_screenheight() // 2) - (height // 2)
window.geometry('+{}+{}'.format(x, y))
GetRows_StringVariable = "SELECT * FROM SEC_USER"
cursor.execute(GetRows_StringVariable)
results = cursor.fetchall()
RowCounter = 1
ColumnCounter = -1
ListCounter = 0
RowListCounter = 0
rows = []
id_list = []
firstname_list = []
surname_list = []
username_list = []
email_list = []
password_list = []
tk.Label(window, text="Testing").grid(row=0, column=0, sticky=NSEW)
UserID_TextBox = tk.Entry(window, width=20, bg="white") #Create text box
UserID_TextBox.grid(row=1, column=0, sticky=W) #Give the text box a position in the window
FirstName_TextBox = tk.Entry(window, width=60, bg="white")
FirstName_TextBox.grid(row=2, column=0, sticky=W)
Surname_TextBox = tk.Entry(window, width=60, bg="white")
Surname_TextBox.grid(row=3, column=0, sticky=W)
Email_TextBox = tk.Entry(window, width=60, bg="white")
Email_TextBox.grid(row=4, column=0, sticky=W)
Username_TextBox = tk.Entry(window, width=20, bg="white")
Username_TextBox.grid(row=5, column=0, sticky=W)
Password_TextBox = tk.Entry(window, show="*", width=20, bg="white")
Password_TextBox.grid(row=6, column=0, sticky=W)
notebook = ttk.Notebook(window)
notebook.grid(row=7, column=0, sticky=NSEW)
page1 = ttk.Frame(notebook)
notebook.add(page1, text="Mass Editing Mode")
tk.Label(page1, text=SQL_ID, fg="black", font="Arial 8 bold").grid(row=0, column=0, sticky=W)
tk.Label(page1, text=SQL_FIRSTNAME, fg="black", font="Arial 8 bold").grid(row=0, column=1, sticky=W)
tk.Label(page1, text=SQL_SURNAME, fg="black", font="Arial 8 bold").grid(row=0, column=2, sticky=W)
tk.Label(page1, text=SQL_USERNAME, fg="black", font="Arial 8 bold").grid(row=0, column=3, sticky=W)
tk.Label(page1, text=SQL_EMAIL, fg="black", font="Arial 8 bold").grid(row=0, column=4, sticky=W)
for row in results: # Page 1 TAB
ColumnCounter += 1
id_list.append(row[SQL_ID])
a = tk.Entry(page1, fg="black", font="Arial 8 bold")
a.grid(row=RowCounter, column=ColumnCounter, sticky=W)
a.insert(0, row[SQL_ID])
ColumnCounter += 1
firstname_list.append(row[SQL_FIRSTNAME])
b = tk.Entry(page1, fg="black", font="Arial 8 bold")
b.grid(row=RowCounter,column=ColumnCounter,sticky=W)
b.insert(0, row[SQL_FIRSTNAME])
ColumnCounter += 1
surname_list.append(row[SQL_SURNAME])
c = tk.Entry(page1, fg="black", font="Arial 8 bold")
c.grid(row=RowCounter,column=ColumnCounter,sticky=W)
c.insert(0, row[SQL_SURNAME])
ColumnCounter += 1
username_list.append(row[SQL_USERNAME])
d = tk.Entry(page1, fg="black", font="Arial 8 bold")
d.grid(row=RowCounter, column=ColumnCounter, sticky=W)
d.insert(0, row[SQL_USERNAME])
ColumnCounter += 1
email_list.append(row[SQL_EMAIL])
e = tk.Entry(page1, fg="black", font="Arial 8 bold")
e.grid(row=RowCounter, column=ColumnCounter, sticky=W)
e.insert(0, row[SQL_EMAIL])
RowCounter += 1
ColumnCounter = -1
ListCounter += 1
RowListCounter += 1
此代码产生一个窗口,每行有5个条目。
答案 0 :(得分:0)
您可以在创建tk.Entry
对象之后添加此内容:
for obj in (a, b, c, d):
def double_click(ev, RowCounter=RowCounter):
print("selected row:", RowCounter)
obj.bind("<Double-Button>", double_click)
(需要{RowCounter=RowCounter
来捕获当前行值。)
有了行号,您应该能够从results
对象获得所需的内容。
答案 1 :(得分:0)
正如@Florian Weimer所解释的那样,您需要定义一个或多个回调函数,并将其附加在每个Entry
上。
除此之外,我认为您可以(并且应该)消除所拥有的多余代码,这将使其变得更小,更容易进行工作和增强。在软件开发中,这被称为DRY,因为“不要重复自己”的原则。
由于我没有您的数据库,因此我必须添加以下行:
db_keys = results[0].keys() # Get list of keys from the first result.
以获得每个记录/行中所需的键列表。您的数据库架构中可能已经有这样的列表,或者希望以其他方式创建它。
import tkinter as tk
from tkinter import ttk
from tkinter.constants import *
window = tk.Tk()
window.title("Flexbase") #Give window entry name
window.update_idletasks()
width = 640
height = 480
x = (window.winfo_screenwidth() // 2) - (width // 2)
y = (window.winfo_screenheight() // 2) - (height // 2)
window.geometry('+{}+{}'.format(x, y))
GetRows_StringVariable = "SELECT * FROM SEC_USER"
#cursor.execute(GetRows_StringVariable)
#results = cursor.fetchall()
db_keys = results[0].keys() # Get list of keys from the first result.
tk.Label(window, text="Testing").grid(row=0, column=0, sticky=NSEW)
# Create textbox Entry widgets.
textbox_ids = list(db_keys) + ['Password']
textbox_widths = 20, 60, 60, 60, 20, 20
textbox_shows = '', '', '', '', '', '*'
textboxes = {}
for i, (id, width, show) in enumerate(zip(textbox_ids, textbox_widths, textbox_shows), 1):
textboxes[id] = tk.Entry(window, width=width, show=show, bg="white")
textboxes[id].grid(row=i, column=0, sticky=W)
# Double-click callback function for items in result rows.
def double_click_cb(ev, row):
# Replace current values in textboxes with those from the selected row.
for key, value in row.items():
textboxes[key].delete(0, END)
textboxes[key].insert(0, value)
notebook = ttk.Notebook(window)
notebook.grid(row=7, column=0, sticky=NSEW)
page1 = ttk.Frame(notebook)
notebook.add(page1, text="Mass Editing Mode")
nb_font = "Arial 8 bold"
# Create header row.
for i, key in enumerate(db_keys):
tk.Label(page1, text=key, fg="black", font=nb_font).grid(row=0, column=i, sticky=W)
# Display results.
id_list = []
RowCounter = 1
for row in results:
for ColumnCounter, key in enumerate(db_keys):
id_list.append(row[key])
entry = tk.Entry(page1, fg="black", font=nb_font)
entry.grid(row=RowCounter, column=ColumnCounter, sticky=W)
entry.insert(0, row[key])
entry.bind("<Double-Button>", lambda e, r=row: double_click_cb(e, r))
RowCounter += 1
window.mainloop()