Tkinter-Dynamic selection on radio button

Hi...
The following GUI that is used to restore backups for a system.

import Tkinter as tk

root = tk.Tk()

v = tk.IntVar()
v.set(1)  # initializing the choice, i.e. Python

backups = [
    (200,"WIN_7", "active", 4096, 320, 1),
    (201,"WIN_7_sp1", "active", 4096, 320, 2),
    (210,"WIN_Java", "inactive", 8019, 8),
    (230,"Linux_C78", "active", 4096, 90),
    (241,"Linux2_B78", "active", 4096, 80)
]

def ShowChoice():
    print(v.get())

tk.Label(root,
         text="""Choose a backup
to restore from the list:""",
         justify = tk.LEFT,
         padx = 20).pack()

for val, backup in enumerate(backups):
    tk.Radiobutton(root,
                  text=backup,
                  padx = 20,
                  variable=v,
                  command=ShowChoice,
                  value=val).pack(anchor=tk.W)


root.mainloop()

Since the back up list is changing from one system to another, the script needs to be changed to list the available backups. If I run the program 'listbk', it outputs the list as follows:

ID      NAME           STATUS      MEM    Disk    RANDOMID
100     WIN_7          active      4096     320        1
301      WIN_7_sp1     active      4096     320        2
390      WIN_Java      inactive    8019     320        8
480      Linux_C78     active      4096     320       90
541      Linux2_B78    active      4096     500       80  


Inside the python script, I am able to run the script using the below command to get the backup list.

from subprocess import call
call(['listbk']).

What is the way to display the available backups for different systems in the GUI.

Thank You for your kind help.
NetRock6Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
What exactly is 'lstbk'?
0
NetRock6Author Commented:
'lstbk displays the available backup jobs in the system.  as you can see in the my question displays the backup jobs.
0
NorieAnalyst Assistant Commented:
Is it a shell command?

I'm asking because there are methods that can be used to process the output from something called using the subprocess module.

For example this puts the output from a call to 'mediainfo' into a list.
import subprocess

   dim = []

   proc = subprocess.Popen('mediainfo --Inform="Video;%Width%x%Height%\\n" /home/saad/Videos/*.*', shell=True, 
               stdout=subprocess.PIPE)

   for line in iter(proc.stdout.readline, ''):
      dim.append(line.rstrip('\n'))

   print dim

Open in new window

Probably not a good example but hopefully it illustrates the idea.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

NetRock6Author Commented:
Thank you for your comment.
Yes, It is a shell command.
As, I said in my question i can run it using:

Inside the same python script, I am able to run the script using the below command to get the backup list.

from subprocess import call
call(['listbk']).

After running 'lstbk', i get the following output: (The options are chnaging from system to system, I might have only one line or 5 or 7 lines).

ID      NAME           STATUS      MEM    Disk    RANDOMID
100     WIN_7          active      4096     320        1
301      WIN_7_sp1     active      4096     320        2
390      WIN_Java      inactive    8019     320        8
480      Linux_C78     active      4096     320       90
541      Linux2_B78    active      4096     500       80  

Then, how can I display each line in a radiobutton to the users to select, so they can restore the backup they want.

Thanks you for your help....
0
NorieAnalyst Assistant Commented:
This is kind of shot in the dark, currently using Windows not been able to test properly.
import subprocess
import Tkinter as tk

root = tk.Tk()

v = tk.IntVar()
v.set(1)  # initializing the choice, i.e. Python

backups = []

proc = subprocess.Popen('listbk', shell=True, 
               stdout=subprocess.PIPE)

for line in iter(proc.stdout.readline, ''):
   backups.append(line.rstrip('\n'))

def ShowChoice():
    print(v.get())

tk.Label(root,
         text="""Choose a backup
to restore from the list:""",
         justify = tk.LEFT,
         padx = 20).pack()

for val, backup in enumerate(backups):
    tk.Radiobutton(root,
                  text=backup,
                  padx = 20,
                  variable=v,
                  command=ShowChoice,
                  value=val).pack(anchor=tk.W)


root.mainloop()

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NetRock6Author Commented:
Thank you so much for your great help.
It works great....
There is only one thing when users select a radiobutton how to store the "ID" number (the first column) into a varaiable, so it can be passed on to the command 'restore' like: restore 301   (to restore the selected backup).


Thank you again.
0
NetRock6Author Commented:
Also, is there a way to display the headings (below line) but not as a radiobutton option:

ID      NAME           STATUS      MEM    Disk    RANDOMID

Thanks.
0
NorieAnalyst Assistant Commented:
So the first row of the output from 'listbk' is a header row?
0
NetRock6Author Commented:
Yes.. It is a header row as it shown in my question.
Thanks
0
NorieAnalyst Assistant Commented:
We can remove the 'header' row from the group of radio buttons using this which would go just before the loop that creates the buttons.
backups.pop(0)

Open in new window

If you wanted to display the header you would need code to add a label or something similar, for example.
tk.Label(root,text = backups[0]).pack(anchor=tk.N)

Open in new window


To return the text of the selected radio button we can change ShowChoice to this,
def ShowChoice():
    print(backups[v.get()])

Open in new window

and you should be able to manipulate the text to get the ID, perhaps something like this.
def ShowChoice():
   id = backups[v.get()].split()[0]
   print(id)

Open in new window

0
NetRock6Author Commented:
Thank you for your great help...
Keep up the good job! ;)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.