miércoles, 22 de junio de 2011

Python, EasyGui and SAP


Original Post: Tasting the mix of Python and SAP - Volume 2

This blog post continues my Python learning adventures, and as I love Web Micro Frameworks, I also love simple graphical frameworks...this time, we're going to use EasyGui, which is wrapper for Tkinter which is also a layer for Tk.

So what's the fuzz about EasyGui? Well...it's an easy way to do GUI's...got it? Easy-Gui...

Anyway, it's not as complete as TKinter but it's for sure easier to learn and easier to develop. Enough talk...let's see the source code...

First, I used Yaml again to hide most of the connection parameters from the user.


ashost: localhost
sysnr: "00"
client: "001"
lang: EN
trace: 1
loglevel: warn

SE16_EasyGui.py

from easygui import *
import sapnwrfc

TITLE = "Python (Easy Gui) & SAP - SE16 Emulator"
conn = ""
table = ""


def Login():
global conn
msg = "Python (Easy Gui) & SAP - SE16 Emulator"
title = "Login"
fieldNames = ["User", "Passwd"]
fieldValues = []
fieldValues = multpasswordbox(msg, title, fieldNames)
user, passwd = fieldValues[0], fieldValues[1]
sapnwrfc.base.config_location = "sap.yml"
sapnwrfc.base.load_config()
conn = sapnwrfc.base.rfc_connect({'user': user, 'passwd': passwd})
ChooseTable()


def ChooseTable():
global table
table = enterbox("Show Table")
if table != " ":
ShowTable()


def ShowTable():
global conn, table
fields = []
fields_name = []
fields_length = []
output = []
header = ""
lines = ""
separator = ""
counter = 0
func_disc = conn.discover("RFC_READ_TABLE")
func = func_disc.create_function_call()
func.QUERY_TABLE(table)
func.DELIMITER("|")
func.invoke()
data_fields = func.DATA.value
data_names = func.FIELDS.value
long_fields = len(func.DATA())
long_names = len(func.FIELDS())

for line in range(0, long_fields):
fields.append(data_fields[line]["WA"].strip())
for line in range(0, long_names):
counter = counter + 1
fields_name.append(data_names[line]["FIELDNAME"].strip())
fields_length.append(data_names[line]["LENGTH"].strip())

for line in range(0, long_names):
field_name = fields_name[line]
field_length = fields_length[line]
if len(field_name) > field_length:
field_length = len(field_name)
field_length = int(field_length) - len(field_name)
spaces = " " * int(field_length)
counter = counter + len(field_name) + len(spaces)
header = header + field_name + spaces + "|"

separator = "-" * counter
output.append(header)
output.append(separator)

for line in range(0, long_fields):
lines = ""
field_length = ""
spaces = ""
data_split = fields[line].split("|")
for line in range(0, long_names):
field_name = fields_name[line]
field_length = fields_length[line]
if len(field_name) > int(field_length):
field_length = len(field_name)
field_length = int(field_length) -
len(data_split[line])
else:
field_length = 0
spaces = " " * int(field_length)
lines = lines + data_split[line] + spaces + "|"
output.append(lines)


codebox("", TITLE, "\n".join(output))

if __name__ == "__main__":
Login()

Now, some images of course -:)





Hope you like this one...see ya next time...

Greetings,

Blag.

2 comentarios:

Steve Oldner dijo...

Muy Bueno!

Still waiting for a PyBlag comic...

EasyGui's creator's webpage and his Thinking in Tkinter tutorial.
http://www.ferg.org/index.html

Tranlated in French, Brazilian, and Itltian.

Alvaro "Blag" Tejada Galindo dijo...

Thanks for the link Steve, I have already downloaded and will read it soon...about the PyBlag comic...need to came with a good idea for it...but maybe you can send me one and become a guest author -;)

Greetings,

Blag.