python - Issue with customtkinter buttons using callback functions that set focus - Stack Overflow

I am relatively new to using customtkinter, though have worked with some other gui development tools in

I am relatively new to using customtkinter, though have worked with some other gui development tools in the past, but one thing that I have noticed is that it seems that sometimes and only sometimes when pressing a CtkButton with a callback function that creates a popup window and calls popup.focus_set(), the child '!label' of the CtkButton will take focus from the popup window as soon as the popup window appears. Some example code might look like:

import customtkinter

class MainWindow(customtkinter.CTk):
    def __init__(self):
        super().__init__()
        my_button = customtkinter.CTkButton(self, text='Popup Window')
        my_button.grid(row=0, column=0)
        my_button.bind('<ButtonRelease-1>', lambda x: self.show_popup())

        self.bind_all("<Button-1>", lambda event: self.set_focus(event))
        
    def set_focus(self, event):
        if not isinstance(event.widget, str):
            event.widget.focus_set()

    def show_popup(self):
        popup_window = customtkinter.CTkToplevel(self)
        popup_window.focus_set()


if __name__ == "__main__":
    app = MainWindow()
    app.mainloop()

I've noticed that when clicking anywhere on the button besides the label, everything will work fine and the popup window will take focus after popping up. However, sometimes when clicking on the actual label on the button, the button will take back focus after the popup opens. Is there a way around this?

For additional context, the .bind_all() is being used because customtkinter otherwise will not focus on things that are being clicked. Also, a use case for this could be trying to implement a popup that would close when clicking back to the main window.

So far I have tried disabling the child label inside of the CTkButtons that I am instantiating, but that doesn't seem to take away the ability for the labels to take focus.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745581610a4634287.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信