Trying to use textvariable
to set the text of a python tkinter/ttk entry widget. The value of the StringVar
is correctly set, but the text still fails to appear in the entry widget.
In this question, Cannot make default value display for for ttk entry widget using the textvariable parameter, the solution was to make the textvariable a class variable using self, however in my code the issue still persists. I have presented an example of my code (this class is being executed by another):
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
class example:
def __init__(self):
pass
def __call__(self):
self.file1 = StringVar(value = "default value")
self.file2 = StringVar(value = "default value")
def select_file_1():
self.file1.set(filedialog.askopenfilename(initialdir = "/", title = "Select a File", filetype = (("txt", "*.txt"), ("All Files","*.*"))))
def select_file_2():
self.file2.set(filedialog.askopenfilename())
root = Tk()
root.title("example program")
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
style = ttk.Style(root)
style.configure("TLabel", font = ("TkDefaultFont", 12))
ttk.Button(mainframe, text="Select file 1: ", command=select_file_1).grid(column=1, row=1, sticky=(W))
self.file1Entry = ttk.Entry(mainframe, width = 60, textvariable=self.file1).grid(column=2, row=1, sticky=(E))
ttk.Button(mainframe, text="Select file 2: ", command=select_file_2).grid(column=1, row=2, sticky=(W))
self.file2Entry = ttk.Entry(mainframe, width = 60, textvariable=self.file2).grid(column=2, row=2, sticky=(E))
for child in mainframe.winfo_children():
child.grid_configure(padx=5, pady=5)
root.mainloop()
Update: issue still persists when line 15 and 17 are fixed to
self.file1.set(filedialog.askopenfilename())