Tkinter function calling on its own












2















Using tkinter, I'm trying to make a ball appear on the screen then disappear (become the same color as the background) upon the "w" key being clicked, but it immediately disappears upon running the program.



I think this may be due to the structuring of the code, with the function ball_disappear before the variables, but I didn't notice a difference when I put the function below it. Here's the code.



from tkinter import *

root = Tk()

height = 700
width = 1000
canvas = Canvas(width=width, height=height)
canvas_background = '#63fff9'
canvas.configure(background=canvas_background)
root.title = "Ball Bouncer Tk"

def ball_disappear():
canvas.itemconfig(ball, fill=canvas_background, outline=canvas_background)
canvas.update()

ballP1 = 400
ballP2 = 400
ballP3 = 600
ballP4 = 600

ball = canvas.create_oval(ballP1, ballP2, ballP3, ballP4, fill='#000000', tags="ball")

canvas.pack()
canvas.bind("w", ball_disappear())
canvas.mainloop()









share|improve this question




















  • 1





    Change ball_disappear() to ball_disappear in canvas.bind(...).

    – acw1668
    Jan 1 at 4:44











  • When I do that it doesn't make the ball turn to the background when I click "w".

    – B S
    Jan 1 at 4:45
















2















Using tkinter, I'm trying to make a ball appear on the screen then disappear (become the same color as the background) upon the "w" key being clicked, but it immediately disappears upon running the program.



I think this may be due to the structuring of the code, with the function ball_disappear before the variables, but I didn't notice a difference when I put the function below it. Here's the code.



from tkinter import *

root = Tk()

height = 700
width = 1000
canvas = Canvas(width=width, height=height)
canvas_background = '#63fff9'
canvas.configure(background=canvas_background)
root.title = "Ball Bouncer Tk"

def ball_disappear():
canvas.itemconfig(ball, fill=canvas_background, outline=canvas_background)
canvas.update()

ballP1 = 400
ballP2 = 400
ballP3 = 600
ballP4 = 600

ball = canvas.create_oval(ballP1, ballP2, ballP3, ballP4, fill='#000000', tags="ball")

canvas.pack()
canvas.bind("w", ball_disappear())
canvas.mainloop()









share|improve this question




















  • 1





    Change ball_disappear() to ball_disappear in canvas.bind(...).

    – acw1668
    Jan 1 at 4:44











  • When I do that it doesn't make the ball turn to the background when I click "w".

    – B S
    Jan 1 at 4:45














2












2








2








Using tkinter, I'm trying to make a ball appear on the screen then disappear (become the same color as the background) upon the "w" key being clicked, but it immediately disappears upon running the program.



I think this may be due to the structuring of the code, with the function ball_disappear before the variables, but I didn't notice a difference when I put the function below it. Here's the code.



from tkinter import *

root = Tk()

height = 700
width = 1000
canvas = Canvas(width=width, height=height)
canvas_background = '#63fff9'
canvas.configure(background=canvas_background)
root.title = "Ball Bouncer Tk"

def ball_disappear():
canvas.itemconfig(ball, fill=canvas_background, outline=canvas_background)
canvas.update()

ballP1 = 400
ballP2 = 400
ballP3 = 600
ballP4 = 600

ball = canvas.create_oval(ballP1, ballP2, ballP3, ballP4, fill='#000000', tags="ball")

canvas.pack()
canvas.bind("w", ball_disappear())
canvas.mainloop()









share|improve this question
















Using tkinter, I'm trying to make a ball appear on the screen then disappear (become the same color as the background) upon the "w" key being clicked, but it immediately disappears upon running the program.



I think this may be due to the structuring of the code, with the function ball_disappear before the variables, but I didn't notice a difference when I put the function below it. Here's the code.



from tkinter import *

root = Tk()

height = 700
width = 1000
canvas = Canvas(width=width, height=height)
canvas_background = '#63fff9'
canvas.configure(background=canvas_background)
root.title = "Ball Bouncer Tk"

def ball_disappear():
canvas.itemconfig(ball, fill=canvas_background, outline=canvas_background)
canvas.update()

ballP1 = 400
ballP2 = 400
ballP3 = 600
ballP4 = 600

ball = canvas.create_oval(ballP1, ballP2, ballP3, ballP4, fill='#000000', tags="ball")

canvas.pack()
canvas.bind("w", ball_disappear())
canvas.mainloop()






python tkinter graphics






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 8:52









martineau

68.5k1090183




68.5k1090183










asked Jan 1 at 4:28









B SB S

254




254








  • 1





    Change ball_disappear() to ball_disappear in canvas.bind(...).

    – acw1668
    Jan 1 at 4:44











  • When I do that it doesn't make the ball turn to the background when I click "w".

    – B S
    Jan 1 at 4:45














  • 1





    Change ball_disappear() to ball_disappear in canvas.bind(...).

    – acw1668
    Jan 1 at 4:44











  • When I do that it doesn't make the ball turn to the background when I click "w".

    – B S
    Jan 1 at 4:45








1




1





Change ball_disappear() to ball_disappear in canvas.bind(...).

– acw1668
Jan 1 at 4:44





Change ball_disappear() to ball_disappear in canvas.bind(...).

– acw1668
Jan 1 at 4:44













When I do that it doesn't make the ball turn to the background when I click "w".

– B S
Jan 1 at 4:45





When I do that it doesn't make the ball turn to the background when I click "w".

– B S
Jan 1 at 4:45












1 Answer
1






active

oldest

votes


















3














Consider this line of code:



canvas.bind("w", ball_disappear())


It has exactly the same effect as this:



result = ball_disappear()
canvas.bind("w", result)


When you bind an event, you must give it a callable. Typically that takes the form of a reference to a function, though it can also be the result of a call to lambda or functools.partial, or even your own function if that function returns another function.



Thus, the proper way to bind ball_disappear is like this:



canvas.bind("w", ball_disappear)


Your code still won't work, however, because of two other errors in your code.



First, the canvas doesn't get keyboard events by default. You must explicitly give it the keyboard focus, so sometime after creating the canvas you need to do this:



canvas.focus_set()


Second, when you bind a function to an event, the function that is called will be passed an object that has information about the event. Thus, you need to define your function like the following even if you don't plan to use the parameter in your code:



def ball_disappear(event):





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993006%2ftkinter-function-calling-on-its-own%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Consider this line of code:



    canvas.bind("w", ball_disappear())


    It has exactly the same effect as this:



    result = ball_disappear()
    canvas.bind("w", result)


    When you bind an event, you must give it a callable. Typically that takes the form of a reference to a function, though it can also be the result of a call to lambda or functools.partial, or even your own function if that function returns another function.



    Thus, the proper way to bind ball_disappear is like this:



    canvas.bind("w", ball_disappear)


    Your code still won't work, however, because of two other errors in your code.



    First, the canvas doesn't get keyboard events by default. You must explicitly give it the keyboard focus, so sometime after creating the canvas you need to do this:



    canvas.focus_set()


    Second, when you bind a function to an event, the function that is called will be passed an object that has information about the event. Thus, you need to define your function like the following even if you don't plan to use the parameter in your code:



    def ball_disappear(event):





    share|improve this answer




























      3














      Consider this line of code:



      canvas.bind("w", ball_disappear())


      It has exactly the same effect as this:



      result = ball_disappear()
      canvas.bind("w", result)


      When you bind an event, you must give it a callable. Typically that takes the form of a reference to a function, though it can also be the result of a call to lambda or functools.partial, or even your own function if that function returns another function.



      Thus, the proper way to bind ball_disappear is like this:



      canvas.bind("w", ball_disappear)


      Your code still won't work, however, because of two other errors in your code.



      First, the canvas doesn't get keyboard events by default. You must explicitly give it the keyboard focus, so sometime after creating the canvas you need to do this:



      canvas.focus_set()


      Second, when you bind a function to an event, the function that is called will be passed an object that has information about the event. Thus, you need to define your function like the following even if you don't plan to use the parameter in your code:



      def ball_disappear(event):





      share|improve this answer


























        3












        3








        3







        Consider this line of code:



        canvas.bind("w", ball_disappear())


        It has exactly the same effect as this:



        result = ball_disappear()
        canvas.bind("w", result)


        When you bind an event, you must give it a callable. Typically that takes the form of a reference to a function, though it can also be the result of a call to lambda or functools.partial, or even your own function if that function returns another function.



        Thus, the proper way to bind ball_disappear is like this:



        canvas.bind("w", ball_disappear)


        Your code still won't work, however, because of two other errors in your code.



        First, the canvas doesn't get keyboard events by default. You must explicitly give it the keyboard focus, so sometime after creating the canvas you need to do this:



        canvas.focus_set()


        Second, when you bind a function to an event, the function that is called will be passed an object that has information about the event. Thus, you need to define your function like the following even if you don't plan to use the parameter in your code:



        def ball_disappear(event):





        share|improve this answer













        Consider this line of code:



        canvas.bind("w", ball_disappear())


        It has exactly the same effect as this:



        result = ball_disappear()
        canvas.bind("w", result)


        When you bind an event, you must give it a callable. Typically that takes the form of a reference to a function, though it can also be the result of a call to lambda or functools.partial, or even your own function if that function returns another function.



        Thus, the proper way to bind ball_disappear is like this:



        canvas.bind("w", ball_disappear)


        Your code still won't work, however, because of two other errors in your code.



        First, the canvas doesn't get keyboard events by default. You must explicitly give it the keyboard focus, so sometime after creating the canvas you need to do this:



        canvas.focus_set()


        Second, when you bind a function to an event, the function that is called will be passed an object that has information about the event. Thus, you need to define your function like the following even if you don't plan to use the parameter in your code:



        def ball_disappear(event):






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 1 at 5:22









        Bryan OakleyBryan Oakley

        219k22266429




        219k22266429
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993006%2ftkinter-function-calling-on-its-own%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            SQL update select statement

            'app-layout' is not a known element: how to share Component with different Modules