Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

adding GUI to a python script

Posted on 2011-02-21
6
Medium Priority
?
503 Views
Last Modified: 2012-05-11
Hi experts,
I wrote a python script that does exactly what I want it to do:
Extract a specific string(s) from a file and copy/write them to a file output.qry.txt.
I would like your assistance or guidance in doing the following modification to the code (code should run on Win XP machine):
I would like to have simple GUI that will request the following items:
1.      File input (for the fileinput)
2.      File output (currenty its always output.qry.txt).

I would appreciate any suggestion or direction,
Thanks in advance.
0
Comment
Question by:WAS_Infra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 29

Expert Comment

by:pepr
ID: 34942386
Do you mean the real GUI (i.e. graphical) or only a kind of prompt in the console?

Use the raw_input() command for the later (http://docs.python.org/library/functions.html#raw_input).

If you mean GUI, you have to decide whether you want to use the included Tcl/Tk based module Tkinter (http://docs.python.org/library/tkinter.html) or some more modern like wxPython or wxQt.
0
 

Author Comment

by:WAS_Infra
ID: 34942399
Thanks Pepr for the prompt reply.
I am talking about the real GUI (window and buttons) what is the easiest way to achieve that?
I am attaching my original script below

Thanks in advance,

import fileinput

def findStart(line):
	start = line.find("[part ")
	return start

def getCurrentAndTotal(item):
	myStrList = item[item.find("["):item.find("]")].split(" ")
	#print(myStrList)
	total = int(myStrList[-1])
	current = int(myStrList[1])
	return [current, total]
	
qry = ""
lst = list()
times = list()
results = list()
myLen = len("Performing query: ")

for line in fileinput.input():
	n = findStart(line)
	if (n != -1): 
		lst.append(line[n:])
		times.append(line[:22])
listLen = len(lst)
for index in range(listLen):
	total,current = getCurrentAndTotal(lst[index])
	if (total == current):
		lstStr = lst[index]
		lstStr = lstStr[lstStr.find("Performing query:")+myLen:len(lstStr)-1]
		qry = qry + lstStr
		qry = times[index] + "\n" + qry
		results.append(qry)
		qry = ""
	else:
		lstStr = lst[index]
		lstStr = lstStr[lstStr.find("Performing query:")+myLen:len(lstStr)-1]
		qry = qry + lstStr
		
f = open("output.qry.txt", "w+")
for item in results:
	f.write(item)
	f.write("\n\n")

Open in new window


0
 
LVL 29

Accepted Solution

by:
pepr earned 2000 total points
ID: 34942434
Here the raw input case:

inputFilename  = raw_input('Input filename : ')
outputFilename = raw_input('Output filename: ')

print
print 'The selected file names'
print 'Input :', inputFilename
print 'Output:', outputFilename

# You can check whether the input filename exists. The import command 
# is usually put to the top of the source file.
#
import os
if os.path.isfile(inputFilename):
    print 'Input file found.'
else:
    print 'Input file missing.'

Open in new window


It prints the following on the screen:

c:\tmp\___python\WAS_Infra\Q_26835902>python a.py
Input filename : fsdfdsfsdf
Output filename: sdfsfsdfsdfs

The selected file names
Input : fsdfdsfsdf
Output: sdfsfsdfsdfs
Input file missing.

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

Expert Comment

by:pepr
ID: 34942491
For working with the real GUI, you must learn some windowing basics.  The GUI applications are built a bit differently than console applications.  To make it short, this will require more work than the raw_input().

I personally do not use Tkinter, but the following is probably a good tutorial http://www.pythonware.com/library/tkinter/introduction/

I personally do use wxPython (http://www.wxpython.org/).  If you know wxWidgets (http://wxwidgets.org/), it may be your choice.

If you know and/or prefer Qt, then you should use the wxQt.

This may be the decision for a longer time. You should ask for details if you do not know.

0
 
LVL 6

Expert Comment

by:LeeeRussell
ID: 34942528
If you choose to use wxPython, you might want to have a look at wxFormBuilder.

http://sourceforge.net/apps/wordpress/wxformbuilder/

This will generate Python GUI code for you from a graphical design that you create.
0
 
LVL 6

Expert Comment

by:LeeeRussell
ID: 34942565
Or you could use pepr's code to create a console application in Python and then use AutoIt to create a GUI.

http://www.autoitscript.com/site/autoit/

This method may be slightly easier as I think AutoIT is easier to generate a GUI in.  Also, AutoIt can run any script from a GUI, so might be useful for you in the future.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to dynamically set the form action using jQuery.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question