1

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())

  • 1
    The code you posted won't run for me. Nothing instantiates `example`, and if I instantiate it and then call it I get errors. Please make sure your example is complete. That being said, `self.file1` starts out as a `StringVar` but then you overwrite it with the result `askopenfilename`. – Bryan Oakley Jul 19 '22 at 18:45
  • Right. You probably want `self.file1.set(filedialog.asko[enfilename(...))` and `self.file2.set(filedialog.askopenfilename())`. – Tim Roberts Jul 19 '22 at 18:50
  • @BryanOakley `self.file1` still is not displayed before or after askopenfilename is called, even when changing line 15 to `self.focusFile.set(filedialog.askopenfilename(initialdir = "/", title = "Select a File", filetype = (("txt", "*.txt"), ("All Files","*.*"))))` – Darian Siembab Jul 19 '22 at 18:53
  • @DarianSiembab: what is `self.focusFile`? Your code doesn't have that anywhere. – Bryan Oakley Jul 19 '22 at 19:09
  • 1
    Most probably there are more than one instance of `Tk()` because you can create `self.file1` and `self.file2` (instances of `StringVar()`) before the line `root = Tk()`. – acw1668 Jul 20 '22 at 15:40

1 Answers1

2

Bryan is correct. You're overwriting self.file1 and self.file2 with a different object instead of setting their values.

def __call__(self):
    self.file1 = StringVar(value = "default value")
    self.file2 = StringVar(value = "default value")

    def select_file_1():
        file1_selection = filedialog.askopenfilename(
            initialdir="/",
            title="Select a File",
            filetype=(("txt", "*.txt"), ("All Files","*.*"))
        )
        # store the value of from the dialog in the file1 variable via 'set()'
        self.file1.set(file1_selection)
  
  
    def select_file_2():
        file2_selection = filedialog.askopenfilename()
        # store the value of from the dialog in the file2 variable via 'set()'
        self.file2.set(file2_selection)

JRiggles
  • 4,847
  • 1
  • 12
  • 27