![](/img/trans.png)
[英]download from path 'file' save to 'other directory' python script issue
[英]Is it possible to have save the directory and the file path in python?
我是 python 新手,我想创建一个可以存储目录和文件路径的 GUI。 我很困惑,因为代码只存储一个变量。
import tkinter
import tkinter.filedialog
def OnDirectoryButtonClick(self):
self.entryVariable.set( tkinter.filedialog.askdirectory() )
self.entry.focus_set()
self.entry.selection_range(0, tkinter.END)
def OnFileButtonClick(self):
self.entryVariable.set( tkinter.filedialog.askopenfilename() )
self.entry.focus_set()
self.entry.selection_range(0, tkinter.END)
root = tkinter.Tk()
frame=tkinter.Frame(root)
firstButton = tkinter.Button(frame, text ="Choose", command=lambda:
OnDirectoryButtonClick(root))
firstButton.grid(column=1,row=1)
root.entryVariable = tkinter.StringVar()
root.entry = tkinter.Entry(frame,textvariable=root.entryVariable)
root.entry.grid(column=0,row=1)
secondButton = tkinter.Button(frame, text ="Choose", command=lambda: OnFileButtonClick(root))
secondButton.grid(column=1,row=3)
root.entryVariable = tkinter.StringVar()
root.entry = tkinter.Entry(frame,textvariable=root.entryVariable)
root.entry.grid(column=0,row=3)**
frame.pack(pady=9)
root.mainloop()
您对这两个字段使用了相同的变量。 使用此代码可以正常工作。
import tkinter
import tkinter.filedialog
def OnDirectoryButtonClick(self):
self.entryVariable_dir.set( tkinter.filedialog.askdirectory() )
self.entry_dir.focus_set()
self.entry_dir.selection_range(0, tkinter.END)
def OnFileButtonClick(self):
self.entryVariable_file.set( tkinter.filedialog.askopenfilename() )
self.entry_file.focus_set()
self.entry_file.selection_range(0, tkinter.END)
root = tkinter.Tk()
frame=tkinter.Frame(root)
firstButton = tkinter.Button(frame, text ="Choose", command=lambda:
OnDirectoryButtonClick(root))
firstButton.grid(column=1,row=1)
root.entryVariable_dir = tkinter.StringVar()
root.entry_dir = tkinter.Entry(frame,textvariable=root.entryVariable_dir)
root.entry_dir.grid(column=0,row=1)
secondButton = tkinter.Button(frame, text ="Choose", command=lambda:
OnFileButtonClick(root))
secondButton.grid(column=1,row=3)
root.entryVariable_file = tkinter.StringVar()
root.entry_file = tkinter.Entry(frame,textvariable=root.entryVariable_file)
root.entry_file.grid(column=0,row=3)
frame.pack(pady=9)
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.