Force Focus in Tkinter

annagel andrew.nagel at gmail.com
Wed Dec 7 10:36:05 EST 2005


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




More information about the Python-list mailing list