0

I'm trying to create a simple .bin editor, but I can into a problem very early on. I can't figure out how to get the value of each checkbutton. As you see, I am trying to keep the code as condensed as possible by using a class to create all the checkbuttons.

Right now all I am trying to do is get the "saveSave" function to print out if the box is checked or not so that I can later use it in an if...else statement.

try: #Python 2 imports
    from Tkinter import *
    import ttk
    import tkFileDialog as filedialog

except ImportError: #Python 3 imports
    from tkinter import *
    from tkinter import ttk
    from tkinter import filedialog

def openSave():
    file = filedialog.askopenfilename()

def saveSave():
    print MaxMP.get()


class CB(Frame):
   def __init__(self, parent=None, cheater=""):
      Frame.__init__(self, parent)
      self.var = BooleanVar()
      rawr = Checkbutton(self, text=cheater, variable=self.var, onvalue=1, offvalue=0, bg="white", command=self.saveSave)
      rawr.pack()
      self.grid(sticky='w', columnspan=2)

pmdxse = Tk()
pmdxse.title("Project Mirai DX Save Editor")
pmdxse.configure(bg="white", padx=10, pady=10)
pmdxse.resizable(width=False, height=False)

Button(pmdxse, text="Open", command=openSave).grid(row=0, column=0)
Button(pmdxse, text="Save", command=saveSave).grid(row=0, column=1)
MaxMP = CB(pmdxse, 'Max MP')
MaxSnacks = CB(pmdxse, 'Max Snacks')
MaxHighscore = CB(pmdxse, 'Max Highscore')
MaxPerfentage = CB(pmdxse, 'Max Percentage')
MaxCombo = CB(pmdxse, 'Max Combo')
UnlockSongs = CB(pmdxse, 'Unlock all Songs')
UnlockHard = CB(pmdxse, 'Unlock Hard/Super Hard Modes')
UnlockItems = CB(pmdxse, 'Unlock all Items in the Shop')
UnlockOutfits = CB(pmdxse, 'Unlock all Outfits')
UnlockStamps = CB(pmdxse, 'Unlock all 115 Stamps')
UnlockProfileOptions = CB(pmdxse, 'Unlock all Profile Options')
MaxRank = CB(pmdxse, 'Set each song to \"Prefect\" rank')
BuyItems = CB(pmdxse, 'Buy all Items')
BuyOutfits = CB(pmdxse, 'Buy all Outfits')


pmdxse.mainloop()
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
user3251025
  • 21
  • 1
  • 4
  • Use **`trace`**. See [the documentation](http://effbot.org/tkinterbook/variable.htm) – Peter Wood Jul 22 '17 at 20:26
  • 2
    Possible duplicate of [How do I get an event callback when a Tkinter Entry widget is modified?](https://stackoverflow.com/questions/6548837/how-do-i-get-an-event-callback-when-a-tkinter-entry-widget-is-modified) – Peter Wood Jul 22 '17 at 20:27

1 Answers1

0

Every instance of CB has an attribute var, so you can simply get the value of that variable:

def saveSave():
    print MaxMP.var.get()

Another solution is to add a get method to your CB class, to hide the implementation details from the caller:

class CB(...):
    def get(self):
        return self.var.get()

def saveSave():
    print MaxMP.get()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Awesome, thanks! I figured it was something really simple. I am literally learning how to do this all today from reading online. – user3251025 Jul 22 '17 at 22:45