I have two functions that produce two types of output one is a data frame table and the other is a plot of that dataframe. All functions take one file as input. which we load from the previous tkinter function. I would like to dynamically select a function from the radio box, next, once we select a particular function it should show a blank box that will ask for input from a user and based on input the function will get executed.
from tkinter import ttk
import tkinter as tk
root= Tk()
root.geometry("800X600")
root.config(bg='light blue')
root.title('Dashboard')
frame = tk.Frame(root,bg='light blue')
frame.pack(padx=10,pady=10)
file_label = tk.Label(frame,text='Input_File')
file_label.grid(row=0,colulmn=0)
def function_1(Input_File,Var1,Var2):
# Table and Graph will be based on the above input.
def function_2(Input_File,Var3,Var4,Var5,Var6):
# Table and Graph will be based on the above input.
root.mainloop()
Once we select function_1 from the radio box, then immediately we should get two boxes next to the radio box which will ask for "Var1" and "Var2". If we select function_2 then we should get four boxes next to the radio box which will ask for "Var3", "Var4", "Var5", and "Var6".
Once all the input is received we should process the respective function and below we should get two outputs first "dataframe" table produced from function and plot again produced from the function.
Please note "InputFile" in both the function is same as InputFile from file_lable.
You have asked for the complete algorithm, in fact complete programming except how to manipulate your data. I don't know whether this kind of questions are allowed in stack overflow.
I've given explanation as and where required. I couldn't check the code as you haven't provided the data and function you want to execute. So, there could be errors. Correct it accordingly in the real time.
Note: Add widgets for column headers(i.e panda index) separately as I forgot to add. Use Label and grid methods.
Customize the widgets according to your requirement.
Edit: I had done a mistake of executing the function run_functions by default assuming that when it is run by default, that will continue to run once all the inputs are given. Later I realized that it won't as the function run_functions will just run once and stop. Now I 've edited the code. Now the inputs var1, var2, var3, var4, var5 and var6 are bound with '<Leave>'
and run_function. So after leaving each entry, run_functions will run and check for the status.
Maybe, some may think why all the entries to be bound and why not to bind only the last entry. This is because, sometimes user may fill the last Entry first and then the previous entry.
from tkinter import *
from tkinter import ttk
import tkinter as tk
#import file fialogue module to select file
from tkinter import filedialog as fd
#define functions before the design part
def getFile():
#declare the variable got_file global to use in other functions
global got_file
#assuming the file_type is image file. change the file type accordingly.
fileType = [('image', '*.png'),('image', '*.jpg'),('image', '*.jpeg')]
#open a popup box to select file
got_file = fd.askopenfilename(filetypes=fileType)
#show the selected file in the widget selected_file
selected_file.configure(text = got_file)
#function to disable vars
def disable_vars():
for x in [var1, var2, var3, var4, var5, var6, output_frame]: x['state'] = DISABLED
def enable_vars(vars):
for x in vars: x['state'] = NORMAL
def run_functions():
if rad_var.get() == 'function 1':
if all(x.get() != '' for x in [var1, var2, selected_file]):
function_1()
elif radio_var.get == 'function 2':
if all(x.get() != '' for x in [var3, var4, var5, var6, selected_file])
function_2()
else: pass
def function_1():
#declare global for got_file as we should use here
global got_file
#enable frame where the table/graph to be displayed
output_frame['state'] = NORMAL
'''
Your code for dataframe here
'''
#convert the dataframe to matrix
matrix = df.to_numpy()
for i,x in enumerate(matrix):
for j,y in enumerate(x):
cell = Entry(output_frame, width=20, fg='blue',borderwidth=1, relief="solid")
cell.grid(row=i, column=j)
cell.insert(END, y)
def function_2():
#Use the same method followed in function_1
root= Tk()
root.geometry("800X900")
root.config(bg='light blue')
root.title('Dashboard')
#run the function disable_vars immediately while opening the app
root.after_idle(disable_vars)
frame = tk.Frame(root,bg='light blue')
frame.pack(padx=10,pady=10)
#assuming all the widgets are there in the frame
var1 = Entry(frame)
var1.pack()
var2 = Entry(frame)
var2.pack()
radio_var = tk.StringVar()
fun1 = Radiobutton(frame, text='function 1', variable=radio_var, value= 'function 1', command = lambda: enable_vars([var1, var2]))
fun1.pack(side='top')
var3 = Entry(frame)
var3.pack()
var4 = Entry(frame)
var4.pack()
var5 = Entry(frame)
var5.pack()
var6 = Entry(frame)
var6.pack()
rad_var = = tk.StringVar()
fun1 = Radiobutton(frame, text='function 2',variable=radvar, value= 'function 2', command = lambda: enable_vars([var3, var4, var5, var6]))
fun1.pack(side='top')
file_label = Label(frame,text='Input_File')
file_label.pack(side='top')
selected_file = Label(frame, text = '')
selected_file.pack(side= 'top')
file_btn = Button(tab1, width=25, text='select the File', command=getFile)
prgbtn.pack(side='top', padx=10, pady= 0)
output_frame = Frame(root)
output_frame.pack()
for x in [vars1, vars2, vars3, vars4, vars5, vars6]:
x.bind('Leave', run_functions)
root.mainloop()