Executing system commands with wxpython

T

twsnnva

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.
 
P

Paul McNett

twsnnva said:
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
 
P

Peter Hansen

twsnnva said:
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()
 
T

Tom B.

twsnnva said:
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.
 
C

Cliff Wells

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
 
P

Peter Hansen

Cliff said:
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
 
C

Cliff Wells

Cliff said:
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 ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top