![](/img/trans.png)
[英]How do I open a file with tkinter and save the data to another variable?
[英]tkinter - open file with button and save filename to variable
我正在嘗試解決一個看似簡單的問題,但我不知道如何解決。
我想創建一個簡單的程序來檢查文本文件中是否存在某個符號:
非常簡單,但我找不到如何將文件名保存到變量中,然后將其用於該過程。 我看了很多教程,但找不到解決方案。 這是代碼:
from tkinter import *
from tkinter import filedialog
def clicked():
global filename
filename = filedialog.askopenfile(filetypes=(("Word files","*.docx"),))
window = Tk()
window.geometry()
window.title("My App")
open_file_label = Label(window, text="Open your docx file here:", font=("Arial", 10), padx=5, pady=5)
open_file_label.grid(column=0, row=0)
open_file_button = Button(window, text="Click me", command=clicked, padx=5, pady=5)
open_file_button.grid(column=1, row=0)
window.mainloop()
filename 已經是包含所選文件內容的變量。 只需print(filename)
,您就會在控制台中打印您的數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.