FontLab Forum
2012-05-23, 23:52:42 *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Welcome to the FontLab forum, read how to use it! Update: Archives from old MSN forums are now available on our forum.
 
   Home   Help Search Calendar Downloads Tags Login Register  
Del.icio.us Digg FURL FaceBook Stumble Upon Reddit SlashDot

Pages: [1]
  Print  
Author Topic: Please help: Focus lost after end of tkinter save-as-dialog  (Read 2049 times)
Arno Enslin
Hero Member
*****

Karma: +8/-0
Germany Germany

Posts: 98



« on: 2010-11-07, 11:15:58 »

I have written a script with a save-as-dialog included. But after I saved the file or canceled, the focus is not anymore on the FontLab window. I am illustrating this by three pictures. But first the code:

Code:
#FLM: with unencoded glyphs at the bottom

def buildNamFile():
import Tkinter,tkFileDialog
root = Tkinter.Tk()
root.withdraw()
namFileName = tkFileDialog.asksaveasfilename(parent=root, initialfile=fontName+'.nam', initialdir="/", title="Save namFile as ...")
if not namFileName == '':
namFile = open(namFileName, 'w')
print >> namFile, '%%FONTLAB NAMETABLE: '+fontName
for g in fl.font.glyphs:
a = g.unicode
if a:
a = hex(a)
while len(a) <= 5:
a = '0x0'+a[2:]
print >> namFile, a, g.name
counter=0
for g in fl.font.glyphs:
a = g.unicode
if not a:
counter += 1
a = '%0x????'
if counter == 1:
print >> namFile, '%--------------------------------------------------unencoded'
print >> namFile, a, g.name
namFile.close()

if fl.font != None:
if fl.font.glyphs:
if fl.font.font_name == None:
fontName = 'Untitled'
else:
fontName = fl.font.font_name
buildNamFile()
else:
print 'Error: Font does not contain glyphs.'
else:
print 'Error: No font opened.'

Before start of macro


Macro started


File saved / Dialog canceled / End of Macro
Logged
Eigi
Beta: FontLab Studio Mac
Hero Member
***

Karma: +7/-0
Germany Germany

Posts: 57



WWW
« Reply #1 on: 2010-11-07, 11:56:13 »

Hello,

Why don't you use the FontLab-Python-Buildin-File-Dialog?

GetFileName(open_save, string extension, string filename, string filters)
opens the file dialog. open_save = 1 for file open dialog, 0 for save as.

Eigi
Logged
Eigi
Beta: FontLab Studio Mac
Hero Member
***

Karma: +7/-0
Germany Germany

Posts: 57



WWW
« Reply #2 on: 2010-11-07, 12:06:11 »

... like this:
Code:
modeSave = 0
defaultExt = 'xml'
defaultName = 'myFile.xml'
strFilter = 'XML-File      (*.xml)|*.xml|'
strFilter += 'HTML-File     (*.html)|*.html|'
strFilter += 'All files     (*.*)|*.*|'
selectedPath = fl.GetFileName(modeSave, defaultExt, defaultName, strFilter)
print selectedPath
Logged
Arno Enslin
Hero Member
*****

Karma: +8/-0
Germany Germany

Posts: 98



« Reply #3 on: 2010-11-07, 12:25:52 »

Hello,

Why don't you use the FontLab-Python-Buildin-File-Dialog?

GetFileName(open_save, string extension, string filename, string filters)
opens the file dialog. open_save = 1 for file open dialog, 0 for save as.

Eigi

Because I am an idiot, Eigi. A DAU. No, in earnest: I started with Python scripting a few days ago and I miss orientation. I want to buy a book about Python. But in German.

The Buildin-File-Dialog – that sounds promising.

But first I want to document an unelegant solution, that I have found by try and error experiments a few minutes ago:

Code:
#FLM: with unencoded glyphs at the bottom

def buildNamFile():
import Tkinter,tkFileDialog
root = Tkinter.Tk()
root.withdraw()
namFileName = tkFileDialog.asksaveasfilename(parent=root, initialfile=fontName+'.nam', initialdir="/", title="Save namFile as ...")
if not namFileName == '':
namFile = open(namFileName, 'w')
print >> namFile, '%%FONTLAB NAMETABLE: '+fontName
for g in fl.font.glyphs:
a = g.unicode
if a:
a = hex(a)
while len(a) <= 5:
a = '0x0'+a[2:]
print >> namFile, a, g.name
counter=0
for g in fl.font.glyphs:
a = g.unicode
if not a:
counter += 1
a = '%0x????'
if counter == 1:
print >> namFile, '%--------------------------------------------------unencoded'
print >> namFile, a, g.name
namFile.close()
root.iconify()
root.withdraw()

if fl.font != None:
if fl.font.glyphs:
if fl.font.font_name == None:
fontName = 'Untitled'
else:
fontName = fl.font.font_name
buildNamFile()
else:
print 'Error: Font does not contain glyphs.'
else:
print 'Error: No font opened.'

I just have added the following code:

Code:
root.iconify()
root.withdraw()

The following code has the same effect:

Code:
root.deiconify()
root.withdraw()

But I don’t know, why it works, because I assume, that the tkinter window is stll active with the added code. And even, if it works with your proposal, which seems to offer an elegant solution, I would like to make out a more elegant solution with regard to the tkinter dialog additionally.

I will try to make use of the FontLab-Python-Buildin-File-Dialog. Except from that my code is not yet clean.

Thank you, Eigi! I already was afraid, that I don’t get response here, because the activity here is relatively low in comparison to a German Python forum, where I have registered today.
Logged
Arno Enslin
Hero Member
*****

Karma: +8/-0
Germany Germany

Posts: 98



« Reply #4 on: 2010-11-07, 12:49:17 »

The built in dialog works very fine, Eigi.

Code:
#FLM: with unencoded glyphs at the bottom

def buildNamFile():
modeSave = 0
defaultExt = 'nam'
defaultName = fontName
strFilter = 'nam-File (*.nam)|*.nam|'
strFilter += 'All files (*.*)|*.*|'
namFileName = fl.GetFileName(modeSave, defaultExt, defaultName, strFilter)
if not namFileName == '':
namFile = open(namFileName, 'w')
print >> namFile, '%%FONTLAB NAMETABLE: '+fontName
for g in fl.font.glyphs:
a = g.unicode
if a:
a = hex(a)
while len(a) <= 5:
a = '0x0'+a[2:]
print >> namFile, a, g.name
counter=0
for g in fl.font.glyphs:
a = g.unicode
if not a:
counter += 1
a = '%0x????'
if counter == 1:
print >> namFile, '%--------------------------------------------------unencoded'
print >> namFile, a, g.name
namFile.close()


if fl.font != None:
if fl.font.glyphs:
if fl.font.font_name == None:
fontName = 'Untitled'
else:
fontName = fl.font.font_name
buildNamFile()
else:
print 'Error: Font does not contain glyphs.'
else:
print 'Error: No font opened.'

Now I only have to clean the code. And it even is Mac compatible now. I assume it wasn’t before. Cool!
« Last Edit: 2010-11-07, 13:22:02 by Arno Enslin » Logged
Arno Enslin
Hero Member
*****

Karma: +8/-0
Germany Germany

Posts: 98



« Reply #5 on: 2010-11-07, 12:58:38 »

Where is "GetFileName" documented, Eigi?

Is it in the Unofficial FontLab/Python API Reference (http://www.e-font.de/flpydoc/view_html.html)? I searched for something like that there, but I may have overseen it. But even now I cannot find it.
« Last Edit: 2010-11-07, 13:12:21 by Arno Enslin » Logged
Eigi
Beta: FontLab Studio Mac
Hero Member
***

Karma: +7/-0
Germany Germany

Posts: 57



WWW
« Reply #6 on: 2010-11-07, 13:45:48 »

this was my resource:
Code:
print fl.__doc__
Logged
Arno Enslin
Hero Member
*****

Karma: +8/-0
Germany Germany

Posts: 98



« Reply #7 on: 2010-11-07, 13:49:18 »

I have searched for getfilename now on the directory of FontLab. And I found it in the executable file. I only have to open it in UltraEdit and leave the hex mode.

I have read about "print fl.__doc__" and tried it. More than one time. I simply have not seem the getfilename method. Thanks again. Karma enlarged!
« Last Edit: 2010-11-07, 13:57:22 by Arno Enslin » Logged
Tags:
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!