最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin7浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论