| | |
How do I get the current widget in focus without changing focus?
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 4
Reputation:
Solved Threads: 0
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.
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.
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.
•
•
Join Date: Oct 2008
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
My suggestion would be to look at focus_displayof() instead of focus_get()
i'm still learning how everything works, and i'm still very much a newbe,..
I tried this:
python Syntax (Toggle Plain Text)
w= app['info']['name'].displayof() w.insert(INSERT, w.selection_get() )
and got the following error:
python Syntax (Toggle Plain Text)
Exception in Tkinter callback Traceback (most recent call last): File "C:\Python25\lib\lib-tk\Tkinter.py", line 1403, in __call__ return self.func(*args) File "C:\Python25\prg\bs1.py", line 258, in _paste w= app['info']['name'].displayof() 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
•
•
Join Date: Oct 2008
Posts: 45
Reputation:
Solved Threads: 6
take the following code as an example:
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:
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
Python Syntax (Toggle Plain Text)
from Tkinter import * root = Tk() def test(): if (root.focus_displayof()==entry1): print 'hello' entry1 = Entry(root) entry2 = Entry(root) button = Button(text='go', command=test) entry1.pack() entry2.pack() button.pack() 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)
try: root.focus_displayof().insert(INSERT, root.focus_displayof().clipboard_get()) except: 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.
I wish I had something cool to put here...
•
•
•
•
thank u to all
i'm still learning how everything works, and i'm still very much a newbe,..
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
![]() |
Other Threads in the Python Forum
- Previous Thread: WX.ID - Anywhere to get list?
- Next Thread: A simple Monthly Calendar Display
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt ansi anti approximation assignment avogadro backend basic beginner binary bluetooth calculator character code customdialog decimals dictionaries dictionary drive dynamic examples excel exe file float format ftp function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime program programming progressbar projects py2exe pygame pyqt python random recursion recursive refresh schedule scrolledtext sqlite ssh statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode update urllib urllib2 variable wikipedia windows write wxpython xlib






