有这个代码
from tkinter import *
from re_text import *
root=Tk()
def func(event):
print("123")
def fi(event):
top_read(root,l['text'],l)
l=Label(root, text="hello")
l.bind("<Button-1>", fi)
l.pack()
_list=Listbox(root)
_list.insert(END, "test element")
_list.bind("<<ListboxSelect>>", func)
_list.pack()
root.mainloop()
re_text 模块:
from tkinter import Toplevel, Text, FLAT, Tk, END
from tkinter.ttk import Button
class top_read(Toplevel):
def __init__(self, master, text, widget):
self.text = text
self.widget = widget
super().__init__(master)
self.title("Редактировать строку")
self.gui()
def gui(self):
self.geometry("680x300")
self.ob = Text(self, relief=FLAT, height=17, width=72)
self.ob.insert('end',self.text)
self.b = Button(self, text="Сохранить", command=self._command_for_button)
self.b.place(x=600,y=261)
self.ob.place(x=15,y=9)
def _command_for_button(self):
self.widget['text'] = self.ob.get(1.0,END)[:-1]
self.destroy()
问题:
当单击带有文本 hello 的标签后,编辑窗口按预期出现。如果您在此窗口中选择文本,那么 func 函数将以某种神奇的方式(或不是)启动。如果我们允许将 ListboxSelect 替换为 Button-1,则不会观察到“魔法”。
请帮助理解。
当您选择一个元素
ListboxSelect
时,将执行一项功能func
,直到此选择终止,此功能将再次重复第二次。为了解决所有问题,请使用信号<ButtonRelease-1>
或<Button-1>
.代码示例: