[英]Using the result from part of a python function and store in variable outside function
In my program I am calling a function that imports.csv data into a treeview
using tkinter
and csv
module. In my program I am calling a function that imports.csv data into a treeview
using tkinter
and csv
module. The Function is only called when the Import
button is used.仅在使用Import
按钮时才调用 Function。 The import
button is linked to function select_input_file
. import
按钮链接到 function select_input_file
。 The function works as expected and the csv data is imported into my treeview
without any issues. function 按预期工作,csv 数据导入我的treeview
没有任何问题。 From here I would like to store some of the function results into variables
outside the function.从这里我想将一些 function 结果存储到 function 之外的variables
中。 For instance the input_file_path
which will only contain a value once the function is called and a file is imported.例如input_file_path
只有在调用 function 并导入文件时才会包含一个值。 Also data imported into Ro Number, Rego Number
columns.还将数据导入Ro Number, Rego Number
列。 I have attempted to use return()
but it stops the function from running any further.我曾尝试使用return()
但它会阻止 function 继续运行。 I have also read up on yield
but through documentation im not sure if this is my best option.我也阅读了yield
,但通过文档我不确定这是否是我最好的选择。 I thought a solution could be to use return()
and then for in rdr:
after each return so the select_input_file
function continued but Im sure theres a better way??我认为一个解决方案可能是使用return()
然后for in rdr:
在每次返回之后select_input_file
function 继续但我确定有更好的方法吗? Ive cut the code down to what I thought was necessary.我已经将代码削减到我认为必要的程度。
import tkinter as tk
from tkinter import *
import tkinter.ttk as tkrttk
import csv
root = tk.Tk()
treetime = tkrttk.Treeview(root)
treetime['columns'] = ("Column2", "Column3", "Column4", "Column5",
"Column6", "Column7", "Column8", "Column9", "Column10", "Column11")
def select_input_file():
input_file_path = filedialog.askopenfilename(
filetypes=(("CSV files", "*.csv"),))
with open(input_file_path) as csv_file:
rdr = csv.DictReader(csv_file)
for row in rdr:
RoNumber = row['Ro Number']
DateIn = row['Date In']
TimeIn = row['Time In']
TimeOut = row['Time Out']
RegoNumber = row['Rego Number']
CustomerName = row['Customer Name']
VehicleMake = row['Vehicle Make']
VehicleModel = row['Vehicle Model']
JobDescription = row['Job Description']
CurrentStatus = row['Current Status']
treetime.insert("", 0, values=(RoNumber, DateIn, TimeIn, TimeOut, RegoNumber,
CustomerName, VehicleMake, VehicleModel, JobDescription, CurrentStatus))
menubar=Menu(root)
filemenu=Menu(menubar, tearoff=0)
filemenu.add_command(label="Import", command=select_input_file)
root.mainloop()
For your specific question: you can't return
and continue executing within a function.对于您的具体问题:您无法return
并继续在 function 中执行。 You could get that effect with yield
, but I think it would be abusing the semantics and could just give you future headaches.您可以使用yield
来获得这种效果,但我认为它会滥用语义,并且可能会让您在未来感到头疼。 You can return
more than one thing:您可以return
不止一件事:
def foo():
return "Spam", 42
def bar():
name, answer = foo()
This would let you return (eg) the selected filename and data from the file to the calling function.这将允许您将(例如)从文件中选择的文件名和数据返回给调用 function。
A more general comment: functions should generally do one thing.更一般的评论:函数通常应该做一件事。 This makes them easier to test (the purpose is clear) and easier to modify (fewer if any side effects).这使得它们更容易测试(目的很明确)并且更容易修改(如果有副作用的话)。 Right now your function is getting user input (filename), loading and parsing the file contents, and updating the control.现在您的 function 正在获取用户输入(文件名),加载和解析文件内容,并更新控件。 You may want to have separate functions that do these different things, and are just brought together by a short function that is invoked when 'Import' is clicked.您可能希望拥有单独的函数来执行这些不同的操作,并且只需通过单击“导入”时调用的简短 function 将其组合在一起。 Your original question might be easier to address this way, too.您最初的问题也可能更容易以这种方式解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.