如何创建允许用户根据列(python)中的值过滤树视图的按钮或选项? [英] How to create a button or option which allows a user to filter a treeview based on a value in a column (python)?

查看:20
本文介绍了如何创建允许用户根据列(python)中的值过滤树视图的按钮或选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 GUI,其中根据下拉菜单中的货币选择显示交易量数据.显示的数据框类似于下面的示例.数据框使用 Treeview 显示.我想为用户创建选项来过滤它.要加载主框架,下拉列表包含主货币欧元在此示例中,因此在选择所有货币对的卷时,显示欧元的所有货币对.一旦树视图打印在屏幕上,我想为用户提供选择特定货币对的选项,并将视图过滤到它.我读到关注一棵树,但它似乎只适用于一行而不是整个块.实现我的目标最好的方法是什么?

I have created a GUI where volume data is being displayed based on a currency selection from a drop down menu. The displayed dataframe looks like the example below. The dataframe is displayed using a Treeview. I would like to create the option for the user to filter it down. To load the main frame the dropdown contains the main currency EUR in this example so when it is selected the volume for all the currency pairs against EUR is displayed. I would like to give the user the option to select a specific currency pair once the treeview is printed on the screen and filter the view down to it. I read about focusing on a tree but it seems to work for one row only rather then an entire block. What would be the best thing to use to achieve my goal?

from tkinter import *
import pandas as pd


class Application(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.grid()
        self.master.title("Volume")

        for c in range(2, 7):
            self.master.rowconfigure(c, weight=1)

        for c in range(8):
            self.master.columnconfigure(c, weight=1)

        self.Frame1 = Frame(master, bg="blue")
        self.Frame1.grid(row=0, column=0, rowspan=1, columnspan=8, sticky=W + E + N + S)

        self.Frame2 = Frame(master, bg="lightblue")
        self.Frame2.grid(row=1, column=0, rowspan=1, columnspan=8, sticky=W + E + N + S)

        self.Frame3 = Frame(master, bg="white")
        self.Frame3.grid(row=2, column=0, rowspan=5, columnspan=8, sticky=W + E + N + S)

        self.Frame4 = Frame(master, bg="blue")
        self.Frame4.grid(row=7, column=0, rowspan=1, columnspan=8, sticky=W + E + N + S)

        label_title = Label(
            self.Frame1, text="Volume display", font=("Times New Roman", 30)
        )
        label_title.grid(row=0, column=1, padx=750)

        # drop down for currency
        label_option = Label(
            self.Frame2, text="Currency", font=("Times New Roman", 17, "bold")
        )
        label_option.grid(row=0, column=4)
        currencies = sorted(["USD", "GBP", "CAD", "EUR"])
        self.currency = StringVar(root)
        self.currency.set("EUR")
        option = OptionMenu(self.Frame2, self.currency, *currencies)
        option.grid(row=0, column=5)
        option["menu"].config(bg="white")
        option.config(font=("Times New Roman", 17), bg="white")

        # print df for currency
        self.Load_Df = Button(
            self.Frame4, text="Display Volume", command=self.load_data
        )
        self.Load_Df.config(font=("Times New Roman", 17), bg="white")
        self.Load_Df.grid(row=0, column=2, columnspan=2, ipadx=15)

        self.tree = ttk.Treeview(self.Frame3)

    def load_data(self):
        currency = self.currency.get()
        file_name = "D:/" + currency + "/volume.xlsx"
        final_df = pd.read_excel(file_name)

        self.clear_table()
        columns = list(final_df.columns)
        self.tree["columns"] = columns
        self.tree.pack(expand=TRUE, fill=BOTH)

        for i in columns:
            self.tree.column(i, anchor="w")
            self.tree.heading(i, text=i, anchor="w")

        for index, row in final_df.iterrows():
            self.tree.insert("", "end", text=index, values=list(row))

    def clear_table(self):
        for i in self.tree.get_children():
            self.tree.delete(i)


root = Tk()
app = Application(master=root)
app.mainloop()

推荐答案

您可以为用户创建一个选择框并仅显示过滤后的结果.以下是基于您的代码使用 ttk.Combobox 的示例:

You can create a selection box for the user and display only the filtered results. Below is a sample utilizing ttk.Combobox base on your code:

from tkinter import *
import pandas as pd
from tkinter import ttk

df = pd.DataFrame({"Currency":["EUR","GBR","CAD","EUR"],
                   "Volumne":[100,200,300,400]})

class Application(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.title("Volume")

        self.tree = ttk.Treeview(self)
        columns = list(df.columns)
        self.combo = ttk.Combobox(self, values=list(df["Currency"].unique()),state="readonly")
        self.combo.pack()
        self.combo.bind("<<ComboboxSelected>>", self.select_currency)
        self.tree["columns"] = columns
        self.tree.pack(expand=TRUE, fill=BOTH)

        for i in columns:
            self.tree.column(i, anchor="w")
            self.tree.heading(i, text=i, anchor="w")

        for index, row in df.iterrows():
            self.tree.insert("", "end", text=index, values=list(row))

    def select_currency(self,event=None):
        self.tree.delete(*self.tree.get_children())
        for index, row in df.loc[df["Currency"].eq(self.combo.get())].iterrows():
            self.tree.insert("", "end", text=index, values=list(row))

root = Application()
root.mainloop()

这篇关于如何创建允许用户根据列(python)中的值过滤树视图的按钮或选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆