Tkinter custom drawing

  • Thread starter =?iso-8859-1?q?Xavier_B=E9rard?=
  • Start date
?

=?iso-8859-1?q?Xavier_B=E9rard?=

Hello everyone,

I am wondering if there is a way to use custom drawing in Tkinter.
I've been using it for few months, and all I know about custom drawing
is to draw directly on a Canvas with such methods as "create_line",
"create_rectangle", etc.

Now, the problem, is that I have already plenty of widgets on my
screen. I just want to draw over them, which is a bit difficult in my
comprehension of things.

My perfect solution was to put temporary invisible Canvas when I want
do use draw methods, but such thing doesn't exist (as far as I could
search in this community's posts).

Anyone have a clue ?

Thanks,
Xavier Berard
 
K

Kevin Walzer

Xavier said:
Now, the problem, is that I have already plenty of widgets on my
screen. I just want to draw over them, which is a bit difficult in my
comprehension of things.

What are you trying to achieve by "drawing over" widgets?
 
J

James Stroud

Xavier said:
Hello everyone,

I am wondering if there is a way to use custom drawing in Tkinter.
I've been using it for few months, and all I know about custom drawing
is to draw directly on a Canvas with such methods as "create_line",
"create_rectangle", etc.

Now, the problem, is that I have already plenty of widgets on my
screen. I just want to draw over them, which is a bit difficult in my
comprehension of things.

My perfect solution was to put temporary invisible Canvas when I want
do use draw methods, but such thing doesn't exist (as far as I could
search in this community's posts).

Anyone have a clue ?

Thanks,
Xavier Berard

Are you sure you are spelling it right? The C is not capitalized:

from Tkinter import Invisiblecanvas

etc.

James
 
?

=?iso-8859-1?q?Xavier_B=E9rard?=

What are you trying to achieve by "drawing over" widgets?


Want I want to do is a sort of GUI builder for Tkinter. I already
finished a rough version, but for now I'm making a lighter version of
this project. So, my intent, is to create a widget under the widget.
While dragging the mouse, I want to see this rectangle that defines
the boundaries of the new widget I'm creating.

Sorry for being unclear.
 
?

=?iso-8859-1?q?Xavier_B=E9rard?=

from Tkinter import Invisiblecanvas

?

The whole web never mentions this Invisiblecanvas.
Do you have anything alike to share ? ;)

Xavier
 
J

James Stroud

Xavier said:
?

The whole web never mentions this Invisiblecanvas.
Do you have anything alike to share ? ;)

Xavier

I figured that if you were sincere, you'd call me on this one.
 
J

James Stroud

Xavier said:
Want I want to do is a sort of GUI builder for Tkinter. I already
finished a rough version, but for now I'm making a lighter version of
this project. So, my intent, is to create a widget under the widget.
While dragging the mouse, I want to see this rectangle that defines
the boundaries of the new widget I'm creating.

Sorry for being unclear.

You may want to look into the place() method. The python mega widgets
(PMW) has a PanedWidget that implements this smoothly. You may want to
emulate that approach:


#! /usr/bin/env python

from Tkinter import *

def button_pressed(e):
moved = e.widget
moved.move_pending = True
moved['cursor'] = 'hand1'
moved.press_x = e.x
moved.press_y = e.y

def button_moved(e):
moved = e.widget
if moved.move_pending:
moved.after_idle(lambda e=e: _button_moved(e))
moved.move_pending = False

def _button_moved(e):
moved = e.widget
delta_x = e.x - moved.press_x
delta_y = e.y - moved.press_y
size, wx, wy = moved.winfo_geometry().split('+')
new_x = int(wx) + delta_x
new_y = int(wy) + delta_y
moved.place(x=new_x, y=new_y)
moved.update_idletasks()
moved.move_pending = True

def button_up(e):
e.widget['cursor'] = ''

def register(widget):
widget.bind('<ButtonPress-3>', button_pressed)
widget.bind('<B3-Motion>', button_moved)
widget.bind('<Any-ButtonRelease-3>', button_up)
widget.update_idletasks()

def test():
tk = Tk()

b = Button(tk, text='Button')
b.pack()
c = Button(tk, text='Another Button')
c.pack()
x = Label(tk, text='Drag Me', relief=RIDGE, border=1)
register(x)
x.pack()

tk.geometry('200x200')
tk.mainloop()

if __name__ == "__main__":
test()


James
 
?

=?iso-8859-1?q?Xavier_B=E9rard?=

Thank you this is nice code. I never thought of using the move_pending
method..

Still it doesn't answer my question (which I ensure is very unclear).
But do not worry, I found some way to get throught my dilemma and I
can live easily with it. Thanks for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top