我有两个函数可以产生两种类型的输出,一个是数据框表,另一个是该数据框的曲线图.所有函数都接受一个文件作为输入.我们从前面的tkinter函数加载它.我想从单选框中动态 Select 一个函数,接下来,一旦我们 Select 了一个特定的函数,它应该显示一个空白框,要求用户输入,并根据输入执行该函数.

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()

一旦我们从单选框中 Select Function_1,那么我们应该立即得到单选框旁边的两个框,这两个框将要求"var1"和"var2".如果我们 Select Function_2,那么在单选框旁边应该有四个框,要求输入"Var3"、"Var4"、"Var5"和"Var6".

一旦收到所有输入,我们应该处理相应的函数,下面我们应该得到两个输出,第一个是从函数中产生的"框架"表,第二个是从函数中产生的图.

请注意,这两个函数中的"InputFile"与来自FILE_LABLE的InputFile相同.

推荐答案

您要求的是完整的算法,实际上是完整的编程,除了如何操作您的数据.我不知道堆栈溢出中是否允许这种问题.

如有需要,我已作了解释.我无法判断代码,因为您尚未提供要执行的数据和函数.因此,可能存在错误.实时进行相应的更正.

注:单独添加列标题(即Pandas 索引)的小部件,因为我忘了添加.使用标签和网格方法.

根据您的需求定制Widget.

编辑:我犯了一个错误,在缺省情况下执行函数RUN_Functions,假设它在缺省情况下运行时,会在给定所有输入后继续运行.后来,我意识到它不会,因为函数Run_Functions只会运行一次,然后停止.现在,我已经编辑了代码.现在,输入var1、var2、var3、var4、var5和var6与'<Leave>'和Run_Function绑定在一起.因此,在离开每个条目后,Run_Functions将运行并判断状态.

也许,有些人会想,为什么所有条目都要绑定,为什么不只绑定最后一个条目.这是因为,有时用户可能会先填写最后一个条目,然后再填写前一个条目.

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()

Python相关问答推荐

比较两个二元组列表,NP.isin

在内部列表上滚动窗口

将jit与numpy linSpace函数一起使用时出错

对某些列的总数进行民意调查,但不单独列出每列

运行Python脚本时,用作命令行参数的SON文本

将两只Pandas rame乘以指数

图像 pyramid .难以创建所需的合成图像

如何调整QscrollArea以正确显示内部正在变化的Qgridlayout?

未知依赖项pin—1阻止conda安装""

如何指定列数据类型

在Python中调用变量(特别是Tkinter)

如何在BeautifulSoup/CSS Select 器中处理regex?

在pandas/python中计数嵌套类别

为什么常规操作不以其就地对应操作为基础?

python sklearn ValueError:使用序列设置数组元素

当条件满足时停止ODE集成?

获取git修订版中每个文件的最后修改时间的最有效方法是什么?

如何在python tkinter中绑定键盘上的另一个回车?

基于2级列表的Pandas 切片3级多索引

Pandas 数据框自定义排序功能