Git Product home page Git Product logo

Comments (4)

dipeshSam avatar dipeshSam commented on May 29, 2024

@sim2511 Button should be focused first.
Use .focus() before detecting the pressing action.

...
self.button.focus()

Note that every time you need to focus the button whenever you want to listen key events from this button.

from customtkinter.

sim2511 avatar sim2511 commented on May 29, 2024

Hello @dipeshSam thanks for your reply

i tried but still not works, i'm on 5.2.0 customtkinter version, the focus method just return "none"

class W1(customtkinter.CTk):
    def __init__(self):
        super().__init__()
        customtkinter.set_appearance_mode("Light")
        self.geometry("550x150")
        self.title("W1")
        
        self.button = customtkinter.CTkButton(self, text="W1", command=self.open_w2)
        self.button.grid(row=0, column=0)
        self.button.focus()
        self.button.bind('<Return>', self.open_w2)

    def open_w2(self):
        print('hello')

from customtkinter.

sim2511 avatar sim2511 commented on May 29, 2024

I found a solution, hope it can help someone else ! (add event=None in your function parameter) and try with bind_all method instead of bind, it worked for me:

class W1(customtkinter.CTk):
    def __init__(self):
        super().__init__()
        customtkinter.set_appearance_mode("Light")
        self.geometry("550x150")
        self.title("W1")
        
        self.button = customtkinter.CTkButton(self, text="W1", command=self.open_w2)
        self.button.grid(row=0, column=0)
        self.bind_all('<Return>', self.open_w2)

    def open_w2(self, event=None):
        print('hello')


if __name__ == "__main__":
    root_1 = W1()
    root_1.mainloop() 

from customtkinter.

sim2511 avatar sim2511 commented on May 29, 2024

also work with bind, the event parameters is the only thing you need to do

class W1(customtkinter.CTk):
    def __init__(self):
        super().__init__()
        customtkinter.set_appearance_mode("Light")
        self.geometry("550x150")
        self.title("W1")
        
        self.button = customtkinter.CTkButton(self, text="W1", command=self.open_w2)
        self.button.grid(row=0, column=0)
        self.bind('<Return>', self.open_w2)

    def open_w2(self, event=None):
        print('hello')


if __name__ == "__main__":
    root_1 = W1()
    root_1.mainloop() 
    

from customtkinter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.