0

My core goal is to build a checkbox dialogue that remembers a user's multiple selections and feeds them to another function after the user clicks "Run". The code I've been trying is below. It's for a very basic app that reads column headers from a file into a tkinter dialogue box and displays the headers with checkboxes. The app itself works but I'm trying to make it more user friendly with these GUI additions. The file can be different every time, so the dialogue box needs to build the checkboxes dynamically.

I can't get the "Run" button to proceed on with the function that uses the items the user checked. Google keeps suggesting to make a class for the dialogue box, but I have really never built classes before and don't understand how to set it up.

Non-class original code that at least generates the dialogue the way that I want.

def display_checkbox_dialog(column_headers):

root = Tk()

i = 1
for x in column_headers:
    Checkbutton(root, text=x, variable=x).grid(row=i, sticky=W)
    i += 1

Button(root, text='Quit', command=root.quit).grid(row=i + 1, sticky=W, pady=4)
Button(root, text='Run', command="run").grid(row=i + 2, sticky=W, pady=4)

mainloop()

The class version is a mess and doesn't generate anything. Latest error is "NameError: name 'column_labels' is not defined"

class MyDialog(object):
def __init__(self, parent):
    self.toplevel = tk.Toplevel(parent)
    self.var = tk.StringVar()
    self.column_labels = column_headers
    label = tk.Label(self.toplevel, text="Pick the desired columns:")
    label.pack(side="top", fill="x")
    om.pack(side="top", fill="x")
    Button(root, text='Quit', command=root.quit).grid(row=len(self.column_labels) + 1, sticky=W, pady=4)
    Button(root, text='Run', command="run").grid(row=len(self.column_labels) + 2, sticky=W, pady=4)


def show(self):
    value = self.var.get()
    return value

def create_check(self):
    i = 1
    for x in self.column_labels:
        Checkbutton(root, text=x, variable=x).grid(row=i, sticky=W)
        i += 1

def on_click(self):
    result = MyDialog(self).show()
    self.label.configure(text="Run" % result)
D K
  • 1
  • please provide a [mre] and how exactly does your `run()` function work? also you don't need to keep a counter when using a `for loop`, simply do `for i, x in enumerate(column_headers):` (also rows start from 0) – Matiiss Aug 25 '21 at 15:12
  • Thanks for the feedback. My run() function should take the columns the user checks in the dialogue and build a new file with it. If I manually feed the columns into the run() it works fine, but I want the dialogue box to supply the column arguments. – D K Aug 25 '21 at 15:44
  • 1
    I don't quite understand what you're asking. You have two different blocks of code. Which one are you asking about? Are you asking about the "Run" button or about the "name not defined" error? – Bryan Oakley Aug 25 '21 at 16:30
  • 2
    The `command` attribute requires a reference to a function. You are passing it a string. – Bryan Oakley Aug 25 '21 at 16:32
  • 2
    Your question mentions a "run" function, but neither block of code includes a function or method named "run". – Bryan Oakley Aug 25 '21 at 16:43

0 Answers0