Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
103 views
in Technique[技术] by (71.8m points)

python - How to create Tkinter entry for each file uploaded?

I'm trying to write a code which opens a file browser winder to select data files and then creates an entry box for a 'start' value for each file uploaded. Then, I want to be able to access these 'start' values when I run another function. I have successfully made the file browser and entry fields for each file uploaded, but I'm not sure how to append the entry fields to a list when pressing another button to activate a different function. Any help is much appreciated.

import tkinter as tk
from tkinter import (filedialog,Label,Button,Tk,W,Entry)
import os

all_files = []
start_vals = []

window = Tk()
window.title('Entry Test')
window.geometry("500x300")


def browse_files():
    filename_list = list(filedialog.askopenfilenames(initialdir = '/',
                        title = "Select Files",filetypes = (("csv files","*.csv"),
                                                            ("all files","*.*"))))
    
    for i in filename_list:
        all_files.append(i)
    
    Label(window, text=f'Selected Files ({len(all_files)}):', 
          font=("arial", 13)).grid(row=4, sticky=W)
    
    Label(window, text='Start:').grid(row=4, column=1)
    
    u = 0
    while u < len(all_files):    
        Label(window, text=str(os.path.basename(all_files[u])), 
              font=("arial", 11)).grid(row=u+5, column=0, sticky=W)
            
        e1 = Entry(window)        
        e1.grid(row=u+5, column=1)
        
        u+=1    
    
    return 

def print_entry():
    for i in start_vals:
        print(i)
    return

Button(window, text='Browse', command=browse_files).grid(row=3, column=1, sticky=W, pady=4)
Button(window, text='Print start vals', command=print_entry).grid(row=3, column=2, sticky=W, pady=4)

window.mainloop()
question from:https://stackoverflow.com/questions/65940370/how-to-create-tkinter-entry-for-each-file-uploaded

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can append the entries into start_vals inside browse_files(), then go through the entry list to get the values inside print_entry:

def browse_files():
    ...
    while u < len(all_files):
        ...
        e1 = Entry(window)
        start_vals.append(e1)
        ...

def print_entry():
    for e in start_vals:
        print(e.get())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...