Tyron Marshall
Tyron Marshall

Reputation: 13

Tkinter bind only works once

I cannot find any reason that this bind isn't working. I have tried setting focus, moving the bind outside of the class, among other things. The strange thing is it works once at the beginning of the program. This leads me to believe that the bind does work but not as intended.

import tkinter as tk
import os
import Themes

##########################################################################
root = tk.Tk()

topleft = tk.Frame(root,
               bg = 'red',
               )

topmid = tk.Frame(root,
              bg = 'white',
              )

topright = tk.Frame(root,
                bg = 'blue',
                )

bottomleft = tk.Frame(root,
                  bg = 'yellow',
                  )

bottommid = tk.Frame(root,
                  bg = 'green',
                  )

bottomright = tk.Frame(root,
                    bg = 'black',
                    )

tk.Grid.rowconfigure(root, 0, weight = 20)
tk.Grid.columnconfigure(root, 0, weight = 30)
topleft.grid(row = 0, column = 0,
         sticky = tk.N+tk.E+tk.S+tk.W,
         rowspan = 2, columnspan = 1)

tk.Grid.columnconfigure(root, 1, weight = 20)
topmid.grid(row = 0, column = 1,
        sticky = tk.N+tk.E+tk.S+tk.W,
        rowspan = 1, columnspan = 2)

tk.Grid.columnconfigure(root, 3, weight = 85)
topright.grid(row = 0, column = 3,
          sticky = tk.N+tk.E+tk.S+tk.W,
          rowspan = 3, columnspan = 1)

tk.Grid.rowconfigure(root, 2, weight = 40)
bottomleft.grid_propagate(False)
bottomleft.grid(row = 2, column = 0,
            sticky = tk.N+tk.E+tk.S+tk.W,
            rowspan = 1, columnspan = 3)

tk.Grid.rowconfigure(root, 1, weight = 10)
tk.Grid.columnconfigure(root, 1, weight = 10)
bottommid.grid(row = 1, column = 1,
           sticky = tk.N+tk.E+tk.S+tk.W,
           rowspan = 1, columnspan = 1)

tk.Grid.rowconfigure(root, 1, weight = 10)
tk.Grid.columnconfigure(root, 2, weight = 10)
bottomright.grid(row = 1, column = 2,
             sticky = tk.N+tk.E+tk.S+tk.W,
             rowspan = 1, columnspan = 1)
##########################################################################

class File_selector:
    def __init__(self):
    self.scrollbar = tk.Scrollbar(bottomleft,
                                  orient = 'vertical'
                                  )
    self.listbox = tk.Listbox(bottomleft,
                              yscrollcommand = self.scrollbar.set
                              )
    self.check = print(self.listbox.curselection())

    self.scrollbar['command'] = self.listbox.yview

    for file in os.listdir(os.curdir):
        if file.endswith(".py"):
            self.listbox.insert(tk.END, file)

    self.listbox.bind('<<ListboxSelect>>', lambda e: self.check)

    self.grid()
    def grid(self):      
    self.scrollbar.grid(row = 0, column = 1, sticky = tk.N+tk.S)

    tk.Grid.rowconfigure(bottomleft, 0, weight = 1)
    tk.Grid.columnconfigure(bottomleft, 0, weight = 1)
    self.listbox.grid(row = 0, column = 0,
                      sticky = tk.N+tk.E+tk.S+tk.W)

File_selector()
root.mainloop()

Upvotes: 0

Views: 1168

Answers (1)

furas
furas

Reputation: 142651

Line

self.check = print(self.listbox.curselection())

is incorrect.

It runs print() and prints text on screen (when File_selector object is created) and then it assigns None to self.check (because print() returns None)

You need normal function

def check(self, event):
    print(self.listbox.curselection())

or lambda function

self.check = lambda event:print(self.listbox.curselection())

And then you can bind

self.listbox.bind('<<ListboxSelect>>', self.check)

Upvotes: 1

Related Questions