473,395 Members | 1,688 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,395 software developers and data experts.

Tkinter: focus/text selection problem with tkFileDialog

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 things up. After the file dialog, I can no longer use
the Text widget (typing, selecting, it doesn't work anymore!)
What am I doing wrong?

TIA,
--Irmen de Jong
-----test.py----
import Tkinter, tkFileDialog

window=Tkinter.Tk()
txt=Tkinter.Text(window)
txt.insert(Tkinter.END,"Choose a file, then try to select me")
txt.pack()

name=tkFileDialog.askopenfilename()
txt.insert(Tkinter.END,"\nFile chosen: "+name)
window.mainloop()
Jul 18 '05 #1
2 4121
Irmen de Jong wrote:
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 things up. After the file
dialog, I can no longer use the Text widget (typing, selecting, it doesn't
work anymore!) What am I doing wrong?


No sure, but opening the dialog before entering the main loop could be the
problem. The following modification seems to work:

import Tkinter, tkFileDialog

window = Tkinter.Tk()
txt = Tkinter.Text(window)
txt.insert(Tkinter.END, "Choose a file, then try to select me")
txt.pack()

def openFile(event=None):
name = tkFileDialog.askopenfilename()
txt.insert(Tkinter.END, "\nFile chosen: " + name)

txt.focus_set()
window.after_idle(openFile)
window.mainloop()

Peter

Jul 18 '05 #2
Peter Otten wrote:
Irmen de Jong wrote:

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 things up. After the file
dialog, I can no longer use the Text widget (typing, selecting, it doesn't
work anymore!) What am I doing wrong?

No sure, but opening the dialog before entering the main loop could be the
problem. The following modification seems to work:

import Tkinter, tkFileDialog

window = Tkinter.Tk()
txt = Tkinter.Text(window)
txt.insert(Tkinter.END, "Choose a file, then try to select me")
txt.pack()

def openFile(event=None):
name = tkFileDialog.askopenfilename()
txt.insert(Tkinter.END, "\nFile chosen: " + name)

txt.focus_set()
window.after_idle(openFile)
window.mainloop()

Peter


Okay, I didn't know about the after_idle, and still don't understand why
my first attempt resulted in the weird behavior I described. But hey,
with a bit of tweaking, it works now :-)

Also another weird thing: I found out (using my original code
without the after_idle) that if I inserted a tkMessageBox before the
mainloop(), that the 'correct' behavior of the Text box is restored!
....confused...

--Irmen.

P.S. I have now also discovered Fredrik Lundh's page:
http://www.effbot.org/zone/tkinter-threads.htm Interesting.
Jul 18 '05 #3

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

Similar topics

3
by: Phil Schmidt | last post by:
I'm trying to make a custom entry widget, as in the code that follows. There are two problems I'm trying to fix: 1) I would like the widget to behave as myEntry.Escape() does now, except that it...
5
by: max(01)* | last post by:
hello. i wrote a very simple tkinter demo program that uses menus, buttons, labels, entries, frames and secondary toplevels. it is a python version of a java program made by a colleague. ...
5
by: annagel | last post by:
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...
1
by: Michael Yanowitz | last post by:
Hello: Below I have included a stripped down version of the GUI I am working on. It contains 2 dialog boxes - one main and one settings. It has the following problems, probably all related, that...
3
by: Ant | last post by:
Hi all, I have been trying to select text in a Text widget programmatically. I have been trying the following minimal example: #================================= from Tkinter import * def...
5
by: vagrantbrad | last post by:
I've created a short test program that uses tkFileDialog.askdirectory to help the user input a path into a tk entry widget. The problem I'm having is that when I run the code as listed below, the...
1
by: alivip | last post by:
I integrat program to be GUI using Tkinter I try browser direction as you can see # a look at the Tkinter Text widget # use ctrl+c to copy, ctrl+x to cut selected text, # ctrl+v to...
2
by: alivip | last post by:
when I wont to inser (anyting I print) to the textbox it will not inser it just print then hanging # a look at the Tkinter Text widget # use ctrl+c to copy, ctrl+x to cut selected text, #...
0
by: Leonhard Vogt | last post by:
Hello I have the following problem in Python 2.5 on Windows XP. On Ubuntu I do not see the problem. I have a Tkinter application as in the following example The entry-widget is somehow...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...

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.