我正在制作一个程序,它用PyMySQL模块获取数据库记录,然后我想在Tkinter条目中显示这些记录。我已经有了这样做的代码,但我想添加一个功能。在
我想给用户提供双击行的一个条目的功能,获取该行的所有条目文本,并将它们解析到我在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个条目的窗口。在
您可以在创建
tk.Entry
对象之后添加:(需要
RowCounter=RowCounter
来捕获当前行值。)使用行号,您应该能够从
results
对象获得所需的内容。在正如@florianweimer已经解释过的,您需要定义一个或多个回调函数,并将它/它们附加到每个
Entry
。在除此之外,我认为您可以(也应该)消除尽可能多的冗余代码,这将使代码更小、更易于处理和增强。在软件开发中,这被称为DRY,意思是“不要重复你自己”的原则。在
因为我没有你的数据库,我不得不加上一行:
获取每个记录/行中所需的键列表。您的数据库架构中可能已经有这样的列表,或者希望以不同的方式创建它。在
^{pr2}$相关问题 更多 >
编程相关推荐