我是Tkinter和Python的新手。我有三個按鈕,在我的Tkinter框架中有命令。按鈕1調用open_csv_dialog(),打開文件對話框以選擇.csv文件並返回路徑。按鈕2調用save_destination_folder(),打開文件對話框打開首選目錄並返回路徑。Python 3 - Tkinter按鈕命令
我的問題是按鈕3.它調用modify_word_doc(),它需要從文件路徑按鈕1和按鈕2
我試圖返回;
button3 = ttk.Button(root, text="Run", command=lambda: modify_word_doc(open_csv_dialog, save_destination_folder)).pack()
但這顯然只是提示文件對話框,再次爲這兩個open_csv_dialog()和save_destination_folder()函數,這是不希望打開。我想只使用已經從這兩個函數返回的文件路徑,並將其傳遞到modify_word_doc而不會被另一個文件對話框提示。我也嘗試使用partial
,但我要麼使用它錯誤,要麼仍然具有相同的不良後果。
我已經閱讀了關於命令的Tkinter文檔,並搜索了一個可能的答案,所以如果之前已經回答並且我沒有找到答案,請致歉。
import tkinter as tk
from tkinter import filedialog
from tkinter import ttk
import os
import csv
import docx
from functools import partial
root = tk.Tk()
def open_csv_dialog():
file_path = filedialog.askopenfilename(filetypes=(("Database files",
"*.csv"),("All files", "*.*")))
return file_path
def save_destination_folder():
file_path = filedialog.askdirectory()
return file_path
def modify_word_doc(data, location):
#data = open_csv_dialog()
#location = save_destination_folder()
#long code. takes .csv file path opens, reads and modifies word doc with
#the contents of the .csv, then saves the new word doc to the requested
#file path returned from save_destination_folder().
label = ttk.Label(root, text="Step 1 - Choose CSV File.",
font=LARGE_FONT)
label.pack(pady=10, padx=10)
button = ttk.Button(root, text="Choose CSV",
command= open_csv_dialog).pack()
label = ttk.Label(root,
text="Step 2 - Choose destination folder for your letters.",
font=LARGE_FONT)
label.pack(pady=10, padx=10)
button2 = ttk.Button(root, text="Choose Folder",
command=save_destination_folder).pack()
label = ttk.Label(root, text="Step 3 - Select Run.", font=LARGE_FONT)
label.pack(pady=10, padx=10)
button3 = ttk.Button(root, text="Run",
command=lambda: modify_word_doc(open_csv_dialog, save_destination_folder)).pack()
root.mainloop()
你有一個語法錯誤btw,多一個'('then')''。 –