Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - Tkinter opening multiple files using one code

So I have designed a program that allows you to open 3 CSV files, and translate them into json, and 'clean' them then merge them into one file.

The way i have designed it, is that on display, there are 3 buttons for loading each file, but i am wondering if there is a better design that shows only 1 button, which the user can load multiple files that are assigned to different variables, depending on the column headings in these csv files.

The problem that I had, is when you load one file, it overrides the previously loaded file.

For reference, the csv files can be only 1/3 different formats so the column headings are known.

As you can see from a short part of my code, that i have created 2 different loadfile buttons, for the 2 separate files, but is there a way i can have just 1 button, that assisgns different files to different variables?

import tkinter as tk
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
import pandas as pd
import json
import csv
import sys
import sqlite3

#
def loadInspection():
    global inspectionFile
    global label_inspectionFile
    
    loadingCSV = filedialog.askopenfilename()
    inspectionFile = pd.read_csv(loadingCSV)

#Making buttons for load and translate and clean
loadButton = Button(window1, text="Load Inspections File", command=loadInspection)
loadButton.grid(row=0, column=1)

#loading first file called inspections
def inspectionClean(): 
    global inspectionFile
    
    inspectionFile = inspectionFile
    #Check and remove any duplicates in the Inspection Data
    inspectionFile = inspectionFile[inspectionFile.duplicated() == False]
    
cleanInsButton = Button(window1, text="Clean", command=inspectionClean)
cleanInsButton.grid(row=0, column=3)

#loading second file called inventroy
def loadInventroy():
    global inventroyFile
    
    loadingCSV = filedialog.askopenfilename()
    inventroyFile = pd.read_csv(loadingCSV)


#Making buttons for load and translate and clean
loadButton = Button(window1, text="Load Inventroy File", command=loadInventroy)
loadButton.grid(row=3, column=1)

def inventroyClean(): 
    global inventroyFile
    
    inventroyFile = inventroyFile
    #Check and remove any duplicates in the Inspection Data
    inventroyFile = inventroyFile[inventroyFile.duplicated() == False]
    

cleanInvButton = Button(window1, text="Clean", command=inventroyClean)
cleanInvButton.grid(row=3, column=3)
question from:https://stackoverflow.com/questions/65642394/tkinter-opening-multiple-files-using-one-code

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

1 Reply

0 votes
by (71.8m points)

My guess would be this may be a problem with global variables. In general I would recommend using instance/class variables instead, using global variables is generally not advised.

Your load functionality could instead be structured like this:

class MyCSVLoader:
    def __init__(self):
        self.inventory = None
        self.inspection = None

    def loadCSV(self):
        loadingCSV = filedialog.askopenfilename()

        return pd.read_csv(loadingCSV)

    def loadAll(self):
        self.inspection = self.loadCSV()
        self.inventory = self.loadCSV()

However, let me know if this works as I don't have csv files on hand to test it with.

Then, you could implement it with one button like this:

csvLoader = MyCSVLoader()

loadButton = tk.Button(window1, text="Load All Files", command=csvLoader.loadAll)
loadButton.grid(row=0, column=1)

Which should pop up 1 window after the other for you to select 2 files. The files selected can then be accessed through csvLoader.inventory and csvLoader.inspection.

Note that your clean function would also have to go into this class to function properly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...