Home > Mobile >  Ttk label not displaying TextVar
Ttk label not displaying TextVar

Time:12-10

I'm making a GUI with Tkinter (Tcl/Tk version 8.6.12) on Python 3.9. Up until now, I mostly had experience with pure Tk, but wanted to try Ttk out. My problem is that running:

someText = tk.StringVar(value="Some Text")
ttk.Label(mainframe, textvariable=someText).grid(column=0, row=0, sticky=(W, E))

does reserve some space for the string, but doesn't display it. When using the tk.Label class, it works like a charm...

What am I doing wrong, or is the ttk.Label class broken?

EDIT: here is my full MNWE (I wrapped everything in a function because I will need to integrate everything with some other code I have already written and this is the best way I've found to do it):

from tkinter import *
from tkinter import ttk

def generateGUI():
    # Generates the main window
    root = Tk()
    root.title("[INF131] Black Jack")
    root.resizable(FALSE, FALSE)

    # Adds a frame for themed tk
    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)   # setup the frame to scale with the window horizontally
    root.rowconfigure(0, weight=1)      # setup the frame to scale with the window vertically

    # Creates a canvas
    canv = Canvas(mainframe, bg="green")
    canv.grid(column=0, row=0, sticky=(N, E, S, W))

    # Sets some information panel up
    info = ttk.Frame(mainframe)
    info.grid(column=1, row=0, sticky=(N, E))
    title = StringVar(value="<title>")
    text1 = StringVar(value="<text1>")
    text2 = StringVar(value="<text2>")
    text3 = StringVar(value="<text3>")
    # Adds the different labels
    ttk.Label(info, font=25, textvariable=title).grid(column=0, row=0, sticky=N, columnspan=2)
    ttk.Label(info, text="Info 1: ").grid(column=0, row=1, sticky=W)
    ttk.Label(info, text="Info 2: ").grid(column=0, row=2, sticky=W)
    ttk.Label(info, text="Info 3: ").grid(column=0, row=3, sticky=W)
    ttk.Label(info, textvariable=text1).grid(column=1, row=1, sticky=E)
    ttk.Label(info, textvariable=text2).grid(column=1, row=2, sticky=E)
    ttk.Label(info, textvariable=text3).grid(column=1, row=3, sticky=E)


    for child in mainframe.winfo_children(): 
        child.grid_configure(padx=5, pady=5)

    return root

gui = generateGUI()
gui.mainloop()

CodePudding user response:

Your instance of StringVar is a local variable. Make it/them an instance variable if you're using classes, or a global variable if not.

def generateGUI():
    global title
    ...
    title = StringVar(value="<title>")
    ttk.Label(info, font=25, textvariable=title)

CodePudding user response:

This example code is working. You need to import ttk and change remove sticky. why_no_sticky - good example on the link how grid works.

from tkinter import ttk
import tkinter as tk

mainframe = tk.Tk()

someText = tk.StringVar(value="Some Text")
label1 = ttk.Label(mainframe, textvariable=someText)
label1.grid(column=0, row=0)

mainframe.mainloop()
  • Related