943,613 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1475
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 25th, 2008
0

How do I get the current widget in focus without changing focus?

Expand Post »
i'm trying to implement my own edit menu using Tkinter,

my cut and copy methods,... seems to work fine but I'm having allot of trouble getting my paste method to work. will someone please help?

(((its kinda ugly i know, but i'm still new to programming and i'm more focused on getting it
to work right. later I'll go back and really clean up my code and oop practice. )))


app['info']['name']
app['info']['balance']

are both entry widgets

app['info']['item'][ # ]
app['info']['cost'][ # ]
app['info']['date'][ # ]
are lists of entry widgets

def _copy(self):
app=self.app

if app['info']['name'].select_present():
if app['info']['name'].focus_get():
app['info']['name'].clipboard_clear()
app['info']['name'].clipboard_append( app['info']['name'].selection_get() )


elif app['info']['balance'].select_present():
if app['info']['balance'].focus_get():
app['info']['balance'].clipboard_clear()
app['info']['balance'].clipboard_append( app['info']['balance'].selection_get() )

else:
i = 0
for i in range(200):
app['catagory_info']['item'][i].clipboard_clear()
app['catagory_info']['cost'][i].clipboard_clear()
app['catagory_info']['date'][i].clipboard_clear()

if app['catagory_info']['item'][i].select_present():
if app['catagory_info']['item'][i].focus_get():
app['catagory_info']['item'][i].clipboard_append(
app['catagory_info']['item'][i].selection_get() )

elif app['catagory_info']['cost'][i].select_present():
if app['catagory_info']['cost'][i].focus_get():
app['catagory_info']['cost'][i].clipboard_append(
app['catagory_info']['cost'][i].selection_get() )

elif app['catagory_info']['date'][i].select_present():
if app['catagory_info']['date'][i].focus_get():
app['catagory_info']['date'][i].clipboard_append(
app['catagory_info']['date'][i].selection_get() )


#####################################################################################
def _cut(self):
app=self.app

x=0
if app['info']['name'].select_present():
if app['info']['name'].focus_get():
app['info']['name'].clipboard_clear()
app['info']['name'].clipboard_append( app['info']['name'].selection_get() )
app['info']['name'].delete(SEL_FIRST,SEL_LAST)

elif app['info']['balance'].select_present():
if app['info']['balance'].focus_get():
app['info']['balance'].clipboard_clear()
app['info']['balance'].clipboard_append( app['info']['balance'].selection_get() )
app['info']['balance'].delete(SEL_FIRST,SEL_LAST)

else:
i = 0
for i in range(200):
app['catagory_info']['item'][i].clipboard_clear()
app['catagory_info']['cost'][i].clipboard_clear()
app['catagory_info']['date'][i].clipboard_clear()

if app['catagory_info']['item'][i].select_present():
if app['catagory_info']['item'][i].focus_get():
app['catagory_info']['item'][i].clipboard_append(
app['catagory_info']['item'][i].selection_get() )
app['catagory_info']['item'][i].delete(SEL_FIRST,SEL_LAST)

elif app['catagory_info']['cost'][i].select_present():
if app['catagory_info']['cost'][i].focus_get():
app['catagory_info']['cost'][i].clipboard_append(
app['catagory_info']['cost'][i].selection_get() )
app['catagory_info']['cost'][i].delete(SEL_FIRST,SEL_LAST)

elif app['catagory_info']['date'][i].select_present():
if app['catagory_info']['date'][i].focus_get():
app['catagory_info']['date'][i].clipboard_append(
app['catagory_info']['date'][i].selection_get() )
app['catagory_info']['date'][i].delete(SEL_FIRST,SEL_LAST)

##########################################################
def _paste(self):
if app['info']['name'].focus_get():
app['info']['name'].insert(INSERT, app['info']['name'].clipboard_get())

if app['info']['balance'].focus_get():
app['info']['balance'].insert(INSERT, app['info']['balance'].clipboard_get())

...
...
...

<<<< this _paste method doesn't work, it always sets the focus to app['info']['name']
no mater where i paste at. How do I query where the insertion point is?

please someone help me. I've been going through Tkinter tutorials for over a week now, but i can not find a solution and i feel like its one of those really simple things that just keeps aluding me.

thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
imabut is offline Offline
4 posts
since Oct 2008
Oct 27th, 2008
0

Re: How do I get the current widget in focus without changing focus?

Wrap your code in code tags:
[code=python]
# Code here
[/code]
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Oct 29th, 2008
0

Re: How do I get the current widget in focus without changing focus?

Click to Expand / Collapse  Quote originally posted by jlm699 ...
Wrap your code in code tags:
[code=python]
# Code here
[/code]
please elaborate,

the snippet above looks like <html> ... I'm useing python's IDLE

please leave an example
Reputation Points: 10
Solved Threads: 0
Newbie Poster
imabut is offline Offline
4 posts
since Oct 2008
Oct 29th, 2008
0

Re: How do I get the current widget in focus without changing focus?

he was saying if you expect us to look at your code, put that before and after your code in your post. so that we can see syntax highlighting and what not.
Last edited by tyincali; Oct 29th, 2008 at 12:17 am.
Reputation Points: 31
Solved Threads: 7
Light Poster
tyincali is offline Offline
45 posts
since Oct 2008
Oct 29th, 2008
0

Re: How do I get the current widget in focus without changing focus?

Click to Expand / Collapse  Quote originally posted by tyincali ...
he was saying if you expect us to look at your code, put that before and after your code in your post. so that we can see syntax highlighting and what not.
Yes, this is for the forum members' sake. This way your code doesn't lose indentation and become unreadable to us. It also saves space, and turns on syntax highlighting.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Oct 29th, 2008
0

Re: How do I get the current widget in focus without changing focus?

My suggestion would be to look at focus_displayof() instead of focus_get()
Reputation Points: 31
Solved Threads: 7
Light Poster
tyincali is offline Offline
45 posts
since Oct 2008
Oct 30th, 2008
0

Re: How do I get the current widget in focus without changing focus?

Click to Expand / Collapse  Quote originally posted by tyincali ...
My suggestion would be to look at focus_displayof() instead of focus_get()
thank u to all

i'm still learning how everything works, and i'm still very much a newbe,..

I tried this:

python Syntax (Toggle Plain Text)
  1. w= app['info']['name'].displayof()
  2. w.insert(INSERT, w.selection_get() )

and got the following error:

python Syntax (Toggle Plain Text)
  1. Exception in Tkinter callback
  2. Traceback (most recent call last):
  3. File "C:\Python25\lib\lib-tk\Tkinter.py", line 1403, in __call__
  4. return self.func(*args)
  5. File "C:\Python25\prg\bs1.py", line 258, in _paste
  6. w= app['info']['name'].displayof()
  7. AttributeError: Entry instance has no attribute 'displayof'

I appreciate all your patience and help,... i really am stuck,... I am self taught and also the only person in my life and family who has any interest in programming.. so hearing from you all was a real ray of hope 4 me
Reputation Points: 10
Solved Threads: 0
Newbie Poster
imabut is offline Offline
4 posts
since Oct 2008
Oct 30th, 2008
0

Re: How do I get the current widget in focus without changing focus?

.focus_displayof()

not

.displayof()
Last edited by tyincali; Oct 30th, 2008 at 4:44 pm.
Reputation Points: 31
Solved Threads: 7
Light Poster
tyincali is offline Offline
45 posts
since Oct 2008
Oct 30th, 2008
0

Re: How do I get the current widget in focus without changing focus?

take the following code as an example:

Python Syntax (Toggle Plain Text)
  1. from Tkinter import *
  2.  
  3. root = Tk()
  4.  
  5. def test():
  6. if (root.focus_displayof()==entry1):
  7. print 'hello'
  8.  
  9. entry1 = Entry(root)
  10. entry2 = Entry(root)
  11. button = Button(text='go', command=test)
  12.  
  13. entry1.pack()
  14. entry2.pack()
  15. button.pack()
  16. root.mainloop()

This will create a form with two entry widgets and a button.

When the button is pressed it executes test(). If entry1 had the focus when I pressed the button it will print 'hello', otherwise... nothing.

also, for your purposes, since it looks like you would like to be able to paste in several different places I would do something like:

python Syntax (Toggle Plain Text)
  1. try:
  2. root.focus_displayof().insert(INSERT, root.focus_displayof().clipboard_get())
  3. except:
  4. pass

that way, whichever widget has focus will recieve the paste. Unless it isn't capable of being pasted to, in which case it fails silently
Last edited by tyincali; Oct 30th, 2008 at 4:54 pm.
Reputation Points: 31
Solved Threads: 7
Light Poster
tyincali is offline Offline
45 posts
since Oct 2008
Oct 30th, 2008
0

Re: How do I get the current widget in focus without changing focus?

Click to Expand / Collapse  Quote originally posted by imabut ...
thank u to all

i'm still learning how everything works, and i'm still very much a newbe,..
If you feel annoyed by Tkinter, try others also!
I found myself in love with wxpython. Some have fallen in hands of pyQt, PyGTK etc. I mean when you start learning try as amny available as you can and then choose among them. Don't underestimate Tkinter, I don't mean it is uselless, No! It is great, but I mean as I said you may fall in hands of one of those

Just MHO
Steve
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: WX.ID - Anywhere to get list?
Next Thread in Python Forum Timeline: A simple Monthly Calendar Display





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC