简体   繁体   English

如何将一个 python 脚本的输入也分配为另一个脚本的输入

[英]How do I assign an input from one python script to be an input in another script as well

I currently have two different python scripts.我目前有两个不同的 python 脚本。 The first one converts parquet files to xlsx and the second is really just trying to build a gui to be able to run the first script.**第一个将镶木地板文件转换为 xlsx,第二个实际上只是尝试构建一个 gui 以便能够运行第一个脚本。**

Here is the first script:这是第一个脚本:

import pandas as pd
import os
import pyarrow
import shutil
from pathlib import Path
file = input("What file would you like to convert from parquet to csv? ")
df1 = pd.read_parquet(file)
df = df1.append(df1, ignore_index=True)
dirout = input("Where would you like the xlsx file to be output to?")
name = input("What would you like to call the ouput file?" )
cfile = os.path.join(dirout, name + "." + "xlsx")
df.to_excel(cfile)

In this second script I would like the user to input txt that would feed file, name and dirout.在第二个脚本中,我希望用户输入可以提供文件、名称和目录的 txt。 Is that possible?那可能吗?

import os
from tkinter import *

window = Tk()
window.title("Convertor")
#Sets the size of the window
window.geometry('550x400')

#Adds a header to the window and configures the size 
lbl = Label(window, text="Convert Parquet to CSV", font=("Arial Bold", 18))

#Configures where the label message will appear
lbl.grid(column=0, row=0)

#adds an input text message
txt = Entry(window,width = 30)
txt.grid(column=3, row=3)

txt = Entry(window,width = 30)
txt.grid(column=4, row=4)
def clicked():
    os.system('python ParquetToCsv.py')
#Adding a button
btn = Button(window, text="Convert", command=clicked)
btn.grid(column=6, row = 6)

#The mainloop causes the window to remain open until someone interacts with it
window.mainloop()

Turn your first script into a function with arguments for the variables you want to pass:将您的第一个脚本转换为带有要传递的变量的参数的函数:

def myfunc(f, n, dir):
   file = f
   df1 = pd.read_parquet(file)
   df = df1.append(df1, ignore_index=True)
   dirout = dir
   name = n
   cfile = os.path.join(dirout, name + "." + "xlsx")
   df.to_excel(cfile)

Then import the function into the other script and call it passing the arguments:然后将该函数导入另一个脚本并调用它并传递参数:

import os
from tkinter import *
from myutils import myfunc

window = Tk()
window.title("Convertor")
#Sets the size of the window
window.geometry('550x400')

#Adds a header to the window and configures the size 
lbl = Label(window, text="Convert Parquet to CSV", font=("Arial Bold", 18))

#Configures where the label message will appear
lbl.grid(column=0, row=0)

#adds an input text message
txt_name = Entry(window,width = 30)
txt.grid(column=3, row=3)

txt_file = Entry(window,width = 30)
txt.grid(column=4, row=4)

txt_dir = Entry(window,width = 30)
txt.grid(column=4, row=4)
def clicked():
    os.system('python ParquetToCsv.py')
#Adding a button
btn = Button(window, text="Convert", command=clicked)
btn.grid(column=6, row = 6)

#The mainloop causes the window to remain open until someone interacts with it
window.mainloop()

# Call function passing the arguments
myfunc(txt_file, txt_name, txt_dir)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Python中将用户输入从一个脚本传递到另一个脚本? - How to pass user input from one script to another in Python? 如何将一个python脚本的输出作为另一个python脚本的用户输入 - How can i take the output of one python script as user input for another python script 如何在另一个 python 脚本中使用来自 1 个 python 脚本的输入 - How to use the input from 1 python script in another python script 如何从另一个 python 脚本向 python 脚本发送多个“用户输入”? - How can I send multiple “user input” to python script from another python script? 将用户输入从一个 Python 脚本传递到另一个 - Passing user input from one Python script to another 一个Python脚本为另一个python脚本提供“用户输入” - One Python script giving “user input” to another python script 使用另一个Python脚本的输入来更新一个Python脚本中的循环变量 - Update variables in a loop in one python script with input from another python script 如何从另一个脚本访问一个 python 脚本中的变量 - How do I access a variable in one python script from another script 如何将输入传递给在另一个 python 脚本中运行的 python 脚本? - how to pass input to python script running in another python script? 如何连接 Python 脚本以在网页输入上运行组合? - How do I connect a Python script to run combinations on a webpage input?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM