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!
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.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
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 toself.canvas
, but yourSketchpad
class inherits fromtk.Canvas
. This meansself
is the canvas. This means that all references toself.canvas
should be changed toself
.The
**kwargs
in yourSketchpad
initialization is passed directly to thetk.Canvas
’s initialization. This means that any valid Canvas configuration can be passed as keyword arguments when you create aSketchpad
instance.You can try out the following:
In the code above, when initializing the
Sketchpad
instance, I added two keyword arguments as an example:bg="white"
andhighlightthickness=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