Python Forum
Building command in a looping construct
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Building command in a looping construct
#1
Hi,
I'm trying to build commands in a loop. However it isn't working. Here's what I have coded:

   for PtrI in range(1,16):
        PNGID = "self.ShowPNG"+str(PtrI)
        FID  = BASEPATH+"/graphics/CD"+str(PtrI)+".png"
        FileID = '"'+FID+'"'                     #embed double quotes in file name string (also tried w/o quotes)
        eval('PNGID+" = Gtk.Button()"')                                              #add Show png(false buttons)
        eval('"self.fixed.put("+PNGID+","+str(MAIN_WINDOW_WIDTH-120)+","+str(MAIN_WINDOW_HEIGHT-120)+")"')  #set false Button location
        eval('"self.image = Gtk.Image.new_from_file("+FileID+")"')           #set up to use a picture 4 button
        eval('PNGID+".set_image(self.image)"')                                       #set image to button
        eval('PNGID+".set_relief(Gtk.ReliefStyle.NONE)"')                            #remove the button border 2 show only png
        eval('PNGID+".set_no_show_all(True)"')                                       #display only when we specifically say to
To display I do:
ShowPtr = 8
eval('"self.ShowPNG"+str(self.ShowPtr)+".show()"')
Both the creation and the final show appear to work (no errors) yet I get nothing.

If I do a manual version such as the below it works:
    self.ShowPNG8 = Gtk.Button()                                              #add ShowPNG8(false buttons)
    self.fixed.put(self.ShowPNG8,1160,680)  #set false Button location
    self.image = Gtk.Image.new_from_file(BASEPATH+"/graphics/CD8.png")           #set up to use a picture 4 button
    self.ShowPNG8.set_image(self.image)                                       #set image to button
    self.ShowPNG8.set_relief(Gtk.ReliefStyle.NONE)                            #remove the button border 2 show only png
    self.ShowPNG8.set_no_show_all(True)                                       #display only when we specifically say to
    self.ShowPNG8.show()


Perhaps eval isn't the correct expression to use?
Reply
#2
OMG, why do you do this? why evals? this is obvious XY problem, tell what you want to do
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Create a data structure (a list or a dict) and assign your images to them. No eval needed and easy to loop over. Don't create a separate variable for each.
Reply
#4
Thanks. I was hung up on how to create the command string when it wasn't needed. I set up a list and that is working great.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the if construct not calculating correctly? egemynet 21 3,900 Mar-21-2022, 01:20 PM
Last Post: deanhystad
  shortening an elif construct Skaperen 10 5,451 Jul-24-2018, 07:06 AM
Last Post: Skaperen
  Best construct? Array, class, other? PappaBear 1 2,980 May-10-2017, 06:02 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020