繁体   English   中英

Tkinter绑定只能使用一次

[英]Tkinter bind only works once

我找不到此绑定无法正常工作的任何原因。 我尝试过设置焦点,将绑定移出类,等等。 奇怪的是,它在程序开始时只能运行一次。 这使我相信绑定确实有效,但没有达到预期的效果。

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()

线

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

是不正确的。

它运行print()并在屏幕上打印文本(创建File_selector对象时),然后将None分配给self.check (因为print()返回None

您需要正常的功能

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

lambda函数

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

然后你可以绑定

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM