私はTkinterとPythonも新しくなっています。 Tkinterフレームに3つのボタンがあります。ボタン1はopen_csv_dialog()を呼び出し、ファイルダイアログボックスを開いて.csvファイルを選択し、パスを返します。ボタン2はsave_destination_folder()を呼び出し、ファイルダイアログボックスを開いて優先ディレクトリを開き、パスを返します。Python 3 - Tkinterボタンコマンド
私の問題は、それはファイルパスは私が試してみました、ボタン1とボタン2
から返される必要がありますmodify_word_doc()を呼び出すボタン3です。
button3 = ttk.Button(root, text="Run", command=lambda: modify_word_doc(open_csv_dialog, save_destination_folder)).pack()
が、それは明らかにただ望ましくないopen_csv_dialog()とsave_destination_folder()関数の両方のために再度開くファイルダイアログボックスを求めるメッセージが表示されます。これらの2つの関数からすでに返されたファイルパスを使用して、別のファイルダイアログボックスでプロンプトを表示せずに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があります。もう1つは '(' '' '')です。 –