473,503 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Force Focus in Tkinter

I am looking for a way to force a Tkinter window into focus on a system
level. I know the force focus method should bring one window of my
application into focus, but it seems I need to have some part of my
application already in focus on a system level to make this work. If
for example I am entering text at the command line and call a
force_focus on my root window the call will not return till I click on
a window in my application. The same goes for any modification to the
main window I have tried adding new windows (toplevel), setting focus,
generating events, none of these calls returns until I move focus over
to my parrent window. Is there any way around this? Basically any way
to create a kind of panic dialog that jumps up over what the user is
doing at the command line.

Dec 6 '05 #1
5 7771
annagel wrote:
I am looking for a way to force a Tkinter window into focus on a system
level. I know the force focus method should bring one window of my
application into focus, but it seems I need to have some part of my
application already in focus on a system level to make this work. If
for example I am entering text at the command line and call a
force_focus on my root window the call will not return till I click on
a window in my application. The same goes for any modification to the
main window I have tried adding new windows (toplevel), setting focus,
generating events, none of these calls returns until I move focus over
to my parrent window. Is there any way around this? Basically any way
to create a kind of panic dialog that jumps up over what the user is
doing at the command line.

Before discussion *how* to do this, are you *really* sure you want to? I
have always found it a freadful failing of the Windows interface that it
can suddenly snatch the focus away from what I am doing and force my
keystrokes and/or mouseclicks into another application.

It's worst when a dialog box appears just before I hit the ENTER key -
the ENTER acts as a button press on the dialog's default button and I
have no idea whether I just cancelled some actions or agreed to it.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Dec 6 '05 #2
Thanks for the reply and as it turns out no I don't think I really do
want to do this. I started working with tkinter after banging my head
against the wall for a while with wxwindows so the whole thing ended up
being a last minute change so I am trying to get most at least of the
bugs out on a tighter scedule than I planned. Basically coming from wx
I had it in my head that I had to call a main loop or nothing would
work and this meant threads which meant all kinds of extra headaches.
In my desperation I dropped the whole thing and just started opening
windows and to my utter suprise it worked.

Now I just initialize a root window in my import statement withdraw it
and let the user use a picture viewer I made with a toplevel window, a
file chooser, folder chooser, and color chooser. I am still having a
couple problems though if anyone out there can shed some light I would
be apreciative. First the ColorChooser for some reason this dialog
does not open on its own. I am running on Mac and calling it gets my
python icon bouncing but it does not appear till I go click on the
icon. Second I am getting phantom window return with the folder and
file picker. If I have used one of these before showing the picture
viewer and then pop that up when I click on the picture window I also
get the most recent file dialog (filenameopen or directoryopen) as an
unresponsive window.

Any advice on either of those problems would be great. Sorry about the
intial post in my head I knew doing something like that would just be
evil, but you start to think crazy when your sollution is too
complicated for the problem.

Andrew

Dec 7 '05 #3
annagel wrote:
Thanks for the reply and as it turns out no I don't think I really do
want to do this. I started working with tkinter after banging my head
against the wall for a while with wxwindows so the whole thing ended up
being a last minute change so I am trying to get most at least of the
bugs out on a tighter scedule than I planned. Basically coming from wx
I had it in my head that I had to call a main loop or nothing would
work and this meant threads which meant all kinds of extra headaches.
In my desperation I dropped the whole thing and just started opening
windows and to my utter suprise it worked.

Now I just initialize a root window in my import statement withdraw it
and let the user use a picture viewer I made with a toplevel window, a
file chooser, folder chooser, and color chooser. I am still having a
couple problems though if anyone out there can shed some light I would
be apreciative. First the ColorChooser for some reason this dialog
does not open on its own. I am running on Mac and calling it gets my
python icon bouncing but it does not appear till I go click on the
icon. Second I am getting phantom window return with the folder and
file picker. If I have used one of these before showing the picture
viewer and then pop that up when I click on the picture window I also
get the most recent file dialog (filenameopen or directoryopen) as an
unresponsive window.

Any advice on either of those problems would be great. Sorry about the
intial post in my head I knew doing something like that would just be
evil, but you start to think crazy when your sollution is too
complicated for the problem.

Andrew


Andrew,

Glad to hear you got it working, could you post some example code that
shows these other problems?
Martin

Dec 7 '05 #4
annagel wrote:
Thanks for the reply and as it turns out no I don't think I really do
want to do this. I started working with tkinter after banging my head
against the wall for a while with wxwindows so the whole thing ended up
being a last minute change so I am trying to get most at least of the
bugs out on a tighter scedule than I planned. Basically coming from wx
I had it in my head that I had to call a main loop or nothing would
work and this meant threads which meant all kinds of extra headaches.
In my desperation I dropped the whole thing and just started opening
windows and to my utter suprise it worked.
It may well be that you will need to use threads anyway, I'm afraid -
ultimately if you want computation to keep on happening while the GUI
remains responsive then it's almost a requirement. However, it's easier
to make a start when you are actually seeing windows appear on the
screen, so I'm glad you are making progress.
Now I just initialize a root window in my import statement withdraw it
and let the user use a picture viewer I made with a toplevel window, a
file chooser, folder chooser, and color chooser. I am still having a
couple problems though if anyone out there can shed some light I would
be apreciative. First the ColorChooser for some reason this dialog
does not open on its own. I am running on Mac and calling it gets my
python icon bouncing but it does not appear till I go click on the
icon. Second I am getting phantom window return with the folder and
file picker. If I have used one of these before showing the picture
viewer and then pop that up when I click on the picture window I also
get the most recent file dialog (filenameopen or directoryopen) as an
unresponsive window.
It's probable that you are failing to destroy some widget after use, but
that's about as much as I can say at present.
Any advice on either of those problems would be great. Sorry about the
intial post in my head I knew doing something like that would just be
evil, but you start to think crazy when your sollution is too
complicated for the problem.

As Martin has indicated, more detail in the form of code will be
necessary to investigate specific problems like that.

In practice the choice between Tkinter and wxPython, PyGtk and the like
is often either a religious issue or a choice based on the first tool
that could be manipulated to providing the required results. Ideally you
want to structure your program so that the GUI can be changed without
too much effect on the rest of the logic, though that's often easier
said than done.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Dec 7 '05 #5
OK some sample Code. First from the module that I am importing. I
won't post anything because the vast majority of stuff is way outside
the scope of this problem and has to do with image and sound
manipulation.

First the imports I use:

import sys
import os
import user
import traceback
import time
import Numeric
import struct
from Image import *
from threading import *
from math import sqrt
import cStringIO
from Tkinter import *
from Queue import *
import thread
import ImageTk
import tkFileDialog
import tkColorChooser
import pygame

#Now some initialization
ver = "1.1"
pygame.mixer.pre_init(22050, -16, False)
pygame.init()
defaultFont = pygame.font.SysFont("times", 24)

top = Tk()

mediaFolder = user.home + os.sep
#Some global functions that use the file and color dialogs

def pickAFile(**options):
path = tkFileDialog.askopenfilename()
return path

def pickAFolder(**options):
global mediaFolder
folder = tkFileDialog.askdirectory()
if folder == '':
folder = mediaFolder
return folder

def pickAColor(**options):
color = tkColorChooser.askcolor()
newColor = Color(color[0][0], color[0][1], color[0][2])
return newColor

# the color object is just a container for the r, g, b values that can
also do things like
# lighten and darken it

#Finally my picture class (abreviated)

class Picture:
def __init__(self):
self.title = "Unnamed"
self.dispImage = None

def createImage(self, width, height):
self.surf = pygame.Surface((width, height))
self.pixels = pygame.surfarray.pixels3d(self.surf)
self.filename = ''
self.title = 'None'

def getImage(self):
data = pygame.image.tostring(self.surf, "RGB", 0)
image = fromstring("RGB", (self.getWidth(), self.getHeight()), data)
return image

def loadImage(self,filename):
global mediaFolder
if not os.path.isabs(filename):
filename = mediaFolder + filename
self.surf = pygame.image.load(filename)
self.pixels = pygame.surfarray.pixels3d(self.surf)
self.filename = filename
self.title = getShortPath(filename)

def repaint(self):
self.dispImage = ImageTk.PhotoImage(self.getImage())
self.item = self.canvas.create_image(0, 0, image=self.dispImage,
anchor='nw')

def show(self):
self.frame = Toplevel()
self.canvas = Canvas(self.frame, width=self.getWidth(),
height=self.getHeight())
self.dispImage = ImageTk.PhotoImage(self.getImage())
self.item = self.canvas.create_image(0, 0, image=self.dispImage,
anchor='nw')
self.canvas.pack()

#For this the Pixel class is a reference directly into the image data
so a user can modify
#the image in a pixel by pixel maner

#Convenience global function

def makePicture(filename):
picture = Picture()
picture.loadImage(filename)
try:
w = picture.getWidth()
return picture
except:
print "Was unable to load the image in " + filename +"\nMake sure
it's a valid image file."

Now the code I run and the results
from media import *
pic = makePicture(pickAFile())
pic.show()
When I do this I get my image window to pop-up and it works fine is
responsive to everything I do, but when I click on it I get the old
filePicker with my selection still made in it. I can't do anything at
all with this window though I still have complete control over the
picture window but nothig over this new window.

Finally the colorPicker
col = pickAColor()


this starts my python icon jumping around but the picker does not
appear until after I click the jumping window and then click back to my
command prompt (I am using osx 10.4)

If anyone wants more information I would be happy to send it along. I
just did not want to post all of the code because the whole file is 900
lines long and I really do not think that most of it should matter at
all to what is going on with the windows since none of the sound stuff
used windows at all and Color and Pixel use it in that they interact
with Picture but not for any other reason.

Thanks again for any help

Andrew

Dec 7 '05 #6

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

Similar topics

0
1863
by: Kim Petersen | last post by:
Is it possible in Tkinter to simulate a keystroke into the current active (focused widget) by pressing another button (this is for a virtual keyboard on a touchscreen pc). the troubles i see is:...
0
1346
by: Todd Miller | last post by:
I'm working on a Tkinter backend for the matplotlib plotting software. matplotlib can be run interactively from some form of Python shell while plotting to a Tk window. One annoyance we've noticed...
2
4129
by: Irmen de Jong | last post by:
Hi, I'm having trouble with the code below. It's just a regular Tk text widget in which you can type and select text as expected, however the call to tkFileDialog.askopenfilename() seems to screw...
2
8241
by: Charles Eubanks | last post by:
The enclosed html snippet displays an area map. The behavior I want is for each area to show up (eg show the outline of the poly) when the user mouses over it. This happens nicely BUT in order...
2
2041
by: S P Arif Sahari Wibowo | last post by:
Hi! Do you know how to put a form's Access-Visual-Basic-code that will force the form to be inserted, while the user has not type anything in the form, without changing focus, selection, etc.? ...
1
5088
by: vigacmoe | last post by:
Hi all, I'm trying to write a simple tkinter program, then this problem popped up. The followin code will describe the problem. ------------------------------------------ import Tkinter ...
1
6696
by: rahulnag22 | last post by:
Hi, I have a Tk() window "base_win = Tk()" with multiple frames on it having a combination of widgets. If I click on say a button widget which launches a new top level window "new_win =...
10
7338
by: blaine | last post by:
Hey everyone! I'm not very good with Tk, and I am using a very simple canvas to draw some pictures (this relates to that nokia screen emulator I had a post about a few days ago). Anyway, all is...
0
1039
by: William McBrine | last post by:
So, basically what I'm doing is this: window1 = Tkinter.Tk() .... window1.destroy() .... window2 = Tkinter.Tk() This works well in Linux and Mac OS X (and the PyGtk equivalent works on all...
0
7202
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
7086
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
7332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7462
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
5578
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
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
382
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.