3 Replies - 690 Views - Last Post: 11 July 2013 - 07:33 AM Rate Topic: -----

#1 DeviantYoda  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 08-July 13

Dynamic amount of buttons

Posted 08 July 2013 - 08:18 PM

Not sure if wxPython is covered here, but assuming it would be, I'm having some problems (as in, I have no idea) how to/if you can create a dynamic amount of buttons per run. The amount of buttons wont need to change during the programs running. The amount of buttons will be decided upon by a .txt file, with each line being a separate button. It doesnt really matter if this is possible, it will be a personal program which I can manually change if needed, this would just be helpful.
Is This A Good Question/Topic? 0
  • +

Replies To: Dynamic amount of buttons

#2 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: Dynamic amount of buttons

Posted 08 July 2013 - 11:11 PM

View PostDeviantYoda, on 09 July 2013 - 03:18 AM, said:

Not sure if wxPython is covered here, but assuming it would be, I'm having some problems (as in, I have no idea) how to/if you can create a dynamic amount of buttons per run. The amount of buttons wont need to change during the programs running. The amount of buttons will be decided upon by a .txt file, with each line being a separate button. It doesnt really matter if this is possible, it will be a personal program which I can manually change if needed, this would just be helpful.
Well honestly I don't know wx python, but what you want is definitely possible.

First I made this test file called "testing.txt":
this
that
something
nothing
aardvark
spam
eggs
cheese


And saved that and the following code in the same directory:
import wx

SIZE = (100,500)

class MultiButtons(wx.Dialog):
    def __init__(self, parent, id, title,input_file):
        wx.Dialog.__init__(self, parent, id, title, size=SIZE)

        wx.Button(self,1, 'Close', (10,10))
        with open(input_file) as myfile:
            for i,name in enumerate(myfile):
                name = name.strip()
                wx.Button(self,i+2,name,(10, 50+30*i))

        self.Bind(wx.EVT_BUTTON, self.close, id=1)
        self.Centre()
        self.ShowModal()
        self.Destroy()

    def close(self, event):
        self.Close(True)

if __name__ == "__main__":
    app = wx.App(0)
    MultiButtons(None, -1, 'buttons.py',"testing.txt")
    app.MainLoop()

Output:
Spoiler


-Mek
Was This Post Helpful? 0
  • +
  • -

#3 DeviantYoda  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 08-July 13

Re: Dynamic amount of buttons

Posted 09 July 2013 - 05:19 AM

Okay, that does just what i need, thank you very much. But would it be possible to also assign each button a separate event, which would just be opening some .exes (this is being made because I'm lazy). Maybe having the file on the same line as the name it goes with, or in a separate .txt?
Was This Post Helpful? 0
  • +
  • -

#4 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: Dynamic amount of buttons

Posted 11 July 2013 - 07:33 AM

View PostDeviantYoda, on 09 July 2013 - 12:19 PM, said:

Okay, that does just what i need, thank you very much. But would it be possible to also assign each button a separate event, which would just be opening some .exes (this is being made because I'm lazy). Maybe having the file on the same line as the name it goes with, or in a separate .txt?
Of course it is possible. You would similarly need to extract the paths of these executables from your data file. Then bind the buttons to a function that would run the files (presumably using subprocess.Popen or subprocess.call).

You should take a shot at doing this yourself before seeking further assistance.
-Mek
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1