473,320 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Executing system commands with wxpython

Could anyone give me an example (code) of a simple program with a button
that when clicked executes a linux shell or windows dos command like
"ifconfig" or "ipconfig" and prints the output somewhere in the same
window. Thanks.

Jul 18 '05 #1
6 5822
twsnnva writes:
Could anyone give me an example (code) of a simple program
with a button that when clicked executes a linux shell or
windows dos command like "ifconfig" or "ipconfig" and prints
the output somewhere in the same window. Thanks.


Okay, I just re-read your subject and you specified wxPython
there, but I already wrote up how to do it with Tkinter. The
irony is that I'm way more comfortable with wxPython, and had
to spend extra time looking up the Tkinter syntax. Anyway, I'm
not sure if your question is more "how to execute system
commands" or "how to display a button" so why don't you run
with this code and see if it works for you.

Hint: you'll use the same Python code to execute system commands
- the ui toolkit doesn't matter.

# -- Begin sample code

import sys, os
import Tkinter

# Define the main root top-level window:
root = Tkinter.Tk()

# Define the button and edit area:
button = Tkinter.Button(root, text="IP Configuration")
edit = Tkinter.Text(root)

# Lay out the button and edit area:
button.pack()
edit.pack()

# Define the callback function for the button click:
def sysCommand(evt):
if "linux" in sys.platform:
file = os.popen("/sbin/ifconfig")
result = file.read()
file.close()
elif "win" in sys.platform:
file = os.popen("ipconfig")
result = file.read()
file.close()
else:
result = "Unsupported Platform: '%s'" % sys.platform
edit.insert(Tkinter.END, result)
# Bind a click of the button to our callback function:
button.bind("<Button>", sysCommand)
button.bind("<space>", sysCommand)

Tkinter.mainloop()

#-- end sample code
--
Paul McNett
Independent Software Consultant
http://www.paulmcnett.com
Jul 18 '05 #2
twsnnva wrote:
Could anyone give me an example (code) of a simple program with a button
that when clicked executes a linux shell or windows dos command like
"ifconfig" or "ipconfig" and prints the output somewhere in the same
window. Thanks.


Simplified example... just meets your requirements.

import wx

class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, 'Test')
p = wx.Panel(self, -1)

b = wx.Button(p, 10, "A Button")
self.Bind(wx.EVT_BUTTON, self.OnClick, b)

self.tc = wx.StaticText(p, -1, '')
self.tc.SetPosition((3, 25))

self.Show(True)
def OnClick(self, event):
import os
result = os.popen('ipconfig').read()
self.tc.SetLabel(result)
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = MyFrame()
app.MainLoop()

Jul 18 '05 #3

"twsnnva" <tw*****@msn.com> wrote in message
news:3d******************************@localhost.ta lkaboutprogramming.com...
Could anyone give me an example (code) of a simple program with a button
that when clicked executes a linux shell or windows dos command like
"ifconfig" or "ipconfig" and prints the output somewhere in the same
window. Thanks.

First ,

import popen2

In your 'OnButtonEvent' function,

tomin, tomout = popen2.popen4( 'ipconfig')
ipconfig_text = tomin.read()
tomout.close()
tomin.close()

then you can put the results into your window of choice.

Tom
P.S. Try it out by using the new modifiable demo.

Jul 18 '05 #4
On Fri, 2004-09-10 at 13:02 -0400, twsnnva wrote:
Could anyone give me an example (code) of a simple program with a button
that when clicked executes a linux shell or windows dos command like
"ifconfig" or "ipconfig" and prints the output somewhere in the same
window. Thanks.


Building on Peter's example, here's a bit more sophisticated example:

import wx, os

if '__WXMSW__' in wx.PlatformInfo:
PRESETS = [
'ipconfig',
]
else:
PRESETS = [
'/sbin/ifconfig',
]

class MyPanel(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id)
sizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(sizer)

self.output = wx.TextCtrl(self, -1, style = wx.TE_MULTILINE)

sizer2 = wx.BoxSizer(wx.HORIZONTAL)

runButton = wx.Button(self, -1, "Run Command")
self.Bind(wx.EVT_BUTTON, self.OnClick, runButton)

self.command = wx.ComboBox(self, -1, choices = PRESETS)

sizer2.AddMany([
(self.command, 1, wx.ALL, 5),
(runButton, 0, wx.ALL, 5),
])

sizer.AddMany([
(self.output, 1, wx.EXPAND | wx.ALL, 5),
(sizer2, 0, wx.EXPAND),
])

def OnClick(self, event):
cmd = self.command.GetValue()
if cmd:
input, output, errors = os.popen3(cmd)
errors = errors.read()
if errors:
dlg = wx.MessageDialog(self, errors,
'An error occurred',
wx.OK | wx.ICON_EXCLAMATION)
dlg.ShowModal()
self.output.SetValue('')
else:
self.output.SetValue(output.read())
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, 'Test')
p = MyPanel(self, -1)
self.Show(True)
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = MyFrame()
app.MainLoop()
Regards,
Cliff

--
Cliff Wells <cl************@comcast.net>

Jul 18 '05 #5
Cliff Wells wrote:
On Fri, 2004-09-10 at 13:02 -0400, twsnnva wrote:
Could anyone give me an example (code) of a simple program with a button
that when clicked executes a linux shell or windows dos command like
"ifconfig" or "ipconfig" and prints the output somewhere in the same
window. Thanks.

Building on Peter's example, here's a bit more sophisticated example:

[snip longer version]

Cliff, I think this is the point where we're supposed to
vote on the name, and start a new project on SourceForge. ;-)

-Peter
Jul 18 '05 #6
On Fri, 2004-09-10 at 23:50 -0400, Peter Hansen wrote:
Cliff Wells wrote:
On Fri, 2004-09-10 at 13:02 -0400, twsnnva wrote:
Could anyone give me an example (code) of a simple program with a button
that when clicked executes a linux shell or windows dos command like
"ifconfig" or "ipconfig" and prints the output somewhere in the same
window. Thanks.

Building on Peter's example, here's a bit more sophisticated example:

[snip longer version]

Cliff, I think this is the point where we're supposed to
vote on the name, and start a new project on SourceForge. ;-)


And don't forget the required next step which is to abandon the project
shortly after when you get a real job ;)
--
Cliff Wells <cl************@comcast.net>

Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Simon Hawkins | last post by:
I seem to remember there's a java function for executing commands on the native system (UNIX in this case) but cannot remember which. Anyone know? Also, is it possible to grab the results of the...
3
by: Micah | last post by:
Is there an os module call to request that Windows handle a specific file using the default handler? For example, if I pass it a .txt file it opens notepad but if I pass a .wav file it opens...
4
by: Justine | last post by:
Hi All, How can v execute DOS command while executing Console Application written using C#, like cls,dir commnads. Thanz in Advance, Justine
2
by: Pratibha | last post by:
Hi, I want to execute commands through .NET. Using process, i can execute them in WindowsApplication but the process doesn't work in WebApplication. I am executing a batch file which contains...
2
by: Pascal Polleunus | last post by:
(another try with a different subject as it doesn't seem to work with "EXECUTE + transaction = unexpected error -8" :-/) Hi, It seems that there is a problem when executing a dynamic...
3
by: moxie | last post by:
I am trying to rsh from a server that does not have python installed to a server that has python but I get error message rsh python-server "/usr/local/bin/python /home/s/src/utpython/UnitTrip.py"...
10
by: Kentor | last post by:
I know I can use the system function to execute commands, but I need to pass parameters to it and it doesn't want to accept them... is there a way to do system("mv %s temp123", myvar) ? or maybe...
9
by: =?Utf-8?B?UGF1bCBQb2xpY2FycA==?= | last post by:
I'm attempting to make an application that will call and execute multiple "system()" cmd commands simultaneously. The problem is that when the program hits the first system() it waits for it to...
3
by: jim3371 | last post by:
Using wxPython, I'm looking to build a GUI app for a daemon-based app, on Win32 platform, how would I go about executing the daemon app so it stays in the background when the Py app is running?...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.