0

I'm making an editor with tkinter, I've made a status bar and i have 2 labels to show the actual line and column, and the total number of lines but the text of the labels don't change anything.

My code is very very long to show here. I figure out the line, column and lines with these 2 functions:

def get_position(self, event=None):
        """get the line and column number of the text insertion point"""
        self.line = tk.StringVar()
        self.column = tk.StringVar()
        self.line, self.column = self.textView.index('insert').split('.')
        self.s = tk.StringVar()
        self.s.set(('Line : {0} - Column : {1}'.format(self.line, self.column)))
        print(self.s)
        return self.s

def getwindowlines(self, event=None):
    self.numberoflines = int(self.textView.index('end-1c').split('.')[0])
    return self.numberoflines

And the function of my status bar is the next one:

def statusBar(self):
        self.frameStatus = tk.Frame(self.master, border=2, bg='#272822',
            relief='sunken')
        self.frameStatus.pack(side='bottom', after=self.toolbar,
            fill='x', padx=5, pady=1)
        numberoflinestxt = str(self.getwindowlines())
        self.labelNumberOfLines = tk.Label(self.frameStatus,
            text='Lines: {0} '.format(numberoflinestxt))
        self.labelNumberOfLines.configure(bg='#272822', fg='white')
        self.labelNumberOfLines.pack(side='right', fill='x', padx=10, pady=2)
        self.labelLinePosition = tk.Label(self.frameStatus,
            textvariable=self.get_position())
        self.labelLinePosition.configure(bg='#272822', fg='white')
        self.labelLinePosition.pack(side='left', fill='x', padx=10, pady=2)

All the code is in Github Code Link in the file IdlePlus.py With print console all works fine, but with a Label the numbers of lines and columns don't change. Thanks

1 Answer 1

2

Your code doesn't seem to be trying to change the labels in the statusbar. I don't understand why you think they should change. In your get_position function you're creating new StringVars each time it is called.

I wouldn't use StringVars at all here, though you can. If you want to use them, you create them exactly once and associate them with Label widgets, and then whenever you want the labels to change, you change the variables. If you want to use .format(...), you have to call that when you change the values, not when you create the label.

For example:

def statusBar(self):
    ...
    self.line = tk.StringVar()
    self.column = tk.StringVar()
    self.labelLinePosition = tk.Label(self.frameStatus,
        textvariable=self.self.column)
    self.labelLinePosition = tk.Label(self.frameStatus,
        textvariable=self.column)
    ...

def get_position(self):
    line, column = self.textView.index('insert').split('.')
    self.line.set("Line: {0}".format(line))
    self.column.set("Position: {0}".format(column)

That will cause the labels to update every time get_position is called.

However, there's really no need for the special StringVars. You can directly set the text of the label, eliminating a couple of objects and thus reducing the complexity of your code slightly:

def statusbar(self):
    ...
    self.labelNumberOfLines = tk.Label(self.frameStatus)
    self.labelLinePosition = tk.Label(self.frameStatus)
    ...

def get_position(self):
    line, column = self.textView.index('insert').split('.')
    self.labelNumberOfLines.configure(text="Lines: {0}".format(lines))
    self.labelLinePosition.configure(text="Character: {0}".format(column))

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.