• Status: Solved
  • Priority: High
  • Security: Private
  • Views: 22
  • Last Modified:

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.
0
NetRock6
Asked:
NetRock6
  • 6
  • 5
1 Solution
 
NorieVBA ExpertCommented:
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
 
NorieVBA ExpertCommented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
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
 
NorieVBA ExpertCommented:
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
 
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
 
NorieVBA ExpertCommented:
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
 
NorieVBA ExpertCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now