dimanche 19 février 2017

Python 3 Tkinter checkbutton variable not functioning properly

I've started writing a program that will update a checkbutton's value that may appear in different windows, but it currently returns an error message. The code below isn't finished - there are two separate windows because I will be adding more controls in afterwards.

Below is part of my code:

# import tkinter module
from tkinter import *
from tkinter import ttk


# create blank variable names and controls
checke9 = None      # variable to check value of checkbox
e9 = None           # checkbox control


# define function to clear all fields
def clear_data() :

    checke9.set(0)



# define function to open customer window
def customer_window() :

    # assign global variables
    global callroot
    global checke9
    global e9

    # close previous (root) window
    callroot.destroy()


    # set customer info
    customer = Tk()

    # create extra choices
    Label(customer, text="Extras: ", anchor="e", font="Corbel 12 italic", width="12", bg="#c8ebf7").grid(row=9)
    e9 = Checkbutton(customer, text="Test", onvalue=1, offvalue=0, variable=checke9, font="Corbel 12", bg="#c8ebf7")
    e9.grid(row=9, column=1, sticky=W)

    # create buttons
    Button(customer, text="Clear", bg="darkcyan", fg="white", font="Corbel 12", width="5", command=clear_data).grid(row=10, column=1, sticky=W)

    customer.mainloop()




# define function to open staff window
def staff_window() :

    # assign global variables
    global callroot
    global callstaff
    global checke9
    global e9

    # assign variable for the extras checkbox
    checke9 = IntVar()  
    checke9.set(0)

    # close previous (root) window
    callroot.destroy()


    # set staff info
    staff = Tk()
    callstaff = staff

    # create checkbox
    Label(staff, text="Extras: ", anchor="e", font="Corbel 12 italic", width="12", bg="#c8ebf7").grid(row=9)
    e9 = Checkbutton(staff, text="Another checkbox", variable=checke9, onvalue=1, offvalue=0, font="Corbel 12", bg="#c8ebf7")
    e9.grid(row=9, column=1, sticky=W)

    Button(staff, text="Clear", bg="darkcyan", fg="white", font="Corbel 12", width="5", command=clear_data).grid(row=10, column=1, sticky=W)

    staff.mainloop()




# define function to open the main (root) window
def root_window() :

    # set root info
    root = Tk()

    global callroot
    callroot = root

    # create buttons
    Button(root, text="Customer Window", font="Corbel 14 bold", width=20, command=customer_window).grid(row=1, column=0, pady=30)
    Button(root, text="Staff Window", font="Corbel 10", width=15, command=staff_window).grid(row=2, column=0, pady=10)
    Button(root, text="Close", font="Corbel 8", width=10, command=root.destroy).grid(row=3, column=0, pady=5)
    root.mainloop()



# start program
root_window()

Can you help? Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire