button change its text to the file name

  python, tkinter

How do I force the button to change its text to the filename? Everything works fine, a dialog box opens and files open too. But I still can’t change the button name to the file name and save it.

Here is the code:

import tkinter.filedialog as tfd
import tkinter as tk
import os


window = tk.Tk()
window.title("Op")
window.geometry("600x400")
window.resizable(False, False)
file_name = ""

def open():
    global file_name
    file_name = tfd.askopenfilename()
    os.startfile(file_name) #open file


btn1 =  tk.Button(window, text=f"Open {file_name}", command=open) #button
btn1.place(x = 20, y = 25)

Source: Python Questions

LEAVE A COMMENT