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!
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()
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!
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:
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
.
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
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.