[Tutor] Running Python GUI on Windows and Linux...problems encountered in Windows.

Ajaya Babu ajaya@ncoretech.com
Fri, 7 Sep 2001 12:48:29 +0530


This is a multi-part message in MIME format.

------=_NextPart_000_0003_01C1379B.65F750D0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit


Hi,

I've a problem specific to windows. I've developed a simple GUI using
python. My amin is to test with both linux and windows. I've two threads for
it. One for GUI and another for processing. But when I tested with windows
it started hanging...can any one give comments on implementation.

I am jsut attaching my file here with:


from Tkinter import *
import string
import Pmw
from time import sleep
import thread
import sys

class Key(Button):
    def __init__(self, master, font=('arial', 8, 'bold'),
                 fg='white', width=3, bd=3, **kw):
        kw['font'] = font
        kw['fg'] = fg
        kw['width'] = width
        kw['bd'] = bd
        apply(Button.__init__, (self, master), kw)
        self.pack(side=LEFT, expand=YES, fill=BOTH)


class MenuBar(Frame):
    def __init__(self, master, Menus, relief=RAISED, **kw):
        kw['relief'] = relief
        apply(Frame.__init__, (self, master), kw)
        self.pack(side=TOP, expand=YES, fill=BOTH)

        for x in Menus:
            cmdbutn, cmdlist = x
            CommandButton(self, cmdlist, text=cmdbutn)


class CommandButton(Menubutton):
    def __init__(self, MenuBar, commandlist, **kw):
        apply(Menubutton.__init__, (self, MenuBar), kw)
        self.pack(side=LEFT, padx="2m")
        self.menu = Menu(self)
        for x in commandlist:
            L, C, S = x
            self.menu.add_command(label=L, command=C, state=S)

        self['menu'] = self.menu


class PhoneLabel(Frame):
    def __init__(self, master, text):
        Frame.__init__(self, master, bg='gray40')
        self.pack(side=LEFT, expand=YES, fill=BOTH)
        Label(self, text=text, fg='steelblue1', font=("arail", 10, "bold"),
              width=5, bg='gray40').pack(side=LEFT, expand=YES, fill=BOTH)


class VirtualHandSet(Frame):
    def __init__(self, parent=None):
        Frame.__init__(self, bg='gray40')
        self.pack(expand=YES, fill=BOTH)
        self.master.title('Virtual Hand Set')
        self.master.iconname('VHS')
        self.EventQ = []
        self.current = ''
        self.BuildVirtualHandSet()


    def EvalAction(self, action):
        self.EventQ.append(action)




    def EvalKey(self, key):
        self.display.insert(END, key)
        self.current = self.current + key



    def GetEventQ(self):
        return self.EventQ

    def GetDigitMap(self):
        return self.current


    def ConfigMenu(self):
        print 'This is not implemented yet'

    def __del__(self):
        sys.exit(0)


    def BuildVirtualHandSet(self):
        FUN = 1
        KEY = 0
        KC1 = 'gray30'      #Dark Keys
        KC2 = 'gray50'      #Light Keys
        KC3 = 'steelblue1'  #Light Blue Keys
        KC4 = 'steelblue'   #Dark Blue Keys
        OnOffBD = 10
        FunBD = 3
        KeyBD = 3
        Keys = [
                [('ON/OFF', '', FUN, 'OnOff', OnOffBD),
                ],


                  ('Flash', '', FUN, 'Flash', FunBD),
                  ('Mute', '', FUN, 'Mute', FunBD),
                  ('R', '', FUN, 'Redial', FunBD)
                ],
                [ ('1', 'abc', KEY, '1', KeyBD),
                  ('2', 'def', KEY, '2', KeyBD),
                  ('3', 'ghi', KEY, '3', KeyBD),

                ],
                [ ('4', 'jkl', KEY, '4', KeyBD),
                  ('5', 'mno', KEY, '5', KeyBD),
                  ('6', 'pqr', KEY, '6', KeyBD)
                ],
                [
                  ('7', 'stu', KEY, '7', KeyBD),
                  ('8', 'vwx', KEY, '8', KeyBD),
                  ('9', 'yz', KEY, '9', KeyBD)
                ],
                [
                  ('*', '', KEY, '*', KeyBD),
                  ('0', '', KEY, '0', KeyBD),
                  ('#', '', KEY, '#', KeyBD)

                ]
               ]

        MenuItems = [ ('Config', [('IP Parameters', self.ConfigMenu,
DISABLED),
                                  ('Layout', self.ConfigMenu, DISABLED)
                                 ]

                      )


                    ]


        MenuBar(self, MenuItems,relief=RAISED, bd=1)

        self.display = Pmw.ScrolledText(self, hscrollmode='dynamic',
                                  vscrollmode='dynamic',
                                  hull_relief='sunken',
                                  hull_background='gray40',
                                  hull_borderwidth=3,
                                  text_background='honeydew4',
                                  text_width=20,
                                  text_foreground='black',
                                  text_height=3,
                                  text_padx=10, text_pady=10,
                                  text_relief='groove',
                                  text_font=('arial', 12, 'bold'))
        self.display.pack(side=TOP, expand=YES, fill=BOTH)

        for row in Keys:
            rowa = Frame(self, bg='gray40')
            rowb = Frame(self, bg='gray40')
            for p1, p2, ktype, func, bd in row:
                PhoneLabel(rowa, text=p2)
                if ktype == FUN:
                    a = lambda s=self, a=func: s.EvalAction(a)
                    Key(rowb, text=p1, bg=KC4, bd=bd, command=a)
                else:
                    a = lambda s=self, k=func: s.EvalKey(k)
                    Key(rowb, text=p1, bg=KC1, bd=bd, command=a)

            rowa.pack(side=TOP, expand=YES, fill=BOTH)
            rowb.pack(side=TOP, expand=YES, fill=BOTH)









class VirtualHandSetProcessor:
    def __init__(self):
        self.handset = VirtualHandSet()
#        self.Pipe = TcpIpPipe()
        thread.start_new_thread(self.WorkerThread, ())

    def WorkerThread(self):
        while(1):
            print 'Hello'
            print self.handset.GetEventQ()
            sleep(1)



if __name__ == '__main__':
    print "Hello"
    a = VirtualHandSetProcessor()
    print "Hello"

------=_NextPart_000_0003_01C1379B.65F750D0
Content-Type: text/plain;
	name="utils.py"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="utils.py"

from Tkinter import *
import string
import Pmw
from time import sleep
import thread
import sys

class Key(Button):
    def __init__(self, master, font=3D('arial', 8, 'bold'),
                 fg=3D'white', width=3D3, bd=3D3, **kw):
        kw['font'] =3D font
        kw['fg'] =3D fg
        kw['width'] =3D width
        kw['bd'] =3D bd
        apply(Button.__init__, (self, master), kw)
        self.pack(side=3DLEFT, expand=3DYES, fill=3DBOTH)


class MenuBar(Frame):
    def __init__(self, master, Menus, relief=3DRAISED, **kw):
        kw['relief'] =3D relief
        apply(Frame.__init__, (self, master), kw)
        self.pack(side=3DTOP, expand=3DYES, fill=3DBOTH)

        for x in Menus:
            cmdbutn, cmdlist =3D x
            CommandButton(self, cmdlist, text=3Dcmdbutn)
           =20
           =20
class CommandButton(Menubutton):
    def __init__(self, MenuBar, commandlist, **kw):
        apply(Menubutton.__init__, (self, MenuBar), kw)
        self.pack(side=3DLEFT, padx=3D"2m")
        self.menu =3D Menu(self)
        for x in commandlist:
            L, C, S =3D x
            self.menu.add_command(label=3DL, command=3DC, state=3DS)

        self['menu'] =3D self.menu
           =20

class PhoneLabel(Frame):
    def __init__(self, master, text):
        Frame.__init__(self, master, bg=3D'gray40')
        self.pack(side=3DLEFT, expand=3DYES, fill=3DBOTH)
        Label(self, text=3Dtext, fg=3D'steelblue1', font=3D("arail", 10, =
"bold"),
              width=3D5, bg=3D'gray40').pack(side=3DLEFT, expand=3DYES, =
fill=3DBOTH)


class VirtualHandSet(Frame):
    def __init__(self, parent=3DNone):
        Frame.__init__(self, bg=3D'gray40')
        self.pack(expand=3DYES, fill=3DBOTH)
        self.master.title('Virtual Hand Set')
        self.master.iconname('VHS')
        self.EventQ =3D []
        self.current =3D ''
        self.BuildVirtualHandSet()
       =20

    def EvalAction(self, action):
        self.EventQ.append(action)

       =20
       =20
   =20
    def EvalKey(self, key):
        self.display.insert(END, key)
        self.current =3D self.current + key
      =20
       =20

    def GetEventQ(self):
        return self.EventQ

    def GetDigitMap(self):
        return self.current


    def ConfigMenu(self):
        print 'This is not implemented yet'

    def __del__(self):
        sys.exit(0)
    =20

    def BuildVirtualHandSet(self):
        FUN =3D 1
        KEY =3D 0
        KC1 =3D 'gray30'      #Dark Keys
        KC2 =3D 'gray50'      #Light Keys
        KC3 =3D 'steelblue1'  #Light Blue Keys
        KC4 =3D 'steelblue'   #Dark Blue Keys
        OnOffBD =3D 10
        FunBD =3D 3
        KeyBD =3D 3
        Keys =3D [
                [('ON/OFF', '', FUN, 'OnOff', OnOffBD),
                ],
                [=20
                  ('Flash', '', FUN, 'Flash', FunBD),
                  ('Mute', '', FUN, 'Mute', FunBD),
                  ('R', '', FUN, 'Redial', FunBD)
                ],
                [ ('1', 'abc', KEY, '1', KeyBD),
                  ('2', 'def', KEY, '2', KeyBD),
                  ('3', 'ghi', KEY, '3', KeyBD),
                 =20
                ],
                [ ('4', 'jkl', KEY, '4', KeyBD), =20
                  ('5', 'mno', KEY, '5', KeyBD),
                  ('6', 'pqr', KEY, '6', KeyBD)
                ],
                [
                  ('7', 'stu', KEY, '7', KeyBD),
                  ('8', 'vwx', KEY, '8', KeyBD),
                  ('9', 'yz', KEY, '9', KeyBD)
                ],
                [
                  ('*', '', KEY, '*', KeyBD),
                  ('0', '', KEY, '0', KeyBD),
                  ('#', '', KEY, '#', KeyBD)

                ]
               ]

        MenuItems =3D [ ('Config', [('IP Parameters', self.ConfigMenu, =
DISABLED),
                                  ('Layout', self.ConfigMenu, DISABLED)  =

                                 ]
       =20
                      )
                   =20

                    ]

       =20
        MenuBar(self, MenuItems,relief=3DRAISED, bd=3D1)
               =20
        self.display =3D Pmw.ScrolledText(self, hscrollmode=3D'dynamic',
                                  vscrollmode=3D'dynamic',
                                  hull_relief=3D'sunken',
                                  hull_background=3D'gray40',
                                  hull_borderwidth=3D3,
                                  text_background=3D'honeydew4',
                                  text_width=3D20,
                                  text_foreground=3D'black',
                                  text_height=3D3,
                                  text_padx=3D10, text_pady=3D10,
                                  text_relief=3D'groove',
                                  text_font=3D('arial', 12, 'bold'))
        self.display.pack(side=3DTOP, expand=3DYES, fill=3DBOTH)

        for row in Keys:
            rowa =3D Frame(self, bg=3D'gray40')
            rowb =3D Frame(self, bg=3D'gray40')
            for p1, p2, ktype, func, bd in row:
                PhoneLabel(rowa, text=3Dp2)
                if ktype =3D=3D FUN:
                    a =3D lambda s=3Dself, a=3Dfunc: s.EvalAction(a)
                    Key(rowb, text=3Dp1, bg=3DKC4, bd=3Dbd, command=3Da)
                else:
                    a =3D lambda s=3Dself, k=3Dfunc: s.EvalKey(k)
                    Key(rowb, text=3Dp1, bg=3DKC1, bd=3Dbd, command=3Da)
                   =20
            rowa.pack(side=3DTOP, expand=3DYES, fill=3DBOTH)
            rowb.pack(side=3DTOP, expand=3DYES, fill=3DBOTH)

   =20
       =20
   =20





class VirtualHandSetProcessor:
    def __init__(self):
        self.handset =3D VirtualHandSet()
#        self.Pipe =3D TcpIpPipe()
        thread.start_new_thread(self.WorkerThread, ())

    def WorkerThread(self):
        while(1):
            print 'Hello'
            print self.handset.GetEventQ()
            sleep(1)



if __name__ =3D=3D '__main__':
    print "Hello"
    a =3D VirtualHandSetProcessor()
    print "Hello"
   =20

   =20


       =20
       =20
   =20
   =20


       =20
       =20
       =20
       =20
       =20
   =20

       =20

------=_NextPart_000_0003_01C1379B.65F750D0--