Python 如何在tkinter中将用户输入目录作为函数返回?
我只是在学习Python,我正在尝试构建一个程序,在指定位置解压文件,然后将其移动到另一个指定位置 我创建了一个函数,要求用户使用Python 如何在tkinter中将用户输入目录作为函数返回?,python,tkinter,Python,Tkinter,我只是在学习Python,我正在尝试构建一个程序,在指定位置解压文件,然后将其移动到另一个指定位置 我创建了一个函数,要求用户使用filedialog.askopenfilename搜索包含.zip文件的指定文件。然后应该将其存储起来,以便将其发送到另一个函数,该函数将解压文件,因为我不希望在收到位置后自动解压 我面临的问题是,我不确定如何传递和存储位置目录以供其他函数使用。还是有更好的方法 import tkinter as tk from tkinter import filedialog
filedialog.askopenfilename
搜索包含.zip文件的指定文件。然后应该将其存储起来,以便将其发送到另一个函数,该函数将解压文件,因为我不希望在收到位置后自动解压
我面临的问题是,我不确定如何传递和存储位置目录以供其他函数使用。还是有更好的方法
import tkinter as tk
from tkinter import filedialog
import zipfile
import os
import pathlib
root = tk.Tk()
root.geometry('450x350+350+150') # setting the screen size
root.title('Unzip file') # setting the title name
# Gets the user to choose the location of the zip file
def input_path():
input_file_dir = filedialog.askdirectory()
return input_file_dir # how do I return this so that it can be passed to the extract_zip
# extracting the zip
def extract_zip(input_file_dir):
os.chdir(input_file_dir) # changing file directory to location of zip folder
for file in os.listdir(): # get the list of files
if zipfile.is_zipfile(file): # if it is a zipfile, extract it
with zipfile.ZipFile(file) as item: # treat the file as a zip
item.extractall() # extract it in the working directory
# Setting the frame size to insert all our widgets
frame = tk.Frame(root, bg='black')
frame.pack(fill='both', expand=False)
# Setting the title in the frame
label = tk.Label(frame, text='Unzip Files', font='Arial 36 bold', fg='#948a54', bg='black', pady=50)
label.pack(side='top')
openFile = tk.Button(frame, text='Choose location of zip file', font='14', command=input_path)
openFile.pack(side='top')
unzipButton = tk.Button(frame, text='Unzip', width=19, font='14', command=extract_zip)
unzipButton.pack(side='top', pady=15)
root.mainloop()
您可以使用
classes
和self
变量,这些变量可以跨类中的函数访问。我已经更新了一些代码以使其能够工作,如果您不熟悉类
和self
,我将在下面提供一些链接,以了解它们的功能
import tkinter as tk
from tkinter import filedialog
import zipfile
import os
import pathlib
class unzip():
def __init__(self):
root = tk.Tk()
root.geometry('450x350+350+150') # setting the screen size
root.title('Unzip file') # setting the title name
frame = tk.Frame(root, bg='black')
frame.pack(fill='both', expand=False)
# Setting the title in the frame
label = tk.Label(frame, text='Unzip Files', font='Arial 36 bold', fg='#948a54', bg='black', pady=50)
label.pack(side='top')
openFile = tk.Button(frame, text='Choose location of zip file', font='14', command=self.input_path)
openFile.pack(side='top')
unzipButton = tk.Button(frame, text='Unzip', width=19, font='14', command=self.extract_zip)
unzipButton.pack(side='top', pady=15)
root.mainloop()
# Gets the user to choose the location of the zip file
def input_path(self):
self.input_file_dir = filedialog.askdirectory()
# extracting the zip
def extract_zip(self):
print(self.input_file_dir)
os.chdir(self.input_file_dir) # changing file directory to location of zip folder
for file in os.listdir(): # get the list of files
if zipfile.is_zipfile(file): # if it is a zipfile, extract it
with zipfile.ZipFile(file) as item: # treat the file as a zip
item.extractall() # extract it in the working directory
if __name__ == '__main__':
unzip()
您能提供与问题相关的代码以及您尝试过的内容吗?我已经添加了代码,但我认为没有任何好处