![]() |
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 Code:
browser = gtk.Button() Code:
text2 = open('/home/user/iTablet/browserpic1.txt', 'r') 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 :) |
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] |
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... :) |
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) |
Re: Reading From a File in Python
Quote:
|
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: |
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] Code:
import ConfigParser |
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