10 Replies - 3594 Views - Last Post: 19 March 2010 - 12:31 PM Rate Topic: -----

#1 harryx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-March 10

Python menu program

Posted 14 March 2010 - 12:13 PM

menu program

Using ubuntu 9.10 and python, constructed with Boa:
I have written a desktop menu to provide quick access to application. It works well except that after I have opened one app, the menu program will not allow a second app to be opened until the first is closed.
I a second app is clicked, there is no response until the first is closed. the second app then starts to appear, but freezes the computer before it is complete.
A window labeled Metacity gives the message "<Subject> is not responding. Neither the Force Quit button nor the Wait button, nor the X at the top right have any effect. Opening a terminal to facilitate the kill command is frustrated when it too freezes. Only a switch off and a reboot will clear the computer.

Here is some of the button code I am using:
--------------------------------------------------------------------
   def OnOOoCalcBttnButton(self, event):
       os.system("ooffice -calc")

   def OnOpenOfficeNewButton(self, event):
       os.system("ooffice")              
   def OnBttnThunderbirdReadButton(self, event):
       os.system("thunderbird")


------------------------------------------------------------

Is there something that can be added to these functions which will eliminate my problem?
Manitoban HarryX

This post has been edited by Dogstopper: 14 March 2010 - 02:38 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Python menu program

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Python menu program

Posted 14 March 2010 - 02:43 PM

you're making it sound like you have a GUI that when you click on a button, it executes one of the above methods...I think we'll see some more of your code to be sure, but it could simply be that you need threads to execute because if a method starts a program, in some cases (not sure about Python), that method does not return until the program returns...

Either try Threads or show us the rest of your code, or both...
Was This Post Helpful? 0
  • +
  • -

#3 harryx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-March 10

Re: Python menu program

Posted 15 March 2010 - 07:31 AM

Thank you for your reply ... here is the code that was used

Harry



#Boa:Frame:Frame1

import wx, os

def create(parent):
    return Frame1(parent)

[wxID_FRAME1, wxID_FRAME1BTTNCLOSE, wxID_FRAME1BTTNEMAIL, wxID_FRAME1BTTNGIMP, 
 wxID_FRAME1BTTNMAHJONGG, wxID_FRAME1BTTNOOCALC, wxID_FRAME1BTTNOOWRITER, 
 wxID_FRAME1BUTTON1, wxID_FRAME1PANEL1, wxID_FRAME1STATICTEXT1, 
 wxID_FRAME1STATICTEXT2, wxID_FRAME1TEXTCTRL1, wxID_FRAME1TEXTCTRL2, 
] = [wx.NewId() for _init_ctrls in range(13)]

class Frame1(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
              pos=wx.Point(566, 354), size=wx.Size(638, 319),
              style=wx.DEFAULT_FRAME_STYLE, title=u'My Menu')
        self.SetClientSize(wx.Size(638, 319))

        self.panel1 = wx.Panel(id=wxID_FRAME1PANEL1, name='panel1', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(638, 319),
              style=wx.TAB_TRAVERSAL)

        self.BttnEmail = wx.Button(id=wxID_FRAME1BTTNEMAIL, label=u'E-Mail',
              name=u'BttnEmail', parent=self.panel1, pos=wx.Point(56, 16),
              size=wx.Size(85, 29), style=0)
        self.BttnEmail.Bind(wx.EVT_BUTTON, self.OnBttnEmailButton,
              id=wxID_FRAME1BTTNEMAIL)

        self.BttnOoWriter = wx.Button(id=wxID_FRAME1BTTNOOWRITER,
              label=u'Open Office Writer', name=u'BttnOoWriter',
              parent=self.panel1, pos=wx.Point(56, 64), size=wx.Size(168, 29),
              style=0)
        self.BttnOoWriter.Bind(wx.EVT_BUTTON, self.OnBttnOoWriterButton,
              id=wxID_FRAME1BTTNOOWRITER)

        self.BttnOoCalc = wx.Button(id=wxID_FRAME1BTTNOOCALC,
              label=u'Open Office Calc', name=u'BttnOoCalc', parent=self.panel1,
              pos=wx.Point(56, 112), size=wx.Size(168, 29), style=0)
        self.BttnOoCalc.Bind(wx.EVT_BUTTON, self.OnBttnOoCalcButton,
              id=wxID_FRAME1BTTNOOCALC)

        self.BttnGimp = wx.Button(id=wxID_FRAME1BTTNGIMP, label=u'Gimp',
              name=u'BttnGimp', parent=self.panel1, pos=wx.Point(56, 152),
              size=wx.Size(85, 29), style=0)
        self.BttnGimp.Bind(wx.EVT_BUTTON, self.OnBttnGimpButton,
              id=wxID_FRAME1BTTNGIMP)

        self.BttnMahjongg = wx.Button(id=wxID_FRAME1BTTNMAHJONGG,
              label=u'Editor', name=u'BttnMahjongg', parent=self.panel1,
              pos=wx.Point(56, 200), size=wx.Size(85, 29), style=0)
        self.BttnMahjongg.Bind(wx.EVT_BUTTON, self.OnBttnMahjonggButton,
              id=wxID_FRAME1BTTNMAHJONGG)

        self.BttnClose = wx.Button(id=wxID_FRAME1BTTNCLOSE,
              label=u'Close My Menu', name=u'BttnClose', parent=self.panel1,
              pos=wx.Point(244, 258), size=wx.Size(128, 29), style=0)
        self.BttnClose.Bind(wx.EVT_BUTTON, self.OnBttnCloseButton,
              id=wxID_FRAME1BTTNCLOSE)

        self.textCtrl1 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL1, name='textCtrl1',
              parent=self.panel1, pos=wx.Point(354, 76), size=wx.Size(264, 27),
              style=0, value=u'')

        self.textCtrl2 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL2, name='textCtrl2',
              parent=self.panel1, pos=wx.Point(352, 124), size=wx.Size(256, 27),
              style=0, value=u'')

        self.staticText1 = wx.StaticText(id=wxID_FRAME1STATICTEXT1,
              label=u'To,', name='staticText1', parent=self.panel1,
              pos=wx.Point(288, 80), size=wx.Size(39, 17), style=0)

        self.staticText2 = wx.StaticText(id=wxID_FRAME1STATICTEXT2,
              label=u'Subject,', name='staticText2', parent=self.panel1,
              pos=wx.Point(288, 128), size=wx.Size(72, 24), style=0)

        self.button1 = wx.Button(id=wxID_FRAME1BUTTON1, label=u'Gmail',
              name='button1', parent=self.panel1, pos=wx.Point(56, 256),
              size=wx.Size(88, 29), style=0)
        self.button1.Bind(wx.EVT_BUTTON, self.OnButton1Button,
              id=wxID_FRAME1BUTTON1)

    def __init__(self, parent):
         self._init_ctrls(parent)

    def OnBttnEmailButton(self, event):
        
        tempstr = 'thunderbird -compose'
        tempstr2 = self.textCtrl1.GetValue()
        if tempstr2 !='':
            tempstr = + 'to=' + tempstr2 + ','
            
        tempstr3 = self.textCtrl2.GetValue()
        if tempstr3 !='':
            tempstr = tempstr + 'subject=' + tempstr3
            
        
        print (tempstr)
        os.system(tempstr)
        

    def OnBttnOoWriterButton(self, event):
        os.system("ooffice -writer /home/harry/Documents/")

    def OnBttnOoCalcButton(self, event):
        os.system("ooffice -calc /home/harry/Documents/")

    def OnBttnGimpButton(self, event):
        os.system("gimp")

    def OnBttnMahjonggButton(self, event):
        os.system("gedit")

    def OnBttnCloseButton(self, event):
        self.Close()

    def OnButton1Button(self, event):
        os.system('firefox gmail.com')


if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = create(None)
    frame.Show()

    app.MainLoop()



Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,760
  • Joined: 16-October 07

Re: Python menu program

Posted 15 March 2010 - 08:39 AM

You're waiting for os.system to finish and return a result. If that's not your intent, try Popen instead.

e.g.
subprocess.Popen(["firefox","gmail.com"])


Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,760
  • Joined: 16-October 07

Re: Python menu program

Posted 15 March 2010 - 10:10 AM

I've never played with wx in Python. Nor used a gui generator. What you're doing doesn't seem to need one; plus they tend to write horrid code.

Here's how I'd do it by hand, if you're interested.
import wx, os
import subprocess

class ButtonPanel(wx.Panel):
	def __init__(self, prnt, buttons):
		wx.Panel.__init__(self, id=wx.NewId(), parent=prnt,
			pos=wx.Point(0, 0), size=wx.Size(320, (len(buttons) * 32)+10),
			style=wx.TAB_TRAVERSAL)

		y = 5
		for (labelName, func) in buttons:
			buttonId = wx.NewId()
			button = wx.Button(id=buttonId, label=labelName, parent=self,
				pos=wx.Point(10, y), size=wx.Size(300, 29), style=0)
			button.Bind(wx.EVT_BUTTON, func, id=buttonId)
			y += 32

class MenuFrame(wx.Frame):
	def __init__(self, p=None):
		wx.Frame.__init__(self, id=wx.NewId(), name='', parent=p,
			pos=wx.Point(566, 354),
			style=wx.DEFAULT_FRAME_STYLE, title=u'Test Menu')
		
		def itemNotePad():
			def cmd(event): subprocess.Popen(['gedit'])
			return ['Notepad', cmd]

		def itemCalc():
			def cmd(event): subprocess.Popen(['gcalctool'])
			return ['Calculator', cmd]

		def itemClose():
			def cmd(event): self.Close()
			return ['Close', cmd]

		bp = ButtonPanel(self, [itemNotePad(),itemCalc(),itemClose()])
		self.SetClientSize(bp.GetClientSize())


if __name__ == '__main__':
	app = wx.PySimpleApp()
	frame = MenuFrame()
	frame.Show()

	app.MainLoop()


This post has been edited by baavgai: 15 March 2010 - 10:11 AM

Was This Post Helpful? 0
  • +
  • -

#6 harryx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-March 10

Re: Python menu program

Posted 15 March 2010 - 01:49 PM

hello baavgai

An interesting approach, I have copied the code an will try adding

the other programs in the the program I posted ... And add color to the text and background

thank you

Harry

This post has been edited by harryx: 15 March 2010 - 02:06 PM

Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,760
  • Joined: 16-October 07

Re: Python menu program

Posted 16 March 2010 - 04:56 AM

View Postharryx, on 15 March 2010 - 02:49 PM, said:

An interesting approach, I have copied the code an will try adding


Ultimately, I have to keep myself entertained. ;) There are, of course, innumerable ways to do a menu, even within the constraints of being just buttons.

Upon thinking upon it, I believe the code would be easier to extend if the buttons stored extra information that was passed on click and the main program could process all the buttons on one spot. I also looked at wxWidgets docs to see if there were any tricks I was missing.

Here's another way to do it that I like better.
import wx, os
import subprocess

class ButtonPanel(wx.Panel):
	def __init__(self, parent, buttons, onclick):
		(buttonSize, padding, buttonGutter) = (wx.Size(300, 29), 5, 3)
		w = buttonSize.width + padding + padding
		h = len(buttons) * (buttonSize.height + buttonGutter)
		h += padding + padding
		wx.Panel.__init__(self, parent, wx.ID_ANY, wx.Point(0,0), wx.Size(w, h), wx.TAB_TRAVERSAL)

		y = padding
		for info in buttons:
			button = wx.Button(self, wx.ID_ANY, info[0], wx.Point(padding, y), buttonSize)
			button.Bind(wx.EVT_BUTTON, onclick)
			button.action = info[1]
			button.args = info[2:]
			y += buttonSize.height + buttonGutter


class MenuFrame(wx.Frame):
	def __init__(self, p=None):
		wx.Frame.__init__(self, p, wx.ID_ANY, 'Menu', wx.DefaultPosition, wx.DefaultSize)
			
		buttons = [
			['Notepad', 'exec', 'gedit'],
			['Calculator', 'exec', 'gcalctool'],
			['Close', 'close']
		]
		bp = ButtonPanel(self, buttons, self.OnProcessButton)
		self.SetClientSize(bp.GetClientSize())
		self.Centre();

	def OnProcessButton(self, event):
		button = event.GetEventObject()
		if button.action=="exec":
			subprocess.Popen(button.args)
		elif button.action=="close":
			self.Close()


if __name__ == '__main__':
	app = wx.PySimpleApp()
	frame = MenuFrame()
	frame.Show()

	app.MainLoop()


Was This Post Helpful? 1
  • +
  • -

#8 Guest_harryx*


Reputation:

Re: Python menu program

Posted 17 March 2010 - 11:02 AM

baavgai

I am able to change the programs in "buttons = [ ]" adding others as indicated in the above problem

The 'subprocess' statement was helpful in getting the original post program working

Doing graphical programs , adding color , text, background and images has been a problem

Google searches and books have not helped get the results I was soso

Thank you for you insights and assistance

Manitoban , Harry
Was This Post Helpful? 0

#9 harryx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-March 10

Re: Python menu program

Posted 17 March 2010 - 11:04 AM

View Postharryx, on 17 March 2010 - 10:02 AM, said:

baavgai

I am able to change the programs in "buttons = [ ]" adding others as indicated in the above problem

The 'subprocess' statement was helpful in getting the original post program working

Doing graphical programs , adding color , text, background and images has been a problem

Google searches and books have not helped get the results I was soso

Thank you for you insights and assistance

Manitoban , Harry

Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,760
  • Joined: 16-October 07

Re: Python menu program

Posted 17 March 2010 - 05:34 PM

View Postharryx, on 17 March 2010 - 12:02 PM, said:

Doing graphical programs , adding color , text, background and images has been a problem


Not real sure what you're looking for here. Changing standard controls is usually a bad idea; it's inconsistent and thus confusing. It's still easy enough, though.

If you want to draw your own stuff, just grab the drawing context and have at.

Here's a quick example.
import wx, os

class DoodleButton(wx.Window):
	def __init__(self, parent):
		wx.window.__init__(self, parent)
		self.Bind(wx.EVT_PAINT, self.OnPaint)
		self.SetSize(wx.Size(64,64))
	
	def OnPaint(self, event):
		dc = wx.PaintDC(self)
		size = dc.GetSize()
		center = wx.Point(size.width/2, size.height/2)
		dc.Clear()
		dc.SetPen(wx.Pen(wx.BLACK, 3))
		dc.DrawCirclePoint(center, size.width/2 - 6) 
		dc.DrawCirclePoint(center, size.width/3 - 6) 
		dc.SetPen(wx.Pen(wx.RED, 4))
		dc.DrawCirclePoint(center, size.width/6 - 6) 
		

class Frame1(wx.Frame):
	def __init__(self, p=None):
		wx.Frame.__init__(self, p, wx.ID_ANY, 'Color Button Test')
		panel = wx.Panel(self, wx.ID_ANY)
		sizer = wx.BoxSizer(wx.VERTICAL)
		b = wx.Button(panel, wx.ID_ANY, 'Red Button')
		b.SetOwnBackgroundColour(wx.Colour(255, 0, 0))
		sizer.Add(B)/>
		sizer.Add(wx.Button(panel, wx.ID_ANY, 'Other Button'))
		sizer.Add(DoodleButton(panel))
		sizer.SetSizeHints(self)
		panel.SetSizer(sizer)
		

if __name__ == '__main__':
	app = wx.PySimpleApp()
	frame = Frame1()
	frame.Show()
	app.MainLoop()


Was This Post Helpful? 0
  • +
  • -

#11 harryx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-March 10

Re: Python menu program

Posted 19 March 2010 - 12:31 PM

Baavgai

Finally got the doodle button working , found two glitches
case sensitive letters and changing the size

Your second menu code post, need to be able to extend the menu list of programs
and add color to the button text

Regarding my original post it is working .. the subprocess import made the
difference... I can see where you command line code is neater and cleaner
I will be tackling it as well

Appreciate your assistance

Harry
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1