maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   Reading From a File in Python (https://talk.maemo.org/showthread.php?t=13271)

Aisu 2007-12-17 02:08

Reading From a File in Python
 
Yo!

Just having a bit of trouble with some code here. Basically, what I'd like to do is read a text file that contain this:

Code:

/usr/home/user/browser.png
And use it as the parameter in this:

Code:

browser = gtk.Button()
image3 = gtk.Image()
browser.connect('clicked', browserf)
image3.set_from_file(browserpic)
image3.show()
browser.add(image3)

Where browserpic is the contents of my text file. Right now I am trying to read the file like this:

Code:

text2 = open('/home/user/iTablet/browserpic1.txt', 'r')
picbrowse = text2.read()
text2.close()
browserpic = picbrowse


But the picture only shows up as an X... I have figured out that I can use a variable for image3.set_from_file() but the data that is outputed from text2.read() will not work in it. (Oh, and any typos are not in the actual code, I just had to re-type all that from looking at another screen)

Help, please?

Thanks :)

pipeline 2007-12-17 03:00

Re: Reading From a File in Python
 
have you tried using print statements and launching from xterm (to see those debug outputs) ?

You could try doing a print browserpic immediately after you set it to see what its using.

Also you might need to do something like this to get rid of trailing \n (linefeed) :
browserpic=picbrowse[0:len(picbrowse)-1]

Mara 2007-12-17 03:04

Re: Reading From a File in Python
 
You reading a picture file? Isn't that binary data, so you should open your file for binary read. ('rb')

But... I may not have understood what you are trying to do... :)

blakboy98 2007-12-17 03:29

Re: Reading From a File in Python
 
All you should need is the line below.


browserpic = open('/home/user/iTablet/browserpic1.txt', 'r').readline().strip()

browser = gtk.Button()
image3 = gtk.Image()
browser.connect('clicked', browserf)
image3.set_from_file(browserpic)
image3.show()
browser.add(image3)

Aisu 2007-12-17 03:52

Re: Reading From a File in Python
 
Quote:

Originally Posted by blakboy98 (Post 109519)
All you should need is the line below.


browserpic = open('/home/user/iTablet/browserpic1.txt', 'r').readline().strip()

browser = gtk.Button()
image3 = gtk.Image()
browser.connect('clicked', browserf)
image3.set_from_file(browserpic)
image3.show()
browser.add(image3)

Thank you! That is EXACTLY what I needed! I'm so stupid! Your help is greatly appreciated, thanks again :)

blakboy98 2007-12-17 04:44

Re: Reading From a File in Python
 
it looks like you are going to have multiple files for each button. you could have all buttons in one file if you do something like.

button.ini file contains:
button1 = /home/user/iTablet/button1.png
button2 = /home/user/iTablet/button2.png
button3 = /home/user/iTablet/button3.png


code:

HTML Code:

HTML Code:

buttonConfig = open('/??/??/buttons.ini').readlines()

for b in buttonConfig:
    if b.find('button1 =') != -1:
        button1 = b.split('=')[1].strip()
    if b.find('button2 =') != -1:
        button2 = b.split('=')[1].strip()
    if b.find('button3 =') != -1:
        button3 = b.split('=')[1].strip()


jethro.itt 2007-12-17 07:57

Re: Reading From a File in Python
 
You know, Python has a built-in module for this, no need to reinvent the wheel:

http://docs.python.org/lib/module-ConfigParser.html


settings.ini:

Code:

[button1]
image=/home/user/browser.png
othersetting=whatever

[button2]
image=/home/user/somethingelse.png

...

Python code:

Code:

import ConfigParser

cfg = ConfigParser.ConfigParser()
cfg.read("settings.ini")

browserpic = cfg.get("button1", "image")

...


blakboy98 2007-12-17 13:18

Re: Reading From a File in Python
 
even better, thx


All times are GMT. The time now is 06:52.

vBulletin® Version 3.8.8