Reputation: 1
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)
Upvotes: 0
Views: 36