Reply
Thread Tools
Posts: 662 | Thanked: 238 times | Joined on Jul 2007
#1
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's Avatar
Posts: 693 | Thanked: 502 times | Joined on Jul 2007
#2
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's Avatar
Posts: 1,310 | Thanked: 820 times | Joined on Mar 2006 @ Irving, TX
#3
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's Avatar
Posts: 69 | Thanked: 3 times | Joined on May 2007 @ St.Petersburg, FL
#4
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)
__________________
Your soul brotha from another motha!
 

The Following User Says Thank You to blakboy98 For This Useful Post:
Posts: 662 | Thanked: 238 times | Joined on Jul 2007
#5
Originally Posted by blakboy98 View Post
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's Avatar
Posts: 69 | Thanked: 3 times | Joined on May 2007 @ St.Petersburg, FL
#6
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()
__________________
Your soul brotha from another motha!
 
Guest | Posts: n/a | Thanked: 0 times | Joined on
#7
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")

...
 

The Following User Says Thank You to For This Useful Post:
blakboy98's Avatar
Posts: 69 | Thanked: 3 times | Joined on May 2007 @ St.Petersburg, FL
#8
even better, thx
__________________
Your soul brotha from another motha!
 
Reply


 
Forum Jump


All times are GMT. The time now is 12:08.