Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

input parameters for python using GUI instead raw_input

Status
Not open for further replies.

TarunMakhija

Programmer
Jun 14, 2005
13
US
Hi all,

I am a software engineer by profession working in the area of Content Management, albeit in the J2EE and FileNet platforms.

I intend to make a transition to Python and at a later stage to Zope for building web based applications. Need your help for the same and it would be absolutely great if you spend some time on my rather basic query ---->

I have a CRM application that I have developed completely in Python. Although the application is ready I am taking certain user information through raw_input which is what I now want to replace by a GUI.

For simplicity here is what I have in my code currently,

param1 = raw_input('Enter the first parameter: ')
param2 = raw_input('Enter the second parameter: ')

Now this is where I want to pass param1 and param2 through a page (user interface) where I get to enter the fields for param1 and param2 and on submission, my python code can successfully execute the following in my "__main__":

x=Summary(param1, param2)
x.run()

I hence want to take input parameters into my python code through a GUI instead of raw_input
Hope I have communicated my doubt precisely. I am currently testing this application on Windows.

Eagerly awaiting guidance,

Thanks & Regards,
Tarun M.
 
I don't know if i understood it fine, you mean to create a user interface with Tkinter, wxpython... where you could place different widgets to make it work?

example:

from Tkinter import*
win=Tk()
Frame(win,width=200,height=150).pack()
def go():
print param1.get()+param2.get()
param1 = StringVar()
param2 = StringVar()
Label(win,text='Param1').place(x=, y=)
Label(win,text='Param2').place(x=, y=)
Entry(win,width=10,textvariable=param1).place(x=, y=)
Entry(win,width=10,textvariable=param2).place(x=, y=)
Button(win,text='Go',command=go).place(x=, y=)
win.mainloop()

I have written a small code example in Tkinter, I don't know if that's what you wanted, if it's something different like create the same for a web interface, make me know it and i will provide you some code.
 
hi,

thanks for your reply.

while i was waiting for some guidance, i had already tried my hand at Tkinter.
I did exactly the same as you have now suggested. Although I was able to invoke my application properly, Tkinter would immediately hang! ... This i cannot compromise on.

Let me know if wxPython is better as compared to Tkinter. If yes, some code for the same would help.

The other thing which i was simultaneously working on was usind cgi scripts. but am having problems installing and configuring a server for the same. I am totally lost on which web server to use.

Please guide,
Thanks,
Tarun.
 
Hi,
if you look for some information, a lot of python programmers think wxpython is better than Tkinter, indeed the widgets are more modern and complete in wxpython. you can find it in and if you want an example you can try with this little editor:

from wxPython.wx import *
from sys import argv
import os

#



ID_ABOUT = wxNewId()
ID_OPEN = wxNewId()
ID_SAVE = wxNewId()
ID_SAVEAS = wxNewId()
ID_EXIT = wxNewId()

#

class MyFrame(wxFrame):
def __init__(self, parent, ID, title):

self.__filename__ = NULL

#

wxFrame.__init__(self, parent, ID, title,
wxDefaultPosition,
wxSize(400, 600),
style=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE)


self.CreateStatusBar()
self.SetStatusText("This is the statusbar")

#

FileMenu = wxMenu()

FileMenu.Append(ID_OPEN,
"&Open",
"Open a document")

FileMenu.AppendSeparator()

FileMenu.Append(ID_SAVE,
"&Save",
"Save current document")

FileMenu.Append(ID_SAVEAS,
"Save as",
"Save current document to another file")

FileMenu.AppendSeparator()

FileMenu.Append(ID_EXIT,
"E&xit",
"Terminate the program")

#

HelpMenu = wxMenu()

HelpMenu.Append(ID_ABOUT,
"&About",
"More information about this program")

#

menuBar = wxMenuBar()

menuBar.Append(FileMenu, "&File")
menuBar.Append(HelpMenu,"&Help")

self.SetMenuBar(menuBar)

#

EVT_MENU(self, ID_ABOUT, self.About)
EVT_MENU(self, ID_SAVE, self.Save)
EVT_MENU(self, ID_SAVEAS, self.SaveAs)
EVT_MENU(self, ID_OPEN, self.Open)
EVT_MENU(self, ID_EXIT, self.Quit)

#

self.control = wxTextCtrl(self,
1,
style=wxTE_MULTILINE)

#

if len(argv) > 1:

if os.path.exists(argv[1]):
self.Load(argv[1])
else:
self.Alert("Error",
argv[1] + "is not a valid file")


#

def About(self, event):
self.Alert(ABOUT_TITLE,ABOUT_BODY)

#

def Open(self,event):

filename = wxFileSelector("Choose a file to open")

if filename:
self.Load(filename)

#

def Load(self,filename):

self.__filename__ = filename

self.control.LoadFile(filename)
self.SetStatusText(filename)

#

def Save(self,event):

if self.__filename__:
self.control.SaveFile(self.__filename__)
else:
self.SaveAs(event)

#

def SaveAs(self,event) :

filename = wxFileSelector("Save current document as")

if filename:
self.control.SaveFile(filename)
self.__filename__ = filename
else:
self.Alert("Alert","save as cancelled")

#

def Quit(self, event):

if self.control.IsModified:

answer = wxMessageBox("Document has been modified\n"
"Do you want to save it?",
"Confirm",
wxYES_NO | wxCANCEL);

if (answer == wxYES):
self.Save(event)

if (answer == wxCANCEL):
return false

self.Close(true)

#

def Alert(self,title,msg):
dlg = wxMessageDialog(self, msg,
title,
wxOK | wxICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()

#

class MyApp(wxApp):
def OnInit(self):
frame = MyFrame(NULL, -1, TITLE)
frame.Show(true)
self.SetTopWindow(frame)

return true

#

app = MyApp()
app.MainLoop()


I have also implemented python with the APACHE server, if you are interested, i will guide you in the next post.

PD.- I'm sorry for replaying late, but i'm been quite busy this week.
 
If tkinter is hanging, then you're doing it wrong. A craftsman doesn't blame his tools.
 
Hi,

Sorry for having replied a bit late. Had a deadline to meet at office so I couldnt get back to u. I work on Python only after office hours starting at 10:00pm!

I configured the Apache Web Server as per the instruction at
I started the server and tried running the following python script test.py with test1.html being the page from where i pass 2 parameters to it. The script was not invoked. The browser simply displayed the test.py file!... Please let me know where am i going wrong. Thanks.

test1.html---->>>>

<form action="C:\Python23\Lib\testRayKattar\test.py" method="get">
Enter the site: <input name="param1"
type="text" /><br />
Enter the ply length: <input name="param2"
type="text" /><br />

<input type="reset" />
<input type="submit" />
</form>


test.py--->>>>


import cgi
import sys


try:
import cgitb
cgitb.enable()
except ImportError:
sys.stderr = sys.stdout

def cgiprint(inline=''):
sys.stdout.write(inline)
sys.stdout.write('\r\n')
sys.stdout.flush()


def getform(theform, valuelist, notpresent='', nolist=False):

data = {}
for field in valuelist:
if not theform.has_key(field):
# if the field is not present (or was empty)
data[field] = notpresent
else:
# the field is present
if type(theform[field]) != type([]):
# is it a list or a single item
data[field] = theform[field].value
else:
if not nolist:
# do we want a list ?
data[field] = theform.getlist(field)
else:
data[field] = theform.getfirst(field)
# just fetch the first item
return data

def getall(theform, nolist=False):

data = {}
for field in theform.keys():
# we can't just iterate over it, but must use the keys() method
if type(theform[field]) == type([]):
if not nolist:
data[field] = theform.getlist(field)
else:
data[field] = theform.getfirst(field)
else:
data[field] = theform[field].value
return data

def isblank(indict):

for key in indict.keys():
if indict[key]:
return False
return True


contentheader = 'Content-Type: text/html'

mainpage = '''<html><head><title>Receiving a \
Form</title></head><body>%s</body></html>'''

error = '''<h1>Error</h1><h2>No Form Submission Was Received</h2>'''

result = '''<h1>Receiving a Form Submission - Crawling the web. To see results please go to: /Data</h1>

<p>We received the following parameters from the form :</p>
<ul>
<li>Site Entered is "%s".</li>
<li>Ply length entered is "%s".</li>

</ul>
'''
possible_parameters = ['param1', 'param2']

if __name__ == '__main__':
cgiprint(contentheader) # content header
cgiprint() # finish headers with blank line

theform = cgi.FieldStorage()

formdict = getform(theform, possible_parameters)
if isblank(formdict):
body = error
else:
site = formdict['param1']
ply = formdict['param2'] # should be 'this' or 'that'
body = result % (site, ply, hidden)

print mainpage % body
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top