So I’m learning python and the book that is teaching me gives me two ways to create a label using tkinker:
self.canvas.create_text(30,10,text="Welcome",tags="text")
&
self.lbl = Label(frame1, text = "Welcome")
In the former example, moving it is easy:
self.canvas.move("text", 1, 0)
In the latter example, changing it’s background color is easy:
self.lbl["bg"] = "red"
However I do not know how to both move it AND change it’s background color in either example, at least not how to move it incrementally. I can do this:
self.lbl.place(x=2)
But unless I can get the x coordinate ahead of time, I can only move it once. I could set it ahead of time, but I’d like to avoid that option if possible.
There are ways to do both.
Firstly,
Canvastext does not have a background, but you can create your own with a rectangle.From there you just move them the same way as you mentioned in your question. Adding a group tag to both the text and rectangle would save you from having to move both items separately.
Secondly, you can get the current x, y coordinates of a widget with the
.winfo_x()and.winfo_y()methods. So moving theLabelbecomes a simple matter of addition/subtraction:I do not know of a method that moves a widget in increments as the move method does for the canvas.
As for which is best, I can’t think of much between them. I suppose using a
Canvaswould mean you couldn’t overlap any other widgets that may be in the window, since the text would just scroll out of view, and if you start using thettkversion ofLabelthen styling isn’t quite as straight forward, although it’s not difficult.