Question

How can I connect ***kwargs and Canvas?

How can I connect ***kwargs and Canvas? I studied Digitalocean *args and **kwargs, but this is where I got stuck. What dictionary argument does **kwargs expect? Please help! Thanks!

image alt text

Windows, Python, tkinter, Canvas.

import tkinter as tk

class Sketchpad(tk.Canvas):
    def __init__(self**,** parent**,** **kwargs):
        super().__init__(parent**,** **kwargs)

        self.bind("<Button-1>"**,** self.save_posn)
        self.bind("<B1-Motion>"**,** self.add_line)
        self.lastx**,** self.lasty = **0****,** **0
**        self.color = "black"

        szin_tegla = self.canvas.create_rectangle((**10****,** **10****,** **30****,** **30**)**,** fill="red")
        self.canvas.tag_bind(szin_tegla**,** "<Button-1>"**,** lambda x: self.set_color("red"))

        szin_tegla = self.canvas.create_rectangle((**10****,** **35****,** **30****,** **55**)**,** fill="blue")
        self.canvas.tag_bind(szin_tegla**,** "<Button-1>"**,** lambda x: self.set_color("blue"))

        szin_tegla = self.canvas.create_rectangle((**10****,** **60****,** **30****,** **80**)**,** fill="black")
        self.canvas.tag_bind(szin_tegla**,** "<Button-1>"**,** lambda x: self.set_color("black"))

    def save_posn(self**,** event):
        self.lastx**,** self.lasty = event.x**,** event.y

    def set_color(self**,** newcolor):
        self.color = newcolor

    def add_line(self**,** event):
        self.create_line((self.lastx**,** self.lasty**,** event.x**,** event.y))
        self.save_posn(event)


root = tk.Tk()
root.columnconfigure(**0****,** weight=**1**)
root.rowconfigure(**0****,** weight=**1**)

sketch = Sketchpad(root)
sketch.grid(column=**0****,** row=**0****,** sticky='nwes')

root.mainloop()

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
August 21, 2023
Accepted Answer

Hi there,

As far as I can tell from the code you provided, there seem to be a few things that might be causing the issue for you:

  1. In the initialization of the Sketchpad class, you are referring to self.canvas, but your Sketchpad class inherits from tk.Canvas. This means self is the canvas. This means that all references to self.canvas should be changed to self.

  2. The **kwargs in your Sketchpad initialization is passed directly to the tk.Canvas’s initialization. This means that any valid Canvas configuration can be passed as keyword arguments when you create a Sketchpad instance.

You can try out the following:

import tkinter as tk

class Sketchpad(tk.Canvas):
    def __init__(self, parent, **kwargs):
        super().__init__(parent, **kwargs)
        
        self.bind("<Button-1>", self.save_posn)
        self.bind("<B1-Motion>", self.add_line)
        self.lastx, self.lasty = 0, 0
        self.color = "black"

        szin_tegla = self.create_rectangle(10, 10, 30, 30, fill="red")
        self.tag_bind(szin_tegla, "<Button-1>", lambda x: self.set_color("red"))

        szin_tegla = self.create_rectangle(10, 35, 30, 55, fill="blue")
        self.tag_bind(szin_tegla, "<Button-1>", lambda x: self.set_color("blue"))

        szin_tegla = self.create_rectangle(10, 60, 30, 80, fill="black")
        self.tag_bind(szin_tegla, "<Button-1>", lambda x: self.set_color("black"))

    def save_posn(self, event):
        self.lastx, self.lasty = event.x, event.y

    def set_color(self, newcolor):
        self.color = newcolor

    def add_line(self, event):
        self.create_line(self.lastx, self.lasty, event.x, event.y, fill=self.color)
        self.save_posn(event)


root = tk.Tk()
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

sketch = Sketchpad(root, bg="white", highlightthickness=0)
sketch.grid(column=0, row=0, sticky='nwes')

root.mainloop()

In the code above, when initializing the Sketchpad instance, I added two keyword arguments as an example: bg="white" and highlightthickness=0. This sets the background of the canvas to white and removes the highlight border around the canvas, respectively. This is how you can leverage **kwargs to pass configuration options.

Hope that this helps!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more