英文:
How to set/unset checkbutton in popup menu
问题
I'm banging around on a Python Tkinter gui. It's basically a bunch of text grouped into multiple columns. I have a popup menu that is displayed when the user right clicks any label in the top row. My popup menu has 3 options: Show All, Hide All, Expand On Change. The Expand On Change is a checkbutton. Here's my problem I can't get the set/unset the checkbutton on a column-by-column basis.
I've tied adding a variable when adding the checkbutton to the menu, and then setting that variable in the menu_popup function based on a boolean
show_checkmark = None
def menu_popup(event, fset):
global current_flag_set
global show_checkmark
current_flag_set = fset
show_checkmark = current_flag_set.expand_on_change
try:
popup.tk_popup(event.x_root, event.y_root)
finally:
popup.grab_release()
if __name__ == "__main__":
root = tk.Tk()
root.geometry('1024x768')
show_checkmark = tk.IntVar(root, value=True)
popup = tk.Menu(root, tearoff=0)
popup.add_command(label='Show All', command=show_all)
popup.add_command(label='Hide All', command=hide_all)
popup.add_checkbutton(label='Expand On Change', command=toggle_expand_on_change, variable=show_checkmark)
label_frame = tk.LabelFrame(root)
label_frame.pack(expand='yes', fill='both')
label_frame.bind(popup)
# code that pulls the column headers from a file
# loop to get column headers
label = tk.Label(label_frame, text=t.capitalize(), font='Bold')
flag_set = BoolSet(label, r, c)
label.grid(row=r, column=c)
label.bind("<Button-3>", lambda event, fset=flag_set: menu_popup(event, fset))
(Note: This code is provided as requested without translation.)
英文:
I'm banging around on a Python Tkinter gui. It's basically a bunch of text grouped into multiple columns. I have a popup menu that is displayed when the user right clicks any label in the top row. My popup menu has 3 options: Show All, Hide All, Expand On Change. The Expand On Change is a checkbutton. Here's my problem I can't get the set/unset the checkbutton on a column-by-column basis.
I've tied adding a variable when adding the checkbutton to the menu, and then setting that variable in the menu_popup function based on a boolean
show_checkmark = None
def menu_popup(event, fset):
global current_flag_set
global show_checkmark
current_flag_set = fset
show_checkmark = current_flag_set.expand_on_change
try:
popup.tk_popup(event.x_root, event.y_root)
finally:
popup.grab_release()
if __name__ == "__main__":
root = tk.Tk()
root.geometry('1024x768')
show_checkmark = tk.IntVar(root, value=True)
popup = tk.Menu(root, tearoff=0)
popup.add_command(label='Show All', command=show_all)
popup.add_command(label='Hide All', command=hide_all)
popup.add_checkbutton(label='Expand On Change', command=toggle_expand_on_change, variable=show_checkmark)
label_frame = tk.LabelFrame(root)
label_frame.pack(expand='yes', fill='both')
label_frame.bind(popup)
# code that pulls the column headers from a file
# loop to get column headers
label = tk.Label(label_frame, text=t.capitalize(), font='Bold')
flag_set = BoolSet(label, r, c)
label.grid(row=r, column=c)
label.bind("<Button-3>", lambda event, fset=flag_set: menu_popup(event, fset))
答案1
得分: 0
current_flag_set.expand_on_change
是一个布尔值。
应该像这样使用 set
方法:
show_checkmark.set(current_flag_set.expand_on_change)
英文:
I suppose that current_flag_set.expand_on_change
is a boolean.
<br>You should probably use set
methode like this :
show_checkmark.set(current_flag_set.expand_on_change)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论