473,770 Members | 5,284 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tkinter canvas drag/drop obstacle

Tkinter makes it very easy to drag jpeg images around on a
canvas, but I would like to have a "target" change color when
the cursor dragging an image passes over it. I seem to be
blocked by the fact that the callbacks that might tell the
target that the mouse has entered it (<Enter>, <Any-Enter>,
even <Motion>) aren't called if the mouse's button is down.
What am I missing? Have I failed to find the right Tkinter
document? Is Tkinter the wrong tool for this job? Thanks.

--
To email me, substitute nowhere->spamcop, invalid->net.
Jun 27 '08 #1
7 4713
On Fri, Jun 20, 2008 at 1:11 PM, Peter Pearson <pp******@nowhe re.invalidwrote :
Tkinter makes it very easy to drag jpeg images around on a
canvas, but I would like to have a "target" change color when
the cursor dragging an image passes over it. I seem to be
blocked by the fact that the callbacks that might tell the
target that the mouse has entered it (<Enter>, <Any-Enter>,
even <Motion>) aren't called if the mouse's button is down.
What am I missing? Have I failed to find the right Tkinter
document? Is Tkinter the wrong tool for this job? Thanks.
I believe the only way to achieve this is binding <Motionto the
entire canvas, then checking if the x, y coords are inside the
"target".
--
To email me, substitute nowhere->spamcop, invalid->net.
--
http://mail.python.org/mailman/listinfo/python-list
--
-- Guilherme H. Polo Goncalves
Jun 27 '08 #2
On Jun 20, 9:11*am, Peter Pearson <ppear...@nowhe re.invalidwrote :
Tkinter makes it very easy to drag jpeg images around on a
canvas, but I would like to have a "target" change color when
the cursor dragging an image passes over it. *I seem to be
blocked by the fact that the callbacks that might tell the
target that the mouse has entered it (<Enter>, <Any-Enter>,
even <Motion>) aren't called if the mouse's button is down.
What am I missing? *Have I failed to find the right Tkinter
document? *Is Tkinter the wrong tool for this job? *Thanks.

--
To email me, substitute nowhere->spamcop, invalid->net.
I have used a combination of <Motionand <B1-Motion>. You might also
throw in a <Button-1event to keep track of whether or not the mouse
button was down when it entered the widget or not.

Depending on what you really want to do though, you might take
advantage of the 'active' state:

import Tkinter as tk

can = tk.Canvas()
can.pack(fill=t k.BOTH, expand=True)

can.create_rect angle(
10,10,100,100,
fill="black",
activewidth=5,
activeoutline=" blue"
)

can.mainloop()

The 'active*' options take effect when the mouse is on top of that
item.

If all you are _really_ interested in is a visual indicator, this
should work for you. Note that there is also a disabled state. I only
discovered this by looking at the options available and guessing.
>>from pprint import pprint
import Tkinter as tk
can = tk.Canvas()
can.pack(fill =tk.BOTH, expand=True)
r = can.create_rect angle(10,10,100 ,100)
pprint(can.it emconfig(r))
{'activedash': ('activedash', '', '', '', ''),
'activefill': ('activefill', '', '', '', ''),
'activeoutline' : ('activeoutline ', '', '', '', ''),
'activeoutlines tipple': ('activeoutline stipple', '', '', '', ''),
'activestipple' : ('activestipple ', '', '', '', ''),
'activewidth': ('activewidth', '', '', '0.0', '0.0'),
'dash': ('dash', '', '', '', ''),
'dashoffset': ('dashoffset', '', '', '0', '0'),
'disableddash': ('disableddash' , '', '', '', ''),
'disabledfill': ('disabledfill' , '', '', '', ''),
'disabledoutlin e': ('disabledoutli ne', '', '', '', ''),
'disabledoutlin estipple': ('disabledoutli nestipple', '', '', '', ''),
'disabledstippl e': ('disabledstipp le', '', '', '', ''),
'disabledwidth' : ('disabledwidth ', '', '', '0.0', '0'),
'fill': ('fill', '', '', '', ''),
'offset': ('offset', '', '', '0,0', '0,0'),
'outline': ('outline', '', '', 'black', 'black'),
'outlineoffset' : ('outlineoffset ', '', '', '0,0', '0,0'),
'outlinestipple ': ('outlinestippl e', '', '', '', ''),
'state': ('state', '', '', '', ''),
'stipple': ('stipple', '', '', '', ''),
'tags': ('tags', '', '', '', ''),
'width': ('width', '', '', '1.0', '1.0')}

The 'state' option can be set to 'normal', 'hidden' or 'disabled'. So
if you want to make your canvas items look different when they are
disabled, set the disabled* options and set 'state' to 'disabled'.

Matt
Jun 27 '08 #3
On Jun 20, 11:10*am, Matimus <mccre...@gmail .comwrote:
On Jun 20, 9:11*am, Peter Pearson <ppear...@nowhe re.invalidwrote :
Tkinter makes it very easy to drag jpeg images around on a
canvas, but I would like to have a "target" change color when
the cursor dragging an image passes over it. *I seem to be
blocked by the fact that the callbacks that might tell the
target that the mouse has entered it (<Enter>, <Any-Enter>,
even <Motion>) aren't called if the mouse's button is down.
What am I missing? *Have I failed to find the right Tkinter
document? *Is Tkinter the wrong tool for this job? *Thanks.
--
To email me, substitute nowhere->spamcop, invalid->net.

I have used a combination of <Motionand <B1-Motion>. You might also
throw in a <Button-1event to keep track of whether or not the mouse
button was down when it entered the widget or not.

Depending on what you really want to do though, you might take
advantage of the 'active' state:

import Tkinter as tk

can = tk.Canvas()
can.pack(fill=t k.BOTH, expand=True)

can.create_rect angle(
* * * * 10,10,100,100,
* * * * fill="black",
* * * * activewidth=5,
* * * * activeoutline=" blue"
* * * * )

can.mainloop()

The 'active*' options take effect when the mouse is on top of that
item.

If all you are _really_ interested in is a visual indicator, this
should work for you. Note that there is also a disabled state. I only
discovered this by looking at the options available and guessing.
>from pprint import pprint
import Tkinter as tk
can = tk.Canvas()
can.pack(fill= tk.BOTH, expand=True)
r = can.create_rect angle(10,10,100 ,100)
pprint(can.ite mconfig(r))

{'activedash': ('activedash', '', '', '', ''),
*'activefill': ('activefill', '', '', '', ''),
*'activeoutline ': ('activeoutline ', '', '', '', ''),
*'activeoutline stipple': ('activeoutline stipple', '', '', '', ''),
*'activestipple ': ('activestipple ', '', '', '', ''),
*'activewidth': ('activewidth', '', '', '0.0', '0.0'),
*'dash': ('dash', '', '', '', ''),
*'dashoffset': ('dashoffset', '', '', '0', '0'),
*'disableddash' : ('disableddash' , '', '', '', ''),
*'disabledfill' : ('disabledfill' , '', '', '', ''),
*'disabledoutli ne': ('disabledoutli ne', '', '', '', ''),
*'disabledoutli nestipple': ('disabledoutli nestipple', '', '', '', ''),
*'disabledstipp le': ('disabledstipp le', '', '', '', ''),
*'disabledwidth ': ('disabledwidth ', '', '', '0.0', '0'),
*'fill': ('fill', '', '', '', ''),
*'offset': ('offset', '', '', '0,0', '0,0'),
*'outline': ('outline', '', '', 'black', 'black'),
*'outlineoffset ': ('outlineoffset ', '', '', '0,0', '0,0'),
*'outlinestippl e': ('outlinestippl e', '', '', '', ''),
*'state': ('state', '', '', '', ''),
*'stipple': ('stipple', '', '', '', ''),
*'tags': ('tags', '', '', '', ''),
*'width': ('width', '', '', '1.0', '1.0')}

The 'state' option can be set to 'normal', 'hidden' or 'disabled'. So
if you want to make your canvas items look different when they are
disabled, set the disabled* options and set 'state' to 'disabled'.

Matt
I appologize. I didn't actually test this before posting the code, but
if you have the mouse button down before entering an item on the
canvas, even the active state doesn't seem apply. So, well, I hope
someone finds this information useful, but I guess it isn't going to
solve the original posters issue.

Matt
Jun 27 '08 #4

I have this class:

class Vector(object):
TOL = 1e-5
def __eq__( self, other, tolerance=TOL ):
print tolerance
shortened for clarity obviously. so I want to subclass this class like
so:

class BigVector(Vecto r)
TOL = 100
for example if I was working with large vectors which I knew would never
be very close hence the large tolerance. this doesn't work however -
the TOL class variable, while overridden in BigVector, is still using
the Vector.TOL variable in the __eq__ method.
which kinda makes sense to a certain degree, but how do I get the
behaviour where doing:

BigVector().__e q__( otherVec )
prints 100 instead of 1e-5?

does this question make sense? not sure how clearly I'm phrasing my
question... any of you guys python experts?
I *could* do this, but its ugly:

class Vector(object):
TOL = 1e-5
def __eq__( self, other, tolerance=None ):
if tolerance is None: tolerance = self.TOL
print tolerance
Jun 27 '08 #5
On Fri, Jun 20, 2008 at 6:19 PM, Hamish McKenzie
<ha****@valveso ftware.comwrote :
>
I have this class:

class Vector(object):
TOL = 1e-5
def __eq__( self, other, tolerance=TOL ):
print tolerance
shortened for clarity obviously. so I want to subclass this class like
so:

class BigVector(Vecto r)
TOL = 100
for example if I was working with large vectors which I knew would never
be very close hence the large tolerance. this doesn't work however -
the TOL class variable, while overridden in BigVector, is still using
the Vector.TOL variable in the __eq__ method.
which kinda makes sense to a certain degree, but how do I get the
behaviour where doing:

BigVector().__e q__( otherVec )
No, don't do this. Just do "avector == othervector"
>

prints 100 instead of 1e-5?

does this question make sense? not sure how clearly I'm phrasing my
question... any of you guys python experts?
I *could* do this, but its ugly:

class Vector(object):
TOL = 1e-5
def __eq__( self, other, tolerance=None ):
if tolerance is None: tolerance = self.TOL
print tolerance
class Vector(object):
TOL = 1e-5
def __eq__(self, other):
print self.TOL
>
--
http://mail.python.org/mailman/listinfo/python-list


--
-- Guilherme H. Polo Goncalves
Jun 27 '08 #6
On Fri, 20 Jun 2008 13:41:35 -0300, Guilherme Polo <gg****@gmail.c omwrote:
On Fri, Jun 20, 2008 at 1:11 PM, Peter Pearson <pp******@nowhe re.invalidwrote :
>Tkinter makes it very easy to drag jpeg images around on a
canvas, but I would like to have a "target" change color when
the cursor dragging an image passes over it. I seem to be
blocked by the fact that the callbacks that might tell the
target that the mouse has entered it (<Enter>, <Any-Enter>,
even <Motion>) aren't called if the mouse's button is down.
What am I missing? Have I failed to find the right Tkinter
document? Is Tkinter the wrong tool for this job? Thanks.

I believe the only way to achieve this is binding <Motionto the
entire canvas, then checking if the x, y coords are inside the
"target".
Ugh. OK, thanks.

--
To email me, substitute nowhere->spamcop, invalid->net.
Jun 27 '08 #7
Preamble: when posting a brand new question, you'd better not replying to
an existing completely unrelated message. In most viewers, this will cause
your message to appear in the thread for the original question and far
less people will see it. So better create a brand new thread.

On Fri, 20 Jun 2008 23:19:37 +0200, Hamish McKenzie
<ha****@valveso ftware.comwrote :
I have this class:

class Vector(object):
TOL = 1e-5
def __eq__( self, other, tolerance=TOL ):
print tolerance
shortened for clarity obviously. so I want to subclass this class like
so:

class BigVector(Vecto r)
TOL = 100
for example if I was working with large vectors which I knew would never
be very close hence the large tolerance. this doesn't work however -
the TOL class variable, while overridden in BigVector, is still using
the Vector.TOL variable in the __eq__ method.
which kinda makes sense to a certain degree, but how do I get the
behaviour where doing:

BigVector().__e q__( otherVec )
prints 100 instead of 1e-5?

does this question make sense? not sure how clearly I'm phrasing my
question... any of you guys python experts?
There's just no way. The default values for function/method arguments are
evaluated when the function definition is interpreted. When the __eq__
method is defined, TOL is 1e-5, so that will be the value used in the
method, whatever you may do afterwards.
>
I *could* do this, but its ugly:

class Vector(object):
TOL = 1e-5
def __eq__( self, other, tolerance=None ):
if tolerance is None: tolerance = self.TOL
print tolerance
Well, ugliness is in the eye of the beholder... ;-) Even if you find it
ugly, that's the Python way to do it.

HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz 5(17l8(%,5.Z*(9 3-965$l7+-'])"
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5984
by: Josh | last post by:
Caution, newbie approaching... I'm trying to come up with a very simple Tkinter test application that consists of a window with a drop-down menu bar at the top and a grid of colored rectangles filling the remainder of the window. Mind you, this is a contrived test application to help me understand Tkinter and Python, not an actual application yet. I've trivially subclassed Tkinter.Canvas into ColorCanvas, added a bunch of ColorCanvases...
8
4424
by: Marc | last post by:
Hi all, I was thinking about developing a drag and drop application and was curious how difficult it is to do. Basically I want to have a set of objects that I move around the screen and drop into place. Anyone else ever done it? Got any tips? Thanks, Marc
5
5562
by: Sean McIlroy | last post by:
The subject line essentially says it all. Is there a way to do it? If so, where do I look to find out how? Any help will be much appreciated. Thx, STM
1
2976
by: syed_saqib_ali | last post by:
Please take a look at and run the code snippet shown below. It creates a canvas with vertical & Horizontal scroll-bars. If you shrink the window to smaller than the area of the canvas, the scroll-bars work as advertised. That's great. However, if you click the Left Mouse button, it calls code which expands the width of the canvas by 100 pixels. The area being viewed expands correspondingly..... BUT I DON'T WANT IT TO!!
44
5003
by: bg_ie | last post by:
Hi, I'm in the process of writing some code and noticed a strange problem while doing so. I'm working with PythonWin 210 built for Python 2.5. I noticed the problem for the last py file processed by this script, where the concerned tmp file is only actually written to when PythonWin is closed. In other words, after I run this script, one of the generated tmp files has a size of 0kB. I then close PythonWin and it is then written to.
11
3571
by: Kenneth McDonald | last post by:
Any guesses as to how many people are still using Tkinter? And can anyone direct me to good, current docs for Tkinter? Thanks, Ken
1
5302
by: asharda | last post by:
Hi, I have a listbox on my WPF window and a cnavas. I have to be able to drag the items from the list box and drop them on the canvas and draw the item accorgingly. If anyone knows how to accomplish it, then please let me know. Any help will be greatly appreciated. Thanks for you time in advance.
3
2983
by: joshdw4 | last post by:
I hate to do this, but I've thoroughly exhausted google search. Yes, it's that pesky root window and I have tried withdraw to no avail. I'm assuming this is because of the methods I'm using. I guess my question is two-fold. 1) How do I get rid of that window? 2) Any comments in general? I am just learning python (and coding with classes), so I'm sure there are things I should pound into my head before I learn bad habits. Here's the...
1
192
by: Eric_Dexter | last post by:
I saw this (close to this anyway) lieing around on the internet and was wanting to use it to define a start point exc but I need the graphics to stay within a set y coords and I am not realy sure how to do that. I have no idea on how to bind a min/max y to it. (the concept is inspired by the java csound blue). #!/usr/bin/python from Tkinter import * import csoundroutines as cs
0
9591
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10225
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10001
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7415
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.