如何在弹出菜单中设置/取消设置复选框。

huangapple go评论66阅读模式
英文:

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__ == &quot;__main__&quot;:
  root = tk.Tk()
  root.geometry(&#39;1024x768&#39;)

  show_checkmark = tk.IntVar(root, value=True)
  popup = tk.Menu(root, tearoff=0)
  popup.add_command(label=&#39;Show All&#39;, command=show_all)
  popup.add_command(label=&#39;Hide All&#39;, command=hide_all)
  popup.add_checkbutton(label=&#39;Expand On Change&#39;, command=toggle_expand_on_change, variable=show_checkmark)
  
  label_frame = tk.LabelFrame(root)
  label_frame.pack(expand=&#39;yes&#39;, fill=&#39;both&#39;)
  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=&#39;Bold&#39;)
      flag_set = BoolSet(label, r, c)
      label.grid(row=r, column=c)
      label.bind(&quot;&lt;Button-3&gt;&quot;, 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)

huangapple
  • 本文由 发表于 2023年6月1日 05:13:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377346.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定