使用Tkinter打开文件,运行脚本并导出文件 [英] Use Tkinter to Open file, run script and export file
问题描述
我想创建一个pyton脚本,该脚本接受一个excel文件(由用户提供),运行一个脚本,然后保存该excel文件(使用用户提供的文件名).我想使用Tkinter实现一些按钮,以使非程序员可以轻松使用我的算法.
I would like to create a pyton script that takes an excel file (provided by the user), run a script, and save this excel file (with a file name provided by the user). I would like to implement some buttons using Tkinter to enable non-programmers to easily use my algorithm.
我试图创建一个加载数据"和显示数据"按钮(我在线上获取了代码),两者均起作用.
I tried to create a ''load data'' and ''display data'' button (I took a code online) that both work.
但是我没有设法在导入的数据库上运行脚本(因为我不知道如何访问它)
However I didn't manage to run the script on the database imported (as I don't know how to access it)
我没有设法创建一个按钮,该按钮使用户可以选择要将其保存到的文件的名称.
I didnt manage to create a button that enables the user to choose the name of the file they want to save it to.
import tkinter as tk
from tkinter import ttk
from tkinter.filedialog import askopenfilename
from tkinter.filedialog import asksaveasfilename
from tkinter.messagebox import showerror
import pandas as pd
import csv
# --- classes ---
class MyWindow:
def __init__(self, parent):
self.parent = parent
self.filename = None
self.df = None
self.text = tk.Text(self.parent)
self.text.pack()
self.button = tk.Button(self.parent, text='LOAD DATA', command=self.load)
self.button.pack()
self.button = tk.Button(self.parent, text='DISPLAY DATA', command=self.display)
self.button.pack()
self.button = tk.Button(self.parent, text='DISPLAY DATA', command=self.display)
self.button.pack()
def load(self):
name = askopenfilename(filetypes=[('CSV', '*.csv',), ('Excel', ('*.xls', '*.xslm', '*.xlsx'))])
if name:
if name.endswith('.csv'):
self.df = pd.read_csv(name)
else:
self.df = pd.read_excel(name)
self.filename = name
display directly
self.text.insert('end', str(self.df.head()) + '\n')
def display(self):
# ask for file if not loaded yet
if self.df is None:
self.load_file()
# display if loaded
if self.df is not None:
self.text.insert('end', self.filename + '\n')
self.text.insert('end', str(self.df.head()) + '\n')
#def script_python(self):
# self.df=self.df.iloc[:, :-1]
#this is not my original script
#def file_save(self):
# savefile = asksaveasfilename(filetypes=(("Excel files", "*.xlsx"),
# ("All files", "*.*") ))
# --- main ---
if __name__ == '__main__':
root = tk.Tk()
top = MyWindow(root)
root.mainloop()
我对excel文件进行分析的python脚本可以工作,所以我只用了一个非常简单的python脚本即可使您的生活更轻松.
My python script that do analysis on the excel file works, so I just put a very simple python script to make your life easier.
谢谢我是Tkinter的新手,不习惯使用班级和词典.谢谢你的帮助
Thank you I'm new at Tkinter and not used to classes and dictionaries. thanks for your help
推荐答案
我修改了您的代码,添加了一个用于运行脚本的按钮和一个用于保存数据的按钮(我试图将其余部分保持在您的原始代码越好)
I've modified your code, adding a button to run your script and a button to save the data (I've tried to keep the rest as close to your original code as possible):
import tkinter as tk
from tkinter import ttk
from tkinter.filedialog import askopenfilename
from tkinter.filedialog import asksaveasfilename
from tkinter.messagebox import showerror
import pandas as pd
import csv
# --- classes ---
class MyWindow:
def __init__(self, parent):
self.parent = parent
self.filename = None
self.df = None
self.text = tk.Text(self.parent)
self.text.pack()
# load data button
self.load_button = tk.Button(self.parent, text='LOAD DATA', command=self.load)
self.load_button.pack()
# display data button
self.display_button = tk.Button(self.parent, text='DISPLAY DATA', command=self.display)
self.display_button.pack()
# run script button
self.script_button = tk.Button(self.parent, text='RUN SCRIPT', command=self.script_python)
self.script_button.pack()
# save data button
self.save_button = tk.Button(self.parent, text='SAVE DATA', command=self.file_save)
self.save_button.pack()
def load(self):
name = askopenfilename(filetypes=[('CSV', '*.csv',), ('Excel', ('*.xls', '*.xslm', '*.xlsx'))])
if name:
if name.endswith('.csv'):
self.df = pd.read_csv(name)
else:
self.df = pd.read_excel(name)
self.filename = name
# display directly
self.text.insert('end', str(self.df.head()) + '\n')
def display(self):
# ask for file if not loaded yet
if self.df is None:
self.load_file()
# display if loaded
if self.df is not None:
self.text.insert('end', self.filename + '\n')
self.text.insert('end', str(self.df.head()) + '\n')
def script_python(self):
# replace this with the real thing
self.df = self.df.iloc[:, :-1]
def file_save(self):
fname = asksaveasfilename(filetypes=(("Excel files", "*.xlsx"),
("All files", "*.*")))
# note: this will fail unless user ends the fname with ".xlsx"
self.df.to_excel(fname)
# --- main ---
if __name__ == '__main__':
root = tk.Tk()
top = MyWindow(root)
root.mainloop()
这篇关于使用Tkinter打开文件,运行脚本并导出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!