Python:Get checkbutton值

2024-10-01 19:25:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个简单的.bin编辑器,但我可以很早就陷入问题。我不知道如何得到每个checkbutton的值。如您所见,我试图通过使用一个类来创建所有的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()

Tags: textfromimportselftkinterdefallmax
1条回答
网友
1楼 · 发布于 2024-10-01 19:25:58

CB的每个实例都有一个属性var,因此您可以简单地获得该变量的值:

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

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

^{2}$

相关问题 更多 >

    热门问题