Python Forum
[Tkinter] tkinter.Menu – How to make text-variable?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] tkinter.Menu – How to make text-variable?
#1
Problem context:
"tkinter" module has its own mutable alternatives for str, int, ... .
They are StringVar, IntVar, ... .
Widgets like Button, Label, ... do support them.
One can pass StringVar object to a widget via textvariable parameter.
Eg.
tkinter.Button(master= tkinter.Tk(), textvariable= tkinter.StringVar())
But, here tkinter.Menu stands out.
All its nested “widgets“ are loaded with .add_command(), .add_cascade(), ... methods.
Unfortunately, I don't know all their parameters, therefore, if someone posts a link to them they will have my gratitude.
Anyway, instead of having textvariable parameter, they possess label and [command or menu or sth].
Eg.
from tkinter import *
Win= Tk()
Menu1= Menu(master= Win)
Menu1.add_command(label= "My text.", command= print)
Menu1.add_cascade(label= "My another text.", menu= Menu(Menu1))
Win.configure(menu= Menu1)
Win.mainloop()

My purpose:
I'm making a multilingual object-orientet game. Which has a dictionary full of StringVar-s which change their value each time a user changes languages (I want to make a pull-down menu with languages). Widgets have textvariable-s connected with my dictionary. Dictionary draws values for StringVar-s from sqlite3 database, when language changes. Everything works for every widget, except for the Menu.

What I need:
I want my menubar to also be labelled with StringVar-s, so that I can keep a collection (dictionary) with references to these objects with their values. I would like to control the labels via my dictionary. #Update: Yes, I know I can rebuild the menu or change its properties on every dictionary shift. Eventually I may create such a method for my menubar, which would provide integrity if I cannot use StringVars.
Eg.
from tkinter import *
Win= Tk()
Mydict= {"command": StringVar(master= Win)}
Menu1= Menu(master= Win)
Menu1.add_command(label= Mydict["command"], command= print)
This doesn't work obviously.

Questions:
Does anyone know how to get StringVar-s into menu?
Does anyone know more parameters for tkinter.Menu's .add_...() methods.
#Update: I've got to know a Menubutton, which can hold StringVar(), but its children (content) are still problematic. Can I pack a normal widget into Menu? Eg. button, that would solve the problem. How?
Reply


Messages In This Thread
tkinter.Menu – How to make text-variable? - by Sir - Mar-07-2021, 09:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  make widgets disappear from tkinter jacksfrustration 12 1,130 Feb-06-2024, 03:58 PM
Last Post: deanhystad
  pass a variable between tkinter and toplevel windows janeik 10 2,329 Jan-24-2024, 06:44 AM
Last Post: Liliana
  [Tkinter] Updating tkinter text BliepMonster 5 5,970 Nov-28-2022, 01:42 AM
Last Post: deanhystad
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,858 Jun-26-2022, 06:26 PM
Last Post: menator01
  Why I am getting ModuleNotFoundError when I make an Exe file for tkinter GUI? pymn 0 1,649 Apr-01-2022, 05:36 PM
Last Post: pymn
  [Tkinter] Update variable using tkinter entry methon drSlump 6 5,202 Oct-15-2021, 08:01 AM
Last Post: drSlump
  tkinter change the text of the checkbox zazas321 1 3,830 Sep-17-2021, 06:19 AM
Last Post: zazas321
  [Tkinter] It it possible to make a help file explorer with tkinter? Clunk_Head 0 1,983 Aug-07-2021, 06:02 PM
Last Post: Clunk_Head
  [Tkinter] Make my button text update? Skata100 1 2,035 Aug-07-2021, 05:37 AM
Last Post: deanhystad
  Updating button text based upon different variable values knoxvilles_joker 0 2,234 Apr-18-2021, 04:13 AM
Last Post: knoxvilles_joker

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020