Home > Net >  tkinter update label with filename from menu class
tkinter update label with filename from menu class

Time:05-30

I am trying to follow an object-oriented approach to create a simple tkinter app to display some data but I am struggling to pass values between the MenuBar class in which a file is selected and the main App class where ultimately I will work with the file. For now I am just trying to display the filename on screen in a Label. I have tried to pass the filename as a tk.StringVar but this is not being updated on screen. Simplified code below.

import tkinter as tk
from tkinter import filedialog as fd #needed for file dialogues
from tkinter import ttk

class MenuBar(tk.Menu):
  def __init__(self):
    super().__init__()
    self.file_audio = tk.StringVar()

    fileMenu = tk.Menu(self, tearoff=0)
    fileMenu.add_command(label="Select file...", command=self.select_file)
    self.add_cascade(label="File", menu=fileMenu)

  # method to select a single file
  def select_file(self):
    self.file_audio = fd.askopenfilename(title='Open a file')
    
class App(tk.Tk):
  def __init__(self):
    super().__init__()

    self.title('Title')
    self.geometry("800x300")
    
    self.menubar = MenuBar()
    self.config(menu=self.menubar)

    self.createWidgets()

  def createWidgets(self):
    self.file_info = ttk.Label(self, text = self.menubar.file_audio)
    self.file_info.grid(column=1, row=1)

def main():
  app = App()
  app.mainloop()
    
if __name__ == "__main__":
  main()

CodePudding user response:

Not sure if this is what you are looking for. But I have added a line of code that I think might be useful. In the "createWidgets" method, you should call self.menubar.select_file().

import tkinter as tk
from tkinter import filedialog as fd #needed for file dialogues
from tkinter import ttk

class MenuBar(tk.Menu):
  def __init__(self):
    super().__init__()
    self.file_audio = tk.StringVar()

    fileMenu = tk.Menu(self, tearoff=0)
    fileMenu.add_command(label="Select file...", command=self.select_file)
    self.add_cascade(label="File", menu=fileMenu)

  # method to select a single file
  def select_file(self):
    self.file_audio = fd.askopenfilename(title='Open a file')

class App(tk.Tk):
  def __init__(self):
    super().__init__()

    self.title('Title')
    self.geometry("800x300")
 
    self.menubar = MenuBar()
    self.config(menu=self.menubar)

    self.createWidgets()

  def createWidgets(self):
    self.menubar.select_file() #Added line of code
    self.file_info = ttk.Label(self, text = self.menubar.file_audio)
    self.file_info.grid(column=1, row=1)

def main():
  app = App()
  app.mainloop()

if __name__ == "__main__":
  main()

CodePudding user response:

There are two issues in your code:

  1. textvariable option should be used instead of text option in ttk.Label(...)
  def createWidgets(self):
    self.file_info = ttk.Label(self, textvariable = self.menubar.file_audio)
    self.file_info.grid(column=1, row=1)
  1. Use self.file_audio.set(...) instead of assignment inside MenuBar.select_file()
  def select_file(self):
    self.file_audio.set(fd.askopenfilename(title='Open a file'))
  • Related