在 tkinter 的树视图中选择多行并同时获取它们
Selecting multiple rows and fetching them simultaneously in treeview in tkinter
如何在树视图中 select 多行,树视图从 MySQL 数据库中提取数据,以便我可以在文本框中显示它们。我可以一次为一行执行此操作,但不确定如何 select 多个条目。我的 GUI 中基本上有 2 个框架,左框架和右框架。左框架显示带有动物名称的树视图,右框架显示文本框。当有人 select 输入动物名称并点击 添加 按钮时,它应该被添加到右侧的文本框中。只想弄清楚如何对多个 select 离子执行此操作。以下是我目前的代码。
from tkinter import *
from tkinter import ttk
import pymysql
class Animals_App:
def __init__(self, root):
self.root = root
self.root.title("Animals")
self.root.geometry("1350x750+0+0")
self.root.resizable(FALSE, FALSE)
title = Label(self.root, text="Animals", font=("times new roman", 30, "bold"), bg="#262626",
fg="white").place(x=0, y=0, relwidth=1)
self.root.config(background="powder blue")
# =====Variables========
self.animal_var = StringVar()
# ==For displaying added Animal names on the right text box
def display_name():
txt_box.insert(END, 'Animal : ' + self.animal_var.get() + '\n')
# ==========Frame 1 (Left Frame)===========
Top_Frame = Frame(self.root, bd=5, relief=RIDGE, bg="white")
Top_Frame.place(x=10, y=50, width=750, height=620)
Addbtn = Button(Top_Frame, padx=16, pady=1, bd=7, fg='black', font=('arial', 16, 'bold'), width=4,
text='Add', bg='powder blue', command=display_name).grid(row=0, column=0)
Table_Frame = Frame(self.root, bd=5, relief=RIDGE, bg="white")
Table_Frame.place(x=10, y=130, width=750, height=620)
scroll_x = Scrollbar(Table_Frame, orient=HORIZONTAL)
scroll_y = Scrollbar(Table_Frame, orient=VERTICAL)
self.Animals_table = ttk.Treeview(Table_Frame,
columns=("animal"),
xscrollcommand=scroll_x.set, yscrollcommand=scroll_y.set)
scroll_x.pack(side=BOTTOM, fill=X)
scroll_y.pack(side=RIGHT, fill=Y)
scroll_x.config(command=self.Animals_table.xview)
scroll_y.config(command=self.Animals_table.yview)
self.Animals_table.heading("animal", text="Animal Names")
self.Animals_table['show'] = 'headings'
self.Animals_table.column("animal", width=100)
self.Animals_table.pack(fill=BOTH, expand=1)
self.Animals_table.bind("<ButtonRelease-1>", self.get_cursor)
self.fetch_data()
# ==========Frame 2 (Right Frame)===========
Txt_Frame = Frame(self.root, bd=5, relief=RIDGE, bg="white")
Txt_Frame.place(x=770, y=70, width=580, height=620)
scroll_y = Scrollbar(Txt_Frame, orient=VERTICAL)
scroll_y.pack(fill=Y, side=RIGHT)
txt_box = Text(Txt_Frame, font=("times new roman", 15), bg="lightyellow", fg="black",
yscrollcommand=scroll_y.set)
txt_box.pack(fill=BOTH, expand=1)
scroll_y.config(command=txt_box.yview)
# txt_box.insert(END, 'Animal : ' + '\n')
def fetch_data(self):
con = pymysql.connect(host="localhost", user="root", password="", database="animaltree")
cur = con.cursor()
cur.execute("select * from animals")
rows = cur.fetchall()
# rows=["Cow","Deer","Dog","Zebra"]
if len(rows) != 0:
self.Animals_table.delete(*self.Animals_table.get_children())
for row in rows:
self.Animals_table.insert('', END, values=row)
con.commit()
con.close()
def get_cursor(self, ev):
cursor_row = self.Animals_table.focus()
contents = self.Animals_table.item(cursor_row)
row = contents['values']
self.animal_var.set(row[0])
root = Tk()
obj = Animals_App(root)
root.mainloop()
Treeview
的默认选择模式已经是多重模式。您需要使用 .selection()
而不是 .focus()
来获取所选项目。
为了保存选择,将 self.animal_var
从 StringVar()
更改为 Variable()
并更新 display_name()
和 self.get_cursor()
如下:
class Animals_App:
def __init__(self, root):
...
self.animal_var = Variable() # changed from StringVar()
def display_name():
txt_box.insert(END, 'Animal : ' + ', '.join(self.animal_var.get()) + '\n')
...
def get_cursor(self, ev):
self.animal_var.set([ev.widget.item(idx)['values'][0] for idx in ev.widget.selection()])
如何在树视图中 select 多行,树视图从 MySQL 数据库中提取数据,以便我可以在文本框中显示它们。我可以一次为一行执行此操作,但不确定如何 select 多个条目。我的 GUI 中基本上有 2 个框架,左框架和右框架。左框架显示带有动物名称的树视图,右框架显示文本框。当有人 select 输入动物名称并点击 添加 按钮时,它应该被添加到右侧的文本框中。只想弄清楚如何对多个 select 离子执行此操作。以下是我目前的代码。
from tkinter import *
from tkinter import ttk
import pymysql
class Animals_App:
def __init__(self, root):
self.root = root
self.root.title("Animals")
self.root.geometry("1350x750+0+0")
self.root.resizable(FALSE, FALSE)
title = Label(self.root, text="Animals", font=("times new roman", 30, "bold"), bg="#262626",
fg="white").place(x=0, y=0, relwidth=1)
self.root.config(background="powder blue")
# =====Variables========
self.animal_var = StringVar()
# ==For displaying added Animal names on the right text box
def display_name():
txt_box.insert(END, 'Animal : ' + self.animal_var.get() + '\n')
# ==========Frame 1 (Left Frame)===========
Top_Frame = Frame(self.root, bd=5, relief=RIDGE, bg="white")
Top_Frame.place(x=10, y=50, width=750, height=620)
Addbtn = Button(Top_Frame, padx=16, pady=1, bd=7, fg='black', font=('arial', 16, 'bold'), width=4,
text='Add', bg='powder blue', command=display_name).grid(row=0, column=0)
Table_Frame = Frame(self.root, bd=5, relief=RIDGE, bg="white")
Table_Frame.place(x=10, y=130, width=750, height=620)
scroll_x = Scrollbar(Table_Frame, orient=HORIZONTAL)
scroll_y = Scrollbar(Table_Frame, orient=VERTICAL)
self.Animals_table = ttk.Treeview(Table_Frame,
columns=("animal"),
xscrollcommand=scroll_x.set, yscrollcommand=scroll_y.set)
scroll_x.pack(side=BOTTOM, fill=X)
scroll_y.pack(side=RIGHT, fill=Y)
scroll_x.config(command=self.Animals_table.xview)
scroll_y.config(command=self.Animals_table.yview)
self.Animals_table.heading("animal", text="Animal Names")
self.Animals_table['show'] = 'headings'
self.Animals_table.column("animal", width=100)
self.Animals_table.pack(fill=BOTH, expand=1)
self.Animals_table.bind("<ButtonRelease-1>", self.get_cursor)
self.fetch_data()
# ==========Frame 2 (Right Frame)===========
Txt_Frame = Frame(self.root, bd=5, relief=RIDGE, bg="white")
Txt_Frame.place(x=770, y=70, width=580, height=620)
scroll_y = Scrollbar(Txt_Frame, orient=VERTICAL)
scroll_y.pack(fill=Y, side=RIGHT)
txt_box = Text(Txt_Frame, font=("times new roman", 15), bg="lightyellow", fg="black",
yscrollcommand=scroll_y.set)
txt_box.pack(fill=BOTH, expand=1)
scroll_y.config(command=txt_box.yview)
# txt_box.insert(END, 'Animal : ' + '\n')
def fetch_data(self):
con = pymysql.connect(host="localhost", user="root", password="", database="animaltree")
cur = con.cursor()
cur.execute("select * from animals")
rows = cur.fetchall()
# rows=["Cow","Deer","Dog","Zebra"]
if len(rows) != 0:
self.Animals_table.delete(*self.Animals_table.get_children())
for row in rows:
self.Animals_table.insert('', END, values=row)
con.commit()
con.close()
def get_cursor(self, ev):
cursor_row = self.Animals_table.focus()
contents = self.Animals_table.item(cursor_row)
row = contents['values']
self.animal_var.set(row[0])
root = Tk()
obj = Animals_App(root)
root.mainloop()
Treeview
的默认选择模式已经是多重模式。您需要使用 .selection()
而不是 .focus()
来获取所选项目。
为了保存选择,将 self.animal_var
从 StringVar()
更改为 Variable()
并更新 display_name()
和 self.get_cursor()
如下:
class Animals_App:
def __init__(self, root):
...
self.animal_var = Variable() # changed from StringVar()
def display_name():
txt_box.insert(END, 'Animal : ' + ', '.join(self.animal_var.get()) + '\n')
...
def get_cursor(self, ev):
self.animal_var.set([ev.widget.item(idx)['values'][0] for idx in ev.widget.selection()])