Python:获取checkbutton值

时间:2017-07-22 20:17:28

标签: python tkinter

我试图创建一个简单的.bin编辑器,但我很早就可以解决问题了。我无法弄清楚如何获取每个checkbutton的值。如您所见,我试图通过使用类来创建所有的检查按钮来尽可能地浓缩代码。

现在我所要做的就是获得" saveSave"如果选中该框,则打印出来的功能,以便我以后可以在if ... else语句中使用它。

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()

1 个答案:

答案 0 :(得分:0)

CB的每个实例都有一个属性var,因此您只需获取该变量的值:

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

另一种解决方案是向get类添加CB方法,以隐藏调用方的实现细节:

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

def saveSave():
    print MaxMP.get()