I am trying to solve a problem that seems simple, but I cannot figure out how.
I want to create a simple program checking if a certain symbol exists in a text file:
- The program starts;
- The user clicks a button (inside the window, not in the menu);
- A dialog box appears;
- The user chooses a text file;
- A message box displays the result;
- The program closes.
Pretty straightforward, but I cannot find how to save the filename into a variable and then use it for the process. I read so many tutorials and I could not find a solution. Here is the code:
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()