Python Tkinter - 用于过滤数据集的 2 个组合框 - 第一个组合框列出状态,第二个组合框仅列出该状态下的设施

Python Tkinter - 2 combo boxes to filter dataset - first combobox lists states, second combobox lists only facilities in that state

Tkinter 的新手...我正在尝试在 Tkinter 中制作一个迷你应用程序,用户可以在其中选择一个组合框中的医疗设施状态,并在第二个组合框中选择该状态的设施的具体名称。现在,我已经能够让我的数据框过滤到状态(第一个组合框)中选择的值,但不是设施。我只希望先前选择的状态的设施出现在第二个组合框中。目前所有的设施都出现在第二个组合框中,不管组合框 1 中选择的状态如何。我知道我的代码是错误的,但我试图表明我正在尝试这里。

举个例子,如果我在组合框 1 中选择 'oh' 作为状态,我只希望 'acme facility' 和“123 healthcare”出现在组合框 2 中。从那里,如果我选择“123 医疗保健”,我的数据框将只包含俄亥俄州名为“123 医疗保健”的设施。

基本上,如果我只是在 pandas 中工作(但我需要在 Tkinter 中进行)和我的 IDE 命令,我希望它这样做:

newdf = df.loc[((df['state'] == combobox1value) & (df['provname'] == combobox2value))]

感谢任何帮助。

import pandas as pd
from tkinter import ttk
import numpy as np
from tkinter import Tk
import tkinter as tk

# function to get unique values
def unique(list1):
    x = np.array(list1)
    print(np.unique(x))


def on_click():
    val = n.get()
    if val == 'all':
        print(df)
    else:        
        df2 = df[ df['state'] == val ]
        print(df2)
        
def on_click2():
    val = n2.get()
    if val == 'all':
        print(df)
    else:
        df2 = df[ df['provname'] == val ]
        print(df2)
        df2.to_excel('test.xlsx')
        

df = pd.DataFrame({
    'logdate': ['1/1/2020','1/2/2022', '1/3/2022', '3/3/2021','5/13/2021','10/11/2019','1/5/2020'],
    'state': ['oh','oh', 'oh', 'ar','mi','ma','ms'],
    'provname': ['acme facility','123 healthcare', '123 healthcare','basic clinic','healthcare solutions','best clinic','one stop clinic'],
})

values = ['all'] + list(df['state'].unique())

dfx = df.loc[((df['state'] == 'oh') & (df['provname'] == '123 healthcare'))]
print(dfx)

# Creating tkinter window and set dimensions
window = tk.Tk() 
window.title('Combobox') 
window.geometry('500x250') 
  
# label text for title 
ttk.Label(window, text = "Main Menu",  
          background = 'cyan', foreground ="black",  
          font = ("Times New Roman", 15)).grid(row = 0, column = 1) 


#FILTER STATE - PART 1
# Set label 
ttk.Label(window, text = "Select the State :", 
          font = ("Times New Roman", 12)).grid(column = 0, 
          row = 5, padx = 6, pady = 25)


# Create Combobox
n = tk.StringVar() 
state = ttk.Combobox(window, width = 27, textvariable = n) 
  
# Adding combobox drop down list 
state['values'] = list(df['state'].unique())
state.grid(column = 1, row = 5)

state.current()

#FILTER FACILITY - PART 2

# Set label 
ttk.Label(window, text = "Select the Facility :", 
          font = ("Times New Roman", 12)).grid(column = 0, 
          row = 6, padx = 6, pady = 25)


ttk.Button(window, text = "OK", command=on_click).grid(column = 2, 
          row = 6, padx = 5, pady = 25)

# Create Combobox
n2 = tk.StringVar() 
prov = ttk.Combobox(window, width = 27, textvariable = n2) 
  
# Adding combobox drop down list 
prov['values'] = list(df['provname'].unique())
  
prov.grid(column = 1, row = 6)
prov.current()
window.mainloop() 



当您 select 您的州时,您需要更新第二个组合框中的值。为此,您需要将第一个组合框中的 "<<ComboboxSelected>>" 事件与更改第二个组合框值的函数绑定。

# Function for when first combobx selected
def state_selected(event):
    state_value = state.get()
    facilities = list(df.loc[df['state']==state_value]['provname'].unique())
    if facilities:
        prov['values']=facilities
    else:
        prov['values']=""
# Bind for when your first combobox is selected
state.bind("<<ComboboxSelected>>", state_selected)

在您的脚本中插入上面的代码片段可以得到:

import pandas as pd
from tkinter import ttk
import numpy as np
from tkinter import Tk
import tkinter as tk

# function to get unique values
def unique(list1):
    x = np.array(list1)
    print(np.unique(x))


def on_click():
    val = n.get()
    if val == 'all':
        print(df)
    else:        
        df2 = df[ df['state'] == val ]
        print(df2)
        
def on_click2():
    val = n2.get()
    if val == 'all':
        print(df)
    else:
        df2 = df[ df['provname'] == val ]
        print(df2)
        df2.to_excel('test.xlsx')
        

df = pd.DataFrame({
    'logdate': ['1/1/2020','1/2/2022', '1/3/2022', '3/3/2021','5/13/2021','10/11/2019','1/5/2020'],
    'state': ['oh','oh', 'oh', 'ar','mi','ma','ms'],
    'provname': ['acme facility','123 healthcare', '123 healthcare','basic clinic','healthcare solutions','best clinic','one stop clinic'],
})

values = ['all'] + list(df['state'].unique())

dfx = df.loc[((df['state'] == 'oh') & (df['provname'] == '123 healthcare'))]
print(dfx)

# Creating tkinter window and set dimensions
window = tk.Tk() 
window.title('Combobox') 
window.geometry('500x250') 
  
# label text for title 
ttk.Label(window, text = "Main Menu",  
          background = 'cyan', foreground ="black",  
          font = ("Times New Roman", 15)).grid(row = 0, column = 1) 


#FILTER STATE - PART 1
# Set label 
ttk.Label(window, text = "Select the State :", 
          font = ("Times New Roman", 12)).grid(column = 0, 
          row = 5, padx = 6, pady = 25)


# Create Combobox
n = tk.StringVar() 
state = ttk.Combobox(window, width = 27, textvariable = n) 
  
# Adding combobox drop down list 
state['values'] = list(df['state'].unique())
state.grid(column = 1, row = 5)

state.current()

#FILTER FACILITY - PART 2

# Set label 
ttk.Label(window, text = "Select the Facility :", 
          font = ("Times New Roman", 12)).grid(column = 0, 
          row = 6, padx = 6, pady = 25)


ttk.Button(window, text = "OK", command=on_click).grid(column = 2, 
          row = 6, padx = 5, pady = 25)

# Create Combobox
n2 = tk.StringVar() 
prov = ttk.Combobox(window, width = 27, textvariable = n2) 
  
# Function for when first combobx selected
def state_selected(event):
    state_value = state.get()
    facilities = list(df.loc[df['state']==state_value]['provname'].unique())
    if facilities:
        prov['values']=facilities
    else:
        prov['values']=""
# Bind for when your first combobox is selected
state.bind("<<ComboboxSelected>>", state_selected)

prov.grid(column = 1, row = 6)
prov.current()
window.mainloop()

关于您的代码的更多评论:

  • prov.current() 不会做任何事情,因为你还没有 selected 索引
  • 你不需要 from tkinter import Tk 因为你已经导入了 tkinter