admin管理员组文章数量:1431391
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.
本文标签: pythonIssue with customtkinter buttons using callback functions that set focusStack Overflow
版权声明:本文标题:python - Issue with customtkinter buttons using callback functions that set focus - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745581610a2664653.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论