0

I have the following code where I wanted the values of the selected checkboxes to be displayed. But the output is empty. rxList contains ['CAR1','CAR2','CAR3']

and I want to build checkboxes with these values and return the selected values. I am not getting why it is not reading the selected checkox values

Any suggestions are highly appreciated

def getSelections(rxList):
    window = Tk()
    global selections
    selections =[]
    for i in rxList:
        sel = StringVar()
        selections.append(sel)  
        cb = Checkbutton(window, text = i, variable = sel, onvalue = i, offvalue="None")
        cb.pack()
    btn = Button(window,text = "Next", command = showme)
    # btn1 = Button(window,text= )
    btn.pack()
    print(selections)
    window.mainloop()

def showme():
    for i in range(len(selections)):
        print("---selected values----",selections[i].get())

---EDIT----

I have one GUI already to select a XML file

BELOW IS MY CODE IN main_window.py.....

from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
import path
from ARXML_Parser import importARXML
from pathlib import Path
import os

read_file_path =''
dest_file_path = ''


def select_file():
    global read_file_path
    global dest_file_path
    file_path = ""
    file_path = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("ARXML files","*.arxml"),))
    name = Path(file_path).stem #return the file name
    f_name = os.path.basename(file_path)

    nn = name + '.h'
    if file_path!="":
        file_name.configure(state = 'normal')
        file_name.delete('1.0',END)
        file_name.insert('1.0', file_path)
        file_name.configure(state = 'disabled')

        d_f_name = re.sub(f_name,nn,file_path) # from file path replaces XXXX.arxml with XXXX.h
        dest_file_name.configure(state = 'normal')
        dest_file_name.delete('1.0',END)
        dest_file_name.insert(END, d_f_name)
        dest_file_name.configure(state = 'disabled')
        read_file_path = file_path.strip()
        dest_file_path = dest_file_name.get('1.0',END).strip()

def select_folder():
    global dest_file_path
    chk = file_name.get('1.0',END).strip()
    print(f'chk---{chk}++')
    print(type(chk))
    if chk.strip() != '':
        folder_path = ""
        folder_path = filedialog.askdirectory()
        f_path = file_name.get('1.0',END)
        f_name = Path(f_path).stem + '.h'

        if folder_path!="":
            folder_path = folder_path + '/' + f_name
            dest_file_name.configure(state = 'normal')
            dest_file_name.delete('1.0',END)
            dest_file_name.insert(END, folder_path)
            dest_file_path = folder_path.strip()
            dest_file_name.configure(state = 'disabled')

    else:
        messagebox.showerror("Error", "You should select a file first.")



def call_func():
    if read_file_path =='':
        messagebox.showerror('Error','You need to select a file first before hitting convert button !')
    else:
        ret = importARXML(read_file_path,dest_file_path)
        if ret ==1:
            messagebox.showinfo('Message', 'The file got converted !')
            # root.destroy()
        else:
            messagebox.showinfo('Error', 'There seems to be error with file !')
            root.destroy()


def createCheckBox(rxList):
    var = StringVar()
    for i in rxList:
       c = Checkbutton(root, text = i, variable =  var)
       c.pack()
    return 1


root = Tk()
# root.resizable(False,False)x

root.title("ARXML Code Gen")
# root.geometry('2000x1000')



frame =  LabelFrame(root)
frame_Checkbuttons = LabelFrame(root)

scroll = Scrollbar(frame_Checkbuttons)
scroll.pack(side = RIGHT, fill = Y)  


dummy_text = Label(frame,text = "   ")
dummy_text1 = Label(frame,text = "\n ")

dummy_text1.grid(row=0, column = 1)
dummy_text1.grid(row=1, column = 2)
dummy_text.grid(row=2, column = 3)

select_file_button = Button(frame,text = "Select ARXML file", command = select_file)
select_file_button.grid(row =4, column=0,padx =25,pady=25)

file_name = Text(frame,height=2, width = 90)
file_name.grid(row = 4, column = 3,padx =10,pady=25)


dummy_text1.grid(row=5, column = 0)

dest_folder_button = Button(frame,text = "Destination Folder", command = select_folder)
dest_folder_button.grid(row = 6, column = 0, padx = 25,pady=10)



dest_file_name = Text(frame,height=2, width = 90)
dest_file_name.grid(row = 6, column = 3,padx =10,pady=10)


dummy_text.grid(row =7, column = 0)

submit_button =  Button(frame,text = 'Convert ARXML to .h ',width =20,height = 1,command = call_func)
submit_button.grid(row = 10, column = 3,sticky=N, columnspan=2,pady=10)
lumn = 0)


frame.pack(padx=20,pady=20)
frame_Checkbuttons.pack(padx=20,pady=20)

root.mainloop()

The below code is my parser code in ARXML_Parser.py....


from pathlib import Path
from tkinter import *
from xml.dom import minidom
from time import sleep
from temp import *

from tkinter import messagebox

def importARXML(src_path,dest_path):
    ARXML = minidom.parse(src_path.strip())
    dest_path = dest_path.strip()
    print(src_path.strip())

    ********** File is Parsed from source path ************

    ***************     some       ********************\
    *************   functionality       ******************
    **************      to get Rx   **********************8

    # so here is my Rx
    window = Tk()
    app = MainApplication(window,Rx)

And your code which you sent me is stored in a file called temp.py.

is the problem because we have two mainloops ??

  • Calling the mainloop should happen at the end of your program, otherwise everything after that is not happening. Check out [this answer](https://stackoverflow.com/a/29158947/11033290) for more info. – char Apr 17 '20 at 07:13
  • @char I tried that also even then I am not able to see the values, – Basavaprabhu Sherikar Apr 17 '20 at 09:19

1 Answers1

0

Try something like this

# Use Tkinter for python 2, tkinter for python 3
import tkinter as tk

class MainApplication:
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)

        self.label = tk.Label(self.frame, text="Some selections")
        self.label.pack()

        self.getSelections(['CAR1','CAR2','CAR3','CAR4'])

        self.close_button = tk.Button(self.frame, text="Close", command=master.quit)
        self.close_button.pack()

        self.frame.pack()

    def getSelections(self,rxList):
        global selections
        selections =[]
        for i in rxList:
            sel = tk.StringVar()
            selections.append(sel)
            cb = tk.Checkbutton(self.frame, text = i, variable = sel, onvalue = i, offvalue="None")
            cb.pack()
        btn = tk.Button(self.frame,text = "Next", command = self.showme)
        # btn1 = tk.Button(self.frame,text= )
        btn.pack()
        print(selections)

    def showme(self):
        for i in range(len(selections)):
            print("---selected values----",selections[i].get())


if __name__ == "__main__":
    root = tk.Tk()
    app = MainApplication(root)
    root.mainloop()

In this case the mainloop() is run as the last command, and your selections are filled in and buttons are "executed" beforehand.

Output: Running python3 temp.py gives me first only

[<tkinter.StringVar object at 0x109df8950>, <tkinter.StringVar object at 0x109e0be10>, <tkinter.StringVar object at 0x109e41ad0>, <tkinter.StringVar object at 0x109e41bd0>]

Terminal waits and at the same time you should see the tkinter window:

enter image description here

If I select the first two checkmarks, press next, I get the following output

---selected values---- CAR1
---selected values---- CAR2
---selected values----
---selected values----

Edit: If you're having trouble seeing the button text, and are on Mac in dark mode, that might be this issue.

char
  • 2,063
  • 3
  • 15
  • 26