Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
78 views
in Technique[技术] by (71.8m points)

python - Single click blocks double click in tkinter

I have a GUI where I bind two events to a label like this:

self.label.bind('<Button-1>', self.single)
self.label.bind('<Double-1>', self.double)

the problem is that, even if I double click the label, the 'double' function isn't called, and the 'single' function is called twice.

It looks like the single click event is blocking the double click event as, if I remove the single click event line, I can normally call the 'double' function.

I read the docs and from what I got it should catch both events, but this is not the case.

Anyway, I tried this simple script and it catches both events

from tkinter import *

def single(event):
   print('single click')

def double(event):       
   print('double click')

widget = Button(None, text='Hello event world')
widget.pack()
widget.bind('<Button-1>', single)
widget.bind('<Double-1>', double)
widget.mainloop()

Then I don't understand why it doesn't work in the first script. I can post the entire file if needed. Thank you :)

Original File: https://github.com/matte980/ExplorerFile/blob/main/ObjectsPublish.py

question from:https://stackoverflow.com/questions/65944956/single-click-blocks-double-click-in-tkinter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok, so the problem is that when you make the first click you are calling move() which calls elements(), which recreates all the widgets. Therefore your second click is the first click to a new widget.

To solve this you need to update the widget properties, instead of recreating the widgets with new properties. For example, to update the background colors:

def select(self, event):
    print('selected')
    for folder in folderList:
        if folder.backgroundColor != '#ffffff':
            folder.chg_background('#ffffff')
    self.chg_background('#ffff00')

def chg_background(self, color):
    self.backgroundColor = color
    self.image.config(bg = color)
    self.label.config(bg = color)
    self.frame.config(bg = color)

This will also solve the problem of your program lagging after long use.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...