tkinter上传本地文件
时间: 2023-07-13 15:15:12 浏览: 207
文件上传到本地
你可以使用tkinter中的filedialog模块来上传本地文件。具体步骤如下:
1. 导入tkinter和filedialog模块
```python
from tkinter import *
from tkinter import filedialog
```
2. 创建上传按钮并绑定函数
```python
def upload_file():
file_path = filedialog.askopenfilename()
print("选择的文件路径:", file_path)
upload_button = Button(root, text="上传文件", command=upload_file)
```
3. 执行程序并点击上传按钮选择文件即可
完整代码示例:
```python
from tkinter import *
from tkinter import filedialog
root = Tk()
root.geometry("300x300")
def upload_file():
file_path = filedialog.askopenfilename()
print("选择的文件路径:", file_path)
upload_button = Button(root, text="上传文件", command=upload_file)
upload_button.pack()
root.mainloop()
```
阅读全文