473,441 Members | 2,130 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,441 software developers and data experts.

Tkinter: Strange behavior using place() and changing cursors

I was trying to design a widget that I could drag and drop anywhere in
a frame and then resize by pulling at the edges with the mouse. I have
fiddled with several different approaches and came across this behavior
when using the combination of place() and configure(cursor = ...) This
problem doesn't occur if you remove either one of these elements.

It's a very basic design of a button wrapped in a sizer frame and is
supposed to work like any window that can be resized. The mouse will
change into a resize cursor when it hits the sizer frame (button's
edge).

However when the cursor hits the right edge it gets in some kind of
loop where it keeps entering and leaving the frame. You can also cause
this to happen by entering the widget from the bottom and slowly moving
down. However it doesn't happen if you enter from the left or the top.

Here's the code, boiled down to the basic components that seem to
affect it in some way. Is there something I'm supposed to do in order
to prevent this from happening?

Thanks,
Marc

from Tkinter import *

class Gui:
def __init__(self, master):
master.geometry("200x100")
btn = Widget(master)

class Widget:
def __init__(self, master):
self.master = master
self.buildFrame()
self.buildWidget()

def buildFrame(self):
self.f = Frame(self.master, height=32, width=32, relief=RIDGE,
borderwidth=2)
self.f.place(relx=.5,rely=.5)
self.f.bind( '<Enter>', self.enterFrame )
self.f.bind( '<Leave>', self.leaveFrame )

def buildWidget(self):
self.b = Button(self.f, text="Sure!")
self.b.pack(fill=BOTH, expand=1)

def enterFrame(self, event):
self.f.configure(cursor = 'sb_h_double_arrow')

def leaveFrame(self, event):
self.f.configure(cursor = '' )

root = Tk()
ent = Gui(root)
root.mainloop()

Nov 14 '06 #1
5 1951
Mudcat wrote:
[...]
You have to set cursor once, Tk change it automatically:
def buildFrame(self):
self.f = Frame(self.master, height=32, width=32, relief=RIDGE,
borderwidth=2)
self.f.place(relx=.5,rely=.5)
#self.f.bind( '<Enter>', self.enterFrame )
#self.f.bind( '<Leave>', self.leaveFrame )
self.f.configure(cursor = 'sb_h_double_arrow')
Nov 14 '06 #2

Wojciech Mula wrote:
Mudcat wrote:
[...]

You have to set cursor once, Tk change it automatically:
def buildFrame(self):
self.f = Frame(self.master, height=32, width=32, relief=RIDGE,
borderwidth=2)
self.f.place(relx=.5,rely=.5)
#self.f.bind( '<Enter>', self.enterFrame )
#self.f.bind( '<Leave>', self.leaveFrame )
self.f.configure(cursor = 'sb_h_double_arrow')

The problem is I need the ability to change it dynamically. I don't
want the cursor to be the double_arrow the whole time the mouse hovers
inside that frame. It's supposed to be a resize arrow when the mouse is
on the frame border, and regular cursor once it passes to the inner
part of the frame.

Unless there is a better way to do this, I have written code to
determine if the mouse is on the edge or not. There doesn't seem to be
a 'mouseover' type event to determine if the mouse is currently on the
frame border itself. As a result, I calculate the position of the mouse
and set the cursor appropriately.

I have also determined that this is not a problem if the button is not
packed inside the frame. So somehow the interaction of the internal
button is causing this problem.

Nov 14 '06 #3
Mudcat wrote:
I have also determined that this is not a problem if the button is not
packed inside the frame. So somehow the interaction of the internal
button is causing this problem.
Problem is really strange, and seems to be a Tk issue, not Tkinter.
I've observed that if method configure is called and **any** parameter
of frame is changed, then frame "forgets" all its children, gets natural
size and then pack children again! This just(?) causes flickering, and
do not depend on geometry manager --- all place, pack and grid work in
this way. But only place causes the problem --- I guess it do not block
events, or generate some events.

Workaround:

class Widget:
def __init__(self, master):
self.master = master
self.buildFrame()
self.buildWidget()
self.x = 0
self.y = 0

# [snip]

def enterFrame(self, event):
if self.x != event.x or self.y != event.y:
self.f.configure(cursor = 'sb_h_double_arrow')
self.x, self.y = event.x, event.y

def leaveFrame(self, event):
if self.x != event.x or self.y != event.y:
self.f.configure(cursor = '')
self.x, self.y = event.x, event.y

w.
Nov 14 '06 #4
Mudcat wrote:
Is there something I'm supposed to do in order
to prevent this from happening?
Yes. Instead of configuring the cursor on the frame, do it on the master:

self.master.configure(cursor='sb_h_double_arrow')


--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Nov 15 '06 #5
On Tue, 14 Nov 2006 17:45:52 +0100, Mudcat <mn******@gmail.comwrote:
The problem is I need the ability to change it dynamically. I don't
want the cursor to be the double_arrow the whole time the mouse hovers
inside that frame. It's supposed to be a resize arrow when the mouse is
on the frame border, and regular cursor once it passes to the inner
part of the frame.
Why don't you create a Frame for the inside with no border, and another
Frame for the border? This way, you can have a cursor setting for each of
them, i.e one for the inside and one for the border.

HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
Nov 15 '06 #6

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

Similar topics

3
by: Gary Richardson | last post by:
I would like to define a new cursor for use on a Canvas (other than one of those listed in Appendix F of "Python and Tkinter Programming"). A search on Google turned up one bit of code that seemed...
1
by: Philippe C. Martin | last post by:
Hi, I have the following convern: I have Tkinter applications that require a zoom box, and have had the following behavior without changing a line of code: 1) Mandrake 10.0/KDE 3.2/Python 2.3:...
6
by: William Gill | last post by:
A short while ago someone posted that(unlike the examples) you should use Tk as the base for your main window in tkinter apps, not Frame. Thus : class MyMain(Frame): def __init__(self,...
5
by: Sean McIlroy | last post by:
hi all i recently wrote a script that implements a puzzle. the interface mostly consists of a bunch of colored disks on a tkinter canvas. the problem is that the disks change their colors in...
4
by: Praveen_db2 | last post by:
Hi All I am getting strange errors in my db2diag.log can any one tell me what these errors mean?? Following is the code from my db2diag.log...
1
by: jmdeschamps | last post by:
I made a document on Tkinter cursors (mouse pointers?), pairing the cursor image with its name... Not a great deal of magic here since anyone can program something to see the different cursors on...
4
by: Chris | last post by:
Hi, I'm puzzled by some strange behavior when my Python/Tkinter application quits (on linux): the terminal from which I started Python is messed up. If start up python, then import the code...
2
by: erasmus | last post by:
It seems that in some cases the callback (command) function in a TkInter Button runs automatically when drawn for the first time. Example: b=Button(frame, text="Click me",command=myFunction())...
0
by: Guilherme Polo | last post by:
On Thu, Sep 18, 2008 at 1:06 PM, April Lekin <lekin2@llnl.govwrote: Yes. You could separate them in two labels (if you are using labels), or apply a different tag if you are using a Text...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.