Python Forum
tkinter moving an class object with keybinds
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter moving an class object with keybinds
#6
The main purpose of a class is to define a template for reusable code. Your ship class can never be used anywhere that doesn't have a canvas named c1, a main window named r, and worst of all, an instance of ship named s. It is rare that a class references an instance of itself unless it is a singleton, or maintains a list of instances for some reason (pool allocation for example). My inclination is to write the code like this:
from tkinter import *

class Ship():
    def __init__(self, parent, wide, high):
        self.canvas = Canvas(parent, width=wide, height=high)
        self.canvas.pack()
        self.img = PhotoImage(file = "test_image.png")
        img_wide = self.img.width()
        img_high = self.img.height()
        self.xmax = wide - img_wide
        self.ymax = high - img_high
        self.x = int((wide - img_wide) / 2)
        self.y = int((high - img_high) / 2)
        self.img_id = self.canvas.create_image(self.x, self.y, anchor=NW, image=self.img)

    def move(self, x, y):
        x = max(-self.x, min(x, self.xmax - self.x))
        y = max(-self.y, min(y, self.ymax - self.y))
        self.x += x
        self.y += y
        self.canvas.move(self.img_id, x, y)
 
root = Tk()
ship = Ship(root, 500, 400)
root.bind("<Right>", lambda event: ship.move(x=5, y=0))
root.bind("<Left>", lambda event: ship.move(x=-5, y=0))
root.bind("<Up>", lambda event: ship.move(x=0, y=-5))
root.bind("<Down>", lambda event: ship.move(x=0, y=5))
root.mainloop()
I also changed things so the ship starts out in the middle of the sea (canvas) and is prevented from sailing off the edge.
Reply


Messages In This Thread
RE: tkinter moving an class object with keybinds - by deanhystad - Feb-10-2021, 09:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter AttributeError: 'GUI' object has no attribute pfdjhfuys 3 1,628 May-18-2023, 03:30 PM
Last Post: pfdjhfuys
  Tkinter object scope riversr54 6 2,035 Feb-17-2023, 05:40 AM
Last Post: deanhystad
Lightbulb Using Tkinter With Concurrent.Futures / ThreadPoolExecutor Class AaronCatolico1 1 1,507 Dec-14-2022, 08:01 PM
Last Post: deanhystad
Lightbulb [Tkinter] Tkinter Class Import Module Issue AaronCatolico1 6 3,213 Sep-06-2022, 03:37 PM
Last Post: AaronCatolico1
  [Tkinter] Redirecting all print statements from all functions inside a class to Tkinter Anan 1 2,691 Apr-24-2021, 08:57 AM
Last Post: ndc85430
  [Tkinter] Troubles with accessing attr from other class zarize 3 2,669 Aug-20-2020, 06:05 PM
Last Post: deanhystad
  [Tkinter] Use function from other class (Tkinter) zarize 8 4,936 Aug-17-2020, 09:47 AM
Last Post: zarize
  [Tkinter] how to draw dynamic moving scale and potting trace point on waveform in tkinter pytho sameer_1985 0 2,049 May-31-2020, 01:52 PM
Last Post: sameer_1985
  Unable fetch fucntion data in class in tkinter jenkins43 2 3,909 Nov-30-2019, 09:47 PM
Last Post: jenkins43
  Tkinter Class pythonenthusiast2 1 2,656 Nov-24-2019, 03:51 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020