Menu

Main Menu
Talk Get Daily Search

Member's Online

    User Name
    Password

    Reading From a File in Python

    Reply
    Aisu | # 1 | 2007-12-17, 02:08 | Report

    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

    Edit | Forward | Quote | Quick Reply | Thanks

     
    pipeline | # 2 | 2007-12-17, 03:00 | Report

    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]

    Edit | Forward | Quote | Quick Reply | Thanks

     
    Mara | # 3 | 2007-12-17, 03:04 | Report

    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...

    Edit | Forward | Quote | Quick Reply | Thanks

     
    blakboy98 | # 4 | 2007-12-17, 03:29 | Report

    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)

    Edit | Forward | Quote | Quick Reply | Thanks
    The Following User Says Thank You to blakboy98 For This Useful Post:
    Aisu

     
    Aisu | # 5 | 2007-12-17, 03:52 | Report

    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

    Edit | Forward | Quote | Quick Reply | Thanks

     
    blakboy98 | # 6 | 2007-12-17, 04:44 | Report

    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()

    Edit | Forward | Quote | Quick Reply | Thanks

     
    jethro.itt | # 7 | 2007-12-17, 07:57 | Report

    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")
    
    ...

    Edit | Forward | Quote | Quick Reply | Thanks
    The Following User Says Thank You to For This Useful Post:
    blakboy98

     
    blakboy98 | # 8 | 2007-12-17, 13:18 | Report

    even better, thx

    Edit | Forward | Quote | Quick Reply | Thanks

     
vBulletin® Version 3.8.8
Normal Logout