Moving defined objects in python canvas





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I want ot move a defined object in canvas. I know there's a command that moves an object(.move) hovewer it only works on individual items. So how could I move a whole defined object made up of rectangles?
Like the one in the example? Because I need to move hundreds of little objects as one.



x=400
y=400



def player(x,y):
canvas.create_rectangle(x,y,x+50,y+50,fill='black')
canvas.create_rectangle(x,y+50,x+150,y+150,fill='red')


def moveright(coordinates2):
global x
global y
x=x+200
y=y+0
player(x,y)

def moveleft(coordinates3):
global x
global y
x=x-200
y=y+0
player(x,y)

def moveup(coordinates4):
global x
global y
x=x+0
y=y-150
player(x,y)

def moveright(coordinates5):
global x
global y
x=x+0
y=y+150
player(x,y)



canvas.bind_all('<Right>',moveright)
canvas.bind_all('<Left>',moveleft)
canvas.bind_all('<Up>',moveup)
canvas.bind_all('<Down>',movedown)









share|improve this question

























  • Please try to reduce this down to a Minimal, Complete, and Verifiable example We don't need all the code for all the bindings, just the ones related to moving. We also don't need dozens of canvas items when just one or two will do for the purpose of this question.

    – Bryan Oakley
    Nov 26 '18 at 4:27


















0















I want ot move a defined object in canvas. I know there's a command that moves an object(.move) hovewer it only works on individual items. So how could I move a whole defined object made up of rectangles?
Like the one in the example? Because I need to move hundreds of little objects as one.



x=400
y=400



def player(x,y):
canvas.create_rectangle(x,y,x+50,y+50,fill='black')
canvas.create_rectangle(x,y+50,x+150,y+150,fill='red')


def moveright(coordinates2):
global x
global y
x=x+200
y=y+0
player(x,y)

def moveleft(coordinates3):
global x
global y
x=x-200
y=y+0
player(x,y)

def moveup(coordinates4):
global x
global y
x=x+0
y=y-150
player(x,y)

def moveright(coordinates5):
global x
global y
x=x+0
y=y+150
player(x,y)



canvas.bind_all('<Right>',moveright)
canvas.bind_all('<Left>',moveleft)
canvas.bind_all('<Up>',moveup)
canvas.bind_all('<Down>',movedown)









share|improve this question

























  • Please try to reduce this down to a Minimal, Complete, and Verifiable example We don't need all the code for all the bindings, just the ones related to moving. We also don't need dozens of canvas items when just one or two will do for the purpose of this question.

    – Bryan Oakley
    Nov 26 '18 at 4:27














0












0








0








I want ot move a defined object in canvas. I know there's a command that moves an object(.move) hovewer it only works on individual items. So how could I move a whole defined object made up of rectangles?
Like the one in the example? Because I need to move hundreds of little objects as one.



x=400
y=400



def player(x,y):
canvas.create_rectangle(x,y,x+50,y+50,fill='black')
canvas.create_rectangle(x,y+50,x+150,y+150,fill='red')


def moveright(coordinates2):
global x
global y
x=x+200
y=y+0
player(x,y)

def moveleft(coordinates3):
global x
global y
x=x-200
y=y+0
player(x,y)

def moveup(coordinates4):
global x
global y
x=x+0
y=y-150
player(x,y)

def moveright(coordinates5):
global x
global y
x=x+0
y=y+150
player(x,y)



canvas.bind_all('<Right>',moveright)
canvas.bind_all('<Left>',moveleft)
canvas.bind_all('<Up>',moveup)
canvas.bind_all('<Down>',movedown)









share|improve this question
















I want ot move a defined object in canvas. I know there's a command that moves an object(.move) hovewer it only works on individual items. So how could I move a whole defined object made up of rectangles?
Like the one in the example? Because I need to move hundreds of little objects as one.



x=400
y=400



def player(x,y):
canvas.create_rectangle(x,y,x+50,y+50,fill='black')
canvas.create_rectangle(x,y+50,x+150,y+150,fill='red')


def moveright(coordinates2):
global x
global y
x=x+200
y=y+0
player(x,y)

def moveleft(coordinates3):
global x
global y
x=x-200
y=y+0
player(x,y)

def moveup(coordinates4):
global x
global y
x=x+0
y=y-150
player(x,y)

def moveright(coordinates5):
global x
global y
x=x+0
y=y+150
player(x,y)



canvas.bind_all('<Right>',moveright)
canvas.bind_all('<Left>',moveleft)
canvas.bind_all('<Up>',moveup)
canvas.bind_all('<Down>',movedown)






python tkinter tkinter-canvas






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 20:31







Mátyás Neilinger

















asked Nov 24 '18 at 23:45









Mátyás NeilingerMátyás Neilinger

62




62













  • Please try to reduce this down to a Minimal, Complete, and Verifiable example We don't need all the code for all the bindings, just the ones related to moving. We also don't need dozens of canvas items when just one or two will do for the purpose of this question.

    – Bryan Oakley
    Nov 26 '18 at 4:27



















  • Please try to reduce this down to a Minimal, Complete, and Verifiable example We don't need all the code for all the bindings, just the ones related to moving. We also don't need dozens of canvas items when just one or two will do for the purpose of this question.

    – Bryan Oakley
    Nov 26 '18 at 4:27

















Please try to reduce this down to a Minimal, Complete, and Verifiable example We don't need all the code for all the bindings, just the ones related to moving. We also don't need dozens of canvas items when just one or two will do for the purpose of this question.

– Bryan Oakley
Nov 26 '18 at 4:27





Please try to reduce this down to a Minimal, Complete, and Verifiable example We don't need all the code for all the bindings, just the ones related to moving. We also don't need dozens of canvas items when just one or two will do for the purpose of this question.

– Bryan Oakley
Nov 26 '18 at 4:27












1 Answer
1






active

oldest

votes


















0














Unlike what you said in the question, move does work for groups of items if you use tags: canvas.move(<tag or id>, x, y).



Here is an example:



import tkinter as tk

root = tk.Tk()
canvas = tk.Canvas(root)
canvas.pack()


def move():
# move all items with the 'group' tag
canvas.move('group', 10, 10)


canvas.create_rectangle(10, 10, 30, 30, tags=['group'])
canvas.create_rectangle(20, 40, 50, 70, tags=['group'])
canvas.create_rectangle(60, 50, 80, 60, tags=['group'])

tk.Button(root, text='Move', command=move).pack()
root.mainloop()





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%2f53463403%2fmoving-defined-objects-in-python-canvas%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









    0














    Unlike what you said in the question, move does work for groups of items if you use tags: canvas.move(<tag or id>, x, y).



    Here is an example:



    import tkinter as tk

    root = tk.Tk()
    canvas = tk.Canvas(root)
    canvas.pack()


    def move():
    # move all items with the 'group' tag
    canvas.move('group', 10, 10)


    canvas.create_rectangle(10, 10, 30, 30, tags=['group'])
    canvas.create_rectangle(20, 40, 50, 70, tags=['group'])
    canvas.create_rectangle(60, 50, 80, 60, tags=['group'])

    tk.Button(root, text='Move', command=move).pack()
    root.mainloop()





    share|improve this answer




























      0














      Unlike what you said in the question, move does work for groups of items if you use tags: canvas.move(<tag or id>, x, y).



      Here is an example:



      import tkinter as tk

      root = tk.Tk()
      canvas = tk.Canvas(root)
      canvas.pack()


      def move():
      # move all items with the 'group' tag
      canvas.move('group', 10, 10)


      canvas.create_rectangle(10, 10, 30, 30, tags=['group'])
      canvas.create_rectangle(20, 40, 50, 70, tags=['group'])
      canvas.create_rectangle(60, 50, 80, 60, tags=['group'])

      tk.Button(root, text='Move', command=move).pack()
      root.mainloop()





      share|improve this answer


























        0












        0








        0







        Unlike what you said in the question, move does work for groups of items if you use tags: canvas.move(<tag or id>, x, y).



        Here is an example:



        import tkinter as tk

        root = tk.Tk()
        canvas = tk.Canvas(root)
        canvas.pack()


        def move():
        # move all items with the 'group' tag
        canvas.move('group', 10, 10)


        canvas.create_rectangle(10, 10, 30, 30, tags=['group'])
        canvas.create_rectangle(20, 40, 50, 70, tags=['group'])
        canvas.create_rectangle(60, 50, 80, 60, tags=['group'])

        tk.Button(root, text='Move', command=move).pack()
        root.mainloop()





        share|improve this answer













        Unlike what you said in the question, move does work for groups of items if you use tags: canvas.move(<tag or id>, x, y).



        Here is an example:



        import tkinter as tk

        root = tk.Tk()
        canvas = tk.Canvas(root)
        canvas.pack()


        def move():
        # move all items with the 'group' tag
        canvas.move('group', 10, 10)


        canvas.create_rectangle(10, 10, 30, 30, tags=['group'])
        canvas.create_rectangle(20, 40, 50, 70, tags=['group'])
        canvas.create_rectangle(60, 50, 80, 60, tags=['group'])

        tk.Button(root, text='Move', command=move).pack()
        root.mainloop()






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 '18 at 8:28









        j_4321j_4321

        6,21321631




        6,21321631
































            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%2f53463403%2fmoving-defined-objects-in-python-canvas%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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini