#πŸ”’ Variable doesn't changes

15 messages Β· Page 1 of 1 (latest)

craggy matrix
#

I have two classes. One is ScrollListbox witch contains current_ind. Another class named OptionSelector contains ScrollListbox object and needs current_ind
Problem is that ScrollListBox changes current_ind correctly, but OptionSelector always get current_ind as 0 value
There are crucial part of code

class ScrollListbox(tk.Frame):
    def __init__(self, master=None, **kwargs):
    super().__init__(master)

        self.current_ind = 0

        self.listbox = tk.Listbox(self, selectmode=tk.SINGLE, **kwargs)
        ...

    def select_next(self, event):
        self.current_ind = (self.current_ind + 1) % self.listbox.size()
        self.select_line(self.current_ind)

    def select_previous(self, event):
        self.current_ind = (self.current_ind - 1) % self.listbox.size()
        self.select_line(self.current_ind)
    ...

class OptionSelector(tk.Frame):
    def __init__(self, master=None, **kwargs):
        super().__init__(master)

        self.selected_line_ind: int = self.scroll_listbox.current_ind
        
        self.scroll_listbox.bind_all('<Return>', self.select_line)
        ...
    def select_line(self, event):
        print(self.scroll_listbox.current_ind)
        self.selected_line_ind = self.scroll_listbox.current_ind

Full code in comments

coral saffronBOT
#

@craggy matrix

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

craggy matrix
#
import tkinter as tk
from tkinter import ttk


class ScrollListbox(tk.Frame):
    def __init__(self, master=None, **kwargs):
        super().__init__(master)

        self.current_ind = 0

        self.listbox = tk.Listbox(self, selectmode=tk.SINGLE, **kwargs)
        self.listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

        self.scrollbar = tk.Scrollbar(self, orient=tk.VERTICAL, command=self.listbox.yview)
        self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

        self.listbox.config(yscrollcommand=self.scrollbar.set)

        #call custom events
        self.listbox.bind('<KeyPress-j>', lambda event: self.event_generate('<<Down>>'))
        self.listbox.bind('<Down>', lambda event: self.event_generate('<<Down>>'))
        self.listbox.bind('<KeyPress-k>', lambda event: self.event_generate('<<Up>>'))
        self.listbox.bind('<Up>', lambda event: self.event_generate('<<Up>>'))

        #init default events
        self.listbox.bind('<ButtonRelease-1>', self.on_item_click)

        self.bind('<<ListModified>>', self.on_list_modified)
        self.bind('<<Down>>', self.select_next)
        self.bind('<<Up>>', self.select_previous)

#
    def insert(self, index, *elements):
        self.listbox.insert(index, *elements)
        self.event_generate('<<ListModified>>')

    def delete(self, first, last = None):
        self.listbox.delete(first, last)
        self.event_generate('<<ListModified>>')

    def on_list_modified(self, event): #it isn't used, just learned how it works
        print('Modified') 

    def on_item_click(self, event):
        selection = self.listbox.curselection()
        if selection:
            self.current_ind = selection[0]

    def select_next(self, event):
        self.current_ind = (self.current_ind + 1) % self.listbox.size()
        self.select_line(self.current_ind)

    def select_previous(self, event):
        self.current_ind = (self.current_ind - 1) % self.listbox.size()
        self.select_line(self.current_ind)

    def select_line(self, line_ind: int):
        self.listbox.select_clear(0, tk.END)
        self.listbox.select_set(line_ind)
        self.listbox.activate(line_ind)

    def set_current_line_highlight(self, enable: bool):
        self.set_line_highlight(self.current_ind, enable)

    def set_line_highlight(self, line_ind: int, enable: bool):
        if enable:
            self.listbox.itemconfig(line_ind, bg='lavender')
        else:
            self.listbox.itemconfig(line_ind, bg='lightgray')
#
class OptionSelector(tk.Frame):
    def __init__(self, master=None, **kwargs):
        super().__init__(master)

        self.label = tk.Label(self)
        self.label.pack(side=tk.TOP, fill=tk.X)

        self.scroll_listbox = ScrollListbox(self, **kwargs)
        self.scroll_listbox.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)

        self.selected_line_ind: int = self.scroll_listbox.current_ind

        self.scroll_listbox.bind_all('<Return>', self.select_line)

    def set_name(self, name: str):
        self.label.config(text=name)

    def insert(self, index, *elements):
        self.scroll_listbox.insert(index, *elements)

    def delete(self, first, last = None):
        self.scroll_listbox.delete(first, last)

    def select_line(self, event):
        self.scroll_listbox.set_line_highlight(self.selected_line_ind, False)
        self.scroll_listbox.set_current_line_highlight(True)
        print(self.scroll_listbox.current_ind)
        self.selected_line_ind = self.scroll_listbox.current_ind
ionic plinth
#

In the select_line you first print the old value (0) and only then tell the object to set self.selected_line_ind to the new current value of the current_ind

Have you tried printing the current_ind after the assignment? To see that it changes?

ionic plinth
#

So does it change?
If yes, just move the assignment to be first, then highlight. Because right now you're highlighting the old value

craggy matrix
#

Okey... I found out that problem isn't in this classes. Because I have tested it with simple ui

#

I think I should close this issue

#

!close

coral saffronBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.