Tkinter Label alignment problem using OS 10.6

AJ ajuffali at yahoo.com
Fri Jul 30 19:52:51 EDT 2010


Dear users,

I have written a sample program that ran correctly with earlier than
Mac OS 10.6. The answer field Label does not align correctly. I
downloaded the latest Python 2.7 release but still did not solve the
problem.  Look for line "L4.place(relx=0.32,rely=0.56, anchor=W)"

#!/usr/bin/env python

from Tkinter import *
from tkMessageBox import *
import sys


win = Tk()
#win.tk.call('console', 'hide')
try:
	win.tk.call('after','idle','console','hide')
except TclError:
	pass
ScreenX = win.winfo_screenwidth()
ScreenY = win.winfo_screenheight()
ScreenX = (ScreenX/2) - 250
ScreenY = (ScreenY/2) - 200
win.geometry('500x300%+d%+d' %(ScreenX, ScreenY))
win.title("'Place' Geometry Test")
win.resizable(width=False, height=False)

FirstN  = StringVar()
SecondN = StringVar()
Answer  = StringVar()

Verd12 = ("Verdana","11")
Verd16 = ("Verdana","16")
Verd25 = ("Verdana","25")
Sys16  = ("System" ,"16")


#----------------- AboutDialog ----------------------------

class AboutDialog(Toplevel):

    def __init__(self, parent):
        Toplevel.__init__(self, parent)
        self.configure(bg = 'white', borderwidth=3)
        self.geometry('200x300%+d%+d' %(ScreenX, ScreenY))
        self.title('About...')
        self.resizable(height=FALSE, width=FALSE)
        self.transient(parent)
        self.grab_set()
        self.protocol("WM_DELETE_WINDOW", self.OkToExit)
        self.parent = parent
        self.FillDialog()
        self.Btn.focus_set()
        self.bind('<Return>',self.OkToExit)
        self.bind('<Escape>',self.OkToExit)
        self.bind('<Command-KeyPress-Q>', Done)
        self.bind('<Command-KeyPress-q>', Done)

        self.wait_window()

    def FillDialog(self):

        self.AboutText = "\n\n\nPlace Geometry Test\n\nBy\n\nAmin
Aljuffali"
        self.Lbl = Label(self, text=self.AboutText, font=Verd16, bg =
'white')
        self.Lbl.pack(side=TOP)
        self.Lb2 = Label(self, text="April 1, 2007", height = 1,
font=Verd12, bg = 'white')
        self.Lb2.pack(side=TOP)
        self.Lb3 = Label(self, text=" ", height = 3, font=Verd12, bg =
'white')
        self.Lb3.pack(side=TOP)
        self.Btn = Button(self, text='Done', font=Sys16, width=8,
height=1, command=self.OkToExit)
        self.Btn.pack(side=TOP)
        self.Lb4 = Label(self, text=" ", height = 3, font=Verd12, bg =
'white')
        self.Lb4.pack(side=BOTTOM)
        self.update()

    def OkToExit(self, event=None):
        self.destroy()

#----------------------------------------------------------

def ShowDialog():
    AboutDialog(win)

def done():
    win.destroy()
    win.quit

def Done(e):
    win.destroy()
    win.quit


def CheckAlfanumeric(x):

    if x == '':
      return False
    for ch in x:
      if ch not
in['.','-','+','0','1','2','3','4','5','6','7','8','9','e','E']:
        return False
    return True


def Multiply():

    #global FirstN, SecondN, Answer

    try:
      a = FirstN.get().strip()
      if not CheckAlfanumeric(a):
        raise ValueError

      b = SecondN.get().strip()
      if not CheckAlfanumeric(b):
        raise ValueError

      FirstN.set(a)
      SecondN.set(b)
      Answer.set(str(float(a) * float(b)))

    except ValueError:
      showwarning("Warning...","Input Error!")

    return

def MakeToplevelMenu(topwin):

    top = Menu(topwin)
    topwin.config(menu=top)

    if sys.platform == 'darwin' and '.app' in sys.executable:
       application = Menu(top, name='apple')
       application.add_command(label='About...', command=ShowDialog,
underline=0)
       top.add_cascade(label='PlaceTest', menu=application,
underline=0)

    fileMenu = Menu(top, tearoff=0)
    fileMenu.add_command(label='Exit', command=done, underline=0)
    top.add_cascade(label='File', menu=fileMenu, underline=0)

    helpMenu = Menu(top, tearoff=0)
    helpMenu.add_command(label='About...', command=ShowDialog,
underline=0)
    top.add_cascade(label='Help', menu=helpMenu, underline=0)

    return


MakeToplevelMenu(win)

L1 = Label(win, text='Multiply Two Numbers', font=Verd25, bg =
'white')
L2 = Label(win, text='First Number:', font=Verd16, bg = 'white')
L3 = Label(win, text='Second Number:', font=Verd16, bg = 'white')
L4 = Label(win,  text='', textvariable=Answer, font=Sys16, bg =
'white', bd=1, width=20, height=1, anchor=W, relief=SOLID)
L5 = Label(win,  text='Written by Amin Aljuffali', font=Verd12,padx=2,
bg = 'white', anchor=W, relief=FLAT)
B1 = Button(win, text='Multiply', font=Sys16, width=19, height=1,
command=Multiply)
B2 = Button(win, text='Quit', font=Sys16, width=19, height=1,
command=done)
F1 = Frame(win, relief=FLAT, bd=0, bg='#336699')
F2 = Frame(win, relief=FLAT, bd=0, bg='#336699')
F3 = Frame(win, relief=FLAT, bd=0, bg='#336699')

E1 = Entry(win, textvariable=FirstN, relief=SOLID, font=Sys16, bg =
'white',bd=1)
E2 = Entry(win, textvariable=SecondN, relief=SOLID, font=Sys16, bg =
'white', bd=1)

win.bind('<Command-KeyPress-q>', Done)
win.bind('<Command-KeyPress-Q>', Done)

F1.place(relx=0.0,rely=0.01, anchor=NW, width=600, height=5)
L1.place(relx=0.5,rely=0.09, anchor=CENTER)
F2.place(relx=0.0,rely=0.16, anchor=NW, width=600, height=5)
E1.place(relx=0.32,rely=0.32, anchor=W)
E2.place(relx=0.32,rely=0.44, anchor=W)
L2.place(relx=0.3,rely=0.32, anchor=E)
L3.place(relx=0.3,rely=0.44, anchor=E)
L4.place(relx=0.32,rely=0.56, anchor=W)
B1.place(relx=0.525,rely=0.69, anchor=CENTER)
B2.place(relx=0.525,rely=0.81, anchor=CENTER)
F3.place(relx=0.0,rely=0.9, anchor=W, width=600, height=5)
L5.place(relx=0.01,rely=0.95, anchor=W)


E1.focus_set()
win.update()
win.mainloop()
sys.exit(0)



More information about the Python-list mailing list