1

I don't know why but in each places where strings should be displayed I have PY_VAR# instead (# being a number). For instance instead of displaying "This area shows messages coming from the slave" it displays PY_VAR1. my code is

#!/usr/bin/python
# -*- coding: utf-8 -*-

import tkinter

class Display(tkinter.Tk):
    def __init__(self,parent):
        tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid()

        self.MessageString = tkinter.StringVar()
        MessageDisplay = tkinter.Label(self, text = self.MessageString, anchor = 'nw' , justify = 'left')
        MessageDisplay.grid(columnspan = 3)

        self.ClockString = tkinter.StringVar()
        ClockDisplay = tkinter.Label(self, text = self.ClockString)
        ClockDisplay.grid(row=0)

        self.Menu1Title = tkinter.StringVar()
        Menu1Button = tkinter.Button(self, text = self.Menu1Title, command = self.SetMenu(MenuIndex = 1))
        Menu1Button.grid(column = 0, columnspan = 2)

        self.Menu2Title = tkinter.StringVar()
        Menu2Button = tkinter.Button(self, text = self.Menu2Title, command = self.SetMenu(MenuIndex = 2))
        Menu2Button.grid(column = 0, columnspan = 2)

        self.Menu3Title = tkinter.StringVar()
        Menu3Button = tkinter.Button(self, text = self.Menu3Title, command = self.SetMenu(MenuIndex = 3))
        Menu3Button.grid(column = 0, columnspan = 2)

        self.MenuDescriptionContent = tkinter.StringVar()
        MenuDescription = tkinter.Label(self, text = self.MenuDescriptionContent, anchor = 'nw', justify = 'left')
        MenuDescription.grid(column = 2, columnspan = 2, row = 1, rowspan = 3)

        """Temporary variables"""

        self.MessageString = "This area shows messages coming from the slave"
        self.ClockString = "00:00"
        self.Menu1Title = "Menu 1"
        self.Menu2Title = "Menu 2"
        self.Menu3Title = "Menu 3"
        self.MenuDescriptionContent = "This area shows the description of the selected menu"

    def SetMenu(self, MenuIndex):
        pass

if __name__ == "__main__":
app = Display(None)
app.title('Web Bell')
app.mainloop()

I don't have any error displayed in the console. thank you in advance for your responses

1 Answer 1

2

If you want the value of a StringVar you must use the get method:

MessageDisplay = tkinter.Label(..., text = self.MessageString.get(), ...)

The only time you don't need to do that is if you're using the StringVar as the value for a textvariable attribute.

You have another problem in your code. When you do self.MessageString = "This area..." you aren't setting the text variable, you are essentially throwing it away and changing self.MessageString to refer to a string.

Instead, you need to call the set method of the variable, like this:

self.MessageString.set("This area...")
Sign up to request clarification or add additional context in comments.

2 Comments

while using textvariable instead of text or the get method it don't display anything.
@user2675968: your other problem is that you aren't setting them to anything. To give one of these variables a value you must use the set method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.