python - 当我想获取复选框的状态时,为什么要获取地址而不是值 - tk.IntVar
问题描述
我想通过单击“保存”按钮来获取复选框的状态,然后如果复选框 1(逐行)的状态是,则将键和值附加到新字典中。我已经尝试使用“.get”函数调用字典,但它不起作用。知道如何解决这个问题吗?
import tkinter as tk
from tkinter import ttk
from tkinter import *
class ToolTip(object):
def __init__(self, widget):
self.widget = widget
self.tip_window = None
def show_tip(self, tip_text):
"Display text in a tooltip window"
if self.tip_window or not tip_text:
return
x, y, _cx, cy = self.widget.bbox("insert") # get size of widget
x = x + self.widget.winfo_rootx() + 25 # calculate to display tooltip
y = y + cy + self.widget.winfo_rooty() + 25 # below and to the right
self.tip_window = tw = tk.Toplevel(self.widget) # create new tooltip window
tw.wm_overrideredirect(True) # remove all Window Manager (wm) decorations
# tw.wm_overrideredirect(False) # uncomment to see the effect
tw.wm_geometry("+%d+%d" % (x, y)) # create window size
label = tk.Label(tw, text=tip_text, justify=tk.LEFT,
background="#ffffe0", relief=tk.SOLID, borderwidth=1,
font=("tahoma", "8", "normal"))
label.pack(ipadx=1)
def hide_tip(self):
tw = self.tip_window
self.tip_window = None
if tw:
tw.destroy()
def create_ToolTip(widget, text):
toolTip = ToolTip(widget) # create instance of class
def enter(event):
toolTip.show_tip(text)
def leave(event):
toolTip.hide_tip()
widget.bind('<Enter>', enter) # bind mouse events
widget.bind('<Leave>', leave)
class OOP(tk.Frame):
def __init__(self, parent): # Initializer method
tk.Frame.__init__(self, parent)
self.canvas = tk.Canvas(self, borderwidth=0, background="#ffffff")
self.frame = tk.Frame(self.canvas, background="#ffffff")
self.vsb = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview)
self.canvas.configure(yscrollcommand=self.vsb.set)
self.hsb = tk.Scrollbar(self, orient="horizontal", command=self.canvas.xview)
self.canvas.configure(xscrollcommand=self.hsb.set)
self.vsb.pack(side="right", fill="y")
self.hsb.pack(side="bottom", fill="x")
self.canvas.pack(side="left", fill="both", expand=True)
self.canvas_window = self.canvas.create_window((4,4), window=self.frame, anchor="nw", tags="self.frame")
self.frame.bind("<Configure>", self.onFrameConfigure)#Bind an event whenever the size of the frame changes
self.canvas.bind("<Configure>", self.onFrameConfigure)#Enables scrolling
self.colors = {"f1_Blue": 0, "f2_Red": 0, "f3_Green": 0, "f4_Black": 0, "f5_White": 0, "f6_Purple": 0}
self.tabControl = ttk.Notebook(self.frame, width=2500, height=1100) # Create Tab Control
self.tab_menu = ttk.Frame(self.tabControl) # Create a tab
self.tab_menu.pack(fill=BOTH, expand=1)
self.tabControl.add(self.tab_menu, text="MENU") # Add a MENU tab
self.tabControl.pack(expand=1, fill="both") # Pack to make visible
self.label1 = tk.Label(self.tab_menu, text=' Status ', fg='white', bg='black')
self.mighty = ttk.LabelFrame(self.tab_menu, labelwidget=self.label1)
self.mighty.grid(column=0, row=0, padx=20, pady=10)
self.comp_add_drop_status = self.check_box_addordrop(1, 0, self.mighty, self.colors)
ttk.Label(self.mighty, text="Save the colors?", foreground="blue").grid(column=0, row=15, sticky=tk.W, columnspan=3)
self.action = ttk.Button(self.mighty, text="Save", command=self.save_colors2dict)
self.action.grid(column=5, row=15, sticky=tk.E, columnspan=3)
# Add a title
win.title("TESTER")
win.resizable(1, 1)
def onFrameConfigure(self, event):
self.canvas.configure(scrollregion=self.canvas.bbox("all")) #Reset the scroll region to encompass the inner frame
def onCanvasConfigure(self, event):
canvas_width = event.width
self.canvas.itemconfig(self.canvas_window, width=canvas_width) #Whenever the size of the canvas changes, alter the the window region respectively
def check_box_addordrop(self, r, c, frame, items):
cow = c
row = r
self.my_list_test = []
for item in items:
items[item] = tk.IntVar(value=items[item])
checkbox = tk.Checkbutton(frame, text=item[3:15], variable=items[item])
checkbox.grid(column=cow, row=row, sticky=tk.W)
row += 1
return items
def save_colors2dict(self):
#print(list(self.comp_add_drop_basisBN.state()))
print(self.comp_add_drop_status.values())
print(self.comp_add_drop_status.keys())
print(self.comp_add_drop_status.items())
print(self.comp_add_drop_status.get("f1_Blue"))
print(self.comp_add_drop_status.get("f2_Red"))
print(self.comp_add_drop_status.get("f3_Green"))
if __name__ == '__main__':
win = tk.Tk()
example = OOP(win)
example.pack(fill="both", expand=True)
win.mainloop()
解决方案
推荐阅读
- apache-camel - Camel - 捕获 SQL 异常并尝试 3 次
- regex - 正则表达式将字符串拆分为浮点数/数字和字符串
- reactjs - 在行悬停时显示蚂蚁设计弹出框
- android - http包中的respnse.body没有返回所需的内容
- python - 有没有办法阻止自动下载组织者重新定位下载文件?
- sql - 如何从sql中的每个组中找到最小值?
- javascript - Firebase v9 Firestore 在 React JS 中按 id 创建文档
- python - 来自守护进程的错误响应:OCI 运行时创建失败:container_linux.go:380:启动容器进程导致:exec:“python”:
- delphi - 是否可以在 Delphi 中声明具有通用值类型的 TDictionary?
- filter - WP过滤器/动作