jeudi 11 février 2021

Use checkboxes to add items to global list in Python

I'm still pretty new to Python so apologies if this isn't very pythonic. I've been working on a small program with some menu screens with checkboxes on. I'd like to have these checkboxes add items to a list when I click the 'Next' page button. This is what I've got so far (I've removed additional windows since I expect it to be similar for all the windows):

import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
import os
import sys


LARGE_FONT = ('Verdana', 12)
NORM_FONT = ('Verdana', 10)
SMALL_FONT = ('Verdana', 8)

global list

list = []

class SysConfigWiz(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        tk.Tk.iconbitmap(self, default="Icon.ico")
        tk.Tk.wm_title(self, "Order Configuration Generator")

        container = tk.Frame(self)
        container.pack(side='top', fill='both', expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)


        self.frames = {}
        for F in (page1, page2, page3, page4):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky='nsew',padx=20, pady=20)
        self.ShowFrame(page1)

    def ShowFrame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

Then I have a few frames with various checkboxes on, similar to this:

class AccessoriesPage(tk.Frame):
    def add_accessories(self, item1_var, item2_var, item3_var):
        item1_choice = item1_var.get()
        item2_choice = item2_var.get()
        item3_choice = item3_var.get()

        if item1_choice == 1:
            list.append('item1')
        else:
            pass
        if item2_choice == 1:
            list.append('item2')
        else:
            pass
        if item3_choice == 1:
            list.append('item3')
        else:
            pass
        controller.ShowFrame(page4)

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = ttk.Label(self, text='Select Items', font='LARGE_FONT')
        label.grid(row=0, column=0, pady=10, padx=10, columnspan=4, sticky='w')

        item1_var = tk.IntVar()
        item2_var = tk.IntVar()
        item3_var = tk.IntVar()

        label = ttk.Label(self, text='Items')
        label.grid(row=1, column=1, columnspan=4, sticky ='w')
        item1_checkbox = ttk.Checkbutton(self, text='item1', variable=item1_var)
        item1_checkbox.grid(row=2, column=1, sticky='w')
        item2_checkbox = ttk.Checkbutton(self, text='item2', variable=item2_var)
        item2_checkbox.grid(row=2, column=2, sticky='w')
        col_pad = ttk.Label(self, text='').grid(row=3, column=0)
        item3_checkbox = ttk.Checkbutton(self, text='item3', variable=item3)
        item3_checkbox.grid(row=4, column=1, sticky='w')
       
        bk = ttk.Button(self, text='< Back',
                        command=lambda: controller.ShowFrame(page2))
        bk.place(relx=0.72, rely=0.92)
        nxt = ttk.Button(self, text='Build System', command=lambda: self.add_items(controller, item1, item2, item3))
        nxt.place(relx=0.85, rely=0.92)

app = SysConfigWiz()
app.geometry('640x480-100-100')
app.mainloop()
print(list)

If I check the boxes and press next it doesn't seem to do anything and when I exit the program it gives an empty list. I used a similar method to this when dealing with some radiobuttons on another page and it seemed to work, hence me using this method.

Any help would be very welcome! Thanks!




Aucun commentaire:

Enregistrer un commentaire