LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 09-11-2005, 02:24 PM   #1
arpanet1969
LQ Newbie
 
Registered: Dec 2004
Location: On your SQL server.
Distribution: Mandrivia Linux 10.1
Posts: 25

Rep: Reputation: 15
wxPython textbox command line help


Hi, I'm messing about with Python/wxPython, and I have a few questions. Firstly, as Python was named after Monty Python's Flying Circus, then why doesn't it use a circus tent for it's main logo, rather than a snake? (The documentation even states it was not named after a snake)...

Anyway the main question is this... I have a very basic wxPython app, which loads a window with a textbox, menubar and statusbar. How would I go about adding some script to allow python to run a command line argument, THEN display the results of the argument in the textbox? My code is as follows:

Code:
import os
import wx
ID_ABOUT=111
ID_EXIT=110
ID_OPEN=101
ID_SAVE=102
ID_SAVEAS=103
class MainWindow(wx.Frame):
    def __init__(self,parent,id,title):
        wx.Frame.__init__(self,parent,wx.ID_ANY, title, size = ( 200,100),

                                        style=wx.DEFAULT_FRAME_STYLE|
                                        wx.NO_FULL_REPAINT_ON_RESIZE)
        self.control = wx.TextCtrl(self, 1, style=wx.TE_MULTILINE)
        self.CreateStatusBar() # A StatusBar in the bottom of the window
        # Setting up the menu.
        filemenu= wx.Menu()
	filemenu.Append(ID_OPEN, "&Open..."," Open a Python app to edit")
	filemenu.Append(ID_SAVE, "&Save..."," Save the current document")
	filemenu.Append(ID_SAVEAS, "&Save As..."," Save the current document as...")
        filemenu.AppendSeparator()
        filemenu.Append(ID_EXIT,"E&xit"," Terminate the program")
	
	aboutmenu= wx.Menu()
	aboutmenu.Append(ID_ABOUT, "&About"," Information about this program")
        # Creating the menubar.
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
	menuBar.Append(aboutmenu,"&Help")
        self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame content.
        wx.EVT_MENU(self, ID_ABOUT, self.OnAbout) # attach the menu-event ID_ABOUT to the
                                                           # method self.OnAbout
        wx.EVT_MENU(self, ID_EXIT, self.OnExit)   # attach the menu-event ID_EXIT to the
                                                           # method self.OnExit
	wx.EVT_MENU(self, ID_OPEN, self.OnOpen)
        self.Show(True)
    def OnAbout(self,e):
        d= wx.MessageDialog( self, " A sample editor \n"
                            " in wxPython","About Sample Editor", wx.OK)
                            # Create a message dialog box
        d.ShowModal() # Shows it
        d.Destroy() # finally destroy it when finished.
    def OnExit(self,e):
        self.Close(True)  # Close the frame.
    def OnOpen(self,e):
        """ Open a file"""
        self.dirname = ''
        dlg = wx.FileDialog(self, "Choose a file", self.dirname, "Sample Editor", "*.*", wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            self.filename=dlg.GetFilename()
            self.dirname=dlg.GetDirectory()
            f=open(os.path.join(self.dirname,self.filename),'r')
            self.control.SetValue(f.read())
            f.close()
        dlg.Destroy()
app = wx.PySimpleApp()
frame = MainWindow(None, -1, "Sample editor")
app.MainLoop()
I would of course need to add another textbox above for the command line argument itself, followed by a button to 'action' the argument, but basically I'm building a GUI frontend for an app I coded...

Any help would be much appreciated

Arpanet.

P.S. Are there any good wxPython tutorials out there other than:
http://www.wxpython.org/tutorial.php
http://wiki.wxpython.org/

Last edited by arpanet1969; 09-11-2005 at 02:25 PM.
 
Old 09-12-2005, 01:08 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Re: wxPython textbox command line help

Quote:
Originally posted by arpanet1969
Firstly, as Python was named after Monty Python's Flying Circus, then why doesn't it use a circus tent for it's main logo, rather than a snake? (The documentation even states it was not named after a snake)...
Don't ask me, I don't care :-)
Quote:
How would I go about adding some script to allow python to run a command line argument, THEN display the results of the argument in the textbox? My code is as follows:
Here's a working example:
Code:
#!/usr/bin/env python

import os
from wxPython.wx import *

class MainWindow(wxFrame):
	def __init__(self, parent, title, size):
		wxFrame.__init__(self, parent, wxID_ANY, title, wxDefaultPosition, size)

		# Create controls
		commandBoxID = wxNewId()
		outputBoxID = wxNewId()
		self.commandBox = wxTextCtrl(self, commandBoxID, style=wxTE_PROCESS_ENTER)
		self.outputBox = wxTextCtrl(self, outputBoxID, style=wxTE_MULTILINE|wxHSCROLL)

		# Basic control layout
		sizer = wxBoxSizer(wxVERTICAL)
		sizer.Add(self.commandBox, 1, wxEXPAND | wxALL, 10)
		sizer.Add(self.outputBox, 6, wxEXPAND | wxALL | wxTE_READONLY, 10)
		self.SetSizer(sizer)

		# Make command-box resond to ENTER:
		EVT_TEXT_ENTER(self, commandBoxID, self.OnEnter)

		# Start
		self.Show(true)

	def OnEnter(self, e):
		shellCommand = e.GetString()
		self.outputBox.Clear()
		
		# Execute the command, getting output through a  pipe: popen()
		pipe = os.popen(shellCommand)
		while True:
			line = pipe.readline()
			if not line:
				break
			self.outputBox.AppendText(line)
		print "Commmand exit code was:", pipe.close()

app = wxPySimpleApp()
MainWindow(None, "Command Box", (480, 320))
app.MainLoop()
Quote:
P.S. Are there any good wxPython tutorials out there other than:
http://www.wxpython.org/tutorial.php
http://wiki.wxpython.org/
Yes, documentation for wxPython is a little terse, but rather complete though. The only other tutorialish thing that I know of is the demo that ships with wxPython. The code is viewable from the demo itself, and the demo shows how to use all controls with commented code.

Just play a lot with wxPython. And make sure your know enough about python itself (which does have many tutorials). After playing with modifying demo code, try to read the real doc's, which may be a little difficult as it's C++ documentation for wxWidgets, with some wxPython specific notes.

Have a lot of fun. I started Python/wxPython about 6 weeks ago, and I like it very much!
 
Old 09-12-2005, 07:51 PM   #3
arpanet1969
LQ Newbie
 
Registered: Dec 2004
Location: On your SQL server.
Distribution: Mandrivia Linux 10.1
Posts: 25

Original Poster
Rep: Reputation: 15
Thanks very much for your reply. The code you offered works perfectly! I'm reading more and more, just working out how to use some kinda config file for my app now, so the configuration panel can be used to save the user's preferances to a file, which can then be re-loaded back into the app at runtime. I'm using the open() and split() functions at the mo.

Thanks again for your response
 
Old 09-13-2005, 07:40 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally posted by arpanet1969
I'm reading more and more, just working out how to use some kinda config file for my app now, so the configuration panel can be used to save the user's preferances to a file, which can then be re-loaded back into the app at runtime. I'm using the open() and split() functions at the mo.
Have a look overhere.
(note that the object also has a write() method)
 
Old 09-13-2005, 07:43 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Or you could try to use pickle.
Probably easier to use, but the config-file will not be readable / editable by hand.
 
Old 09-13-2005, 06:38 PM   #6
arpanet1969
LQ Newbie
 
Registered: Dec 2004
Location: On your SQL server.
Distribution: Mandrivia Linux 10.1
Posts: 25

Original Poster
Rep: Reputation: 15
Wow, your knowledge has been very useful to me! Please don't hesitate to continue offering advice! Thanks again for your support!



Arpanet.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a single command to list all hardware installed (command line)? davee Linux - Hardware 6 02-28-2009 08:19 PM
Command to output file content line by line aznluvsmc Programming 2 09-12-2004 08:45 PM
51 characters only in the 1st Line of command line eggCover Linux - General 2 07-29-2004 02:28 PM
Command to display whole filestructure hierarchy f/ command line? mjewell Linux - Newbie 10 01-19-2004 11:48 AM
Where is Command line utility for Cups and command tutorial mossy Linux - Software 8 01-16-2004 01:24 AM


All times are GMT -5. The time now is 09:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration