【问题标题】:Check OptionMenu selection and update GUI检查 OptionMenu 选择和更新 GUI
【发布时间】:2014-06-27 13:11:36
【问题描述】:

我正在做一个课堂项目,我正在尝试将它超越这里的要求(我正在做自己的作业,只需要帮助改进它!)所以我想更新基于用户所做的某些选择,而不是始终提供所有不相关的选项(要求只是呈现选项)。

我对 Python 还是很陌生,对 Tkinter 还是很陌生,所以我唯一的尝试是:

#Step Type
ttk.Label(mainframe, text = "Step Type").grid(column = 1, row = 16)
type_entry = OptionMenu(mainframe, StepType, "Kill", "Explore" , "Conversation")
type_entry.grid(column = 2, row = 16, sticky = (E))

#Step Goal
if StepType.get() == "Kill":
   ttk.Label(mainframe, text = "Required Kills").grid(column = 1, row = 17)
   goal_entry = ttk.Entry(mainframe, width = 20, textvariable = StepGoal)
   goal_entry.grid(column = 2, row = 17, sticky = (E))
elif StepType.get() == "Explore":
   ttk.Label(mainframe, text = "Location ID").grid(column = 1, row = 17)
   goal_entry = ttk.Entry(mainframe, width = 20, textvariable = StepGoal)
   goal_entry.grid(column = 2, row = 17, sticky = (E))
elif StepType.get() == "Conversation":
   ttk.Label(mainframe, text = "NPC ID").grid(column = 1, row = 17)
   goal_entry = ttk.Entry(mainframe, width = 20, textvariable = StepGoal)
   goal_entry.grid(column = 2, row = 17, sticky = (E))

显然我在这里想要做的是当用户从菜单中选择一个选项时,显示相应的输入框和标签,而不是一直显示所有 3 个。

也在为CheckButton寻找同样的情况

【问题讨论】:

  • 您可以使用一个标签,并且只更改其上的文字。

标签: validation user-interface python-3.x tkinter optionmenu


【解决方案1】:

完整的工作示例:测试 od 2.7.5 和 3.3.2

当用户更改选项时,它使用OptionMenu 中的command= 来调用函数。

import tkinter as ttk

#----------------------------------------------------------------------

def on_option_change(event):
    selected = step_type.get()

    if selected == "Kill":
        goal_label['text'] = "Required Kills"
    elif selected == "Explore":
       goal_label['text'] = "Location ID"
    elif selected == "Conversation":
       goal_label['text'] = "NPC ID"

    # show label and entry
    #goal_label.grid(column=1, row=17)
    #goal_entry.grid(column=2, row=17, sticky='E')

#----------------------------------------------------------------------

mainframe = ttk.Tk()

# Step Type

step_type = ttk.StringVar() # there is the rule: variable name lowercase with _
ttk.Label(mainframe, text="Step Type").grid(column=1, row=16)
type_entry = ttk.OptionMenu(mainframe, step_type, "Kill", "Explore" , "Conversation", command=on_option_change)
type_entry.grid(column=2, row=16, sticky='E')
step_type.set("Kill")

# Step Goal

step_goal = ttk.StringVar()
goal_label = ttk.Label(mainframe, text="Required Kills")
goal_label.grid(column=1, row=17)
goal_entry = ttk.Entry(mainframe, width=20, textvariable=step_goal)
goal_entry.grid(column=2, row=17, sticky='E')

# hide label and entry
#goal_label.grid_forget()
#goal_entry.grid_forget()

# --- star the engine ---

mainframe.mainloop()

顺便说一句:您可以使用grid()grid_forget() 来显示和隐藏元素。


编辑:StringVar 上使用traceRadiobutton 示例

import tkinter as ttk

#----------------------------------------------------------------------

def on_variable_change(a,b,c): # `trace` send 3 argument to `on_variable_change`

    #print(a, b, c)

    selected = step_type.get()

    if selected == "Kill":
        goal_label['text'] = "Required Kills"
    elif selected == "Explore":
       goal_label['text'] = "Location ID"
    elif selected == "Conversation":
       goal_label['text'] = "NPC ID"

#----------------------------------------------------------------------

mainframe = ttk.Tk()

# Step Type

step_type = ttk.StringVar() # there is the rule: variable name lowercase with _

ttk.Label(mainframe, text="Step Type").grid(column=1, row=16)

ttk.Radiobutton(mainframe, text="Kill", value="Kill", variable=step_type).grid(column=2, row=16, sticky='E')
ttk.Radiobutton(mainframe, text="Explore", value="Explore", variable=step_type).grid(column=3, row=16, sticky='E')
ttk.Radiobutton(mainframe, text="Conversation", value="Conversation", variable=step_type).grid(column=4, row=16, sticky='E')

step_type.set("Kill")
# use `trace` after `set` because `on_variable_change` use `goal_label` which is not created yet.
step_type.trace("w", on_variable_change)

# Step Goal

step_goal = ttk.StringVar()
goal_label = ttk.Label(mainframe, text="Required Kills")
goal_label.grid(column=1, row=17)
goal_entry = ttk.Entry(mainframe, width=20, textvariable=step_goal)
goal_entry.grid(column=2, row=17, sticky='E')

# --- star the engine ---

mainframe.mainloop()

【讨论】:

  • 谢谢,这正是我需要的。出于某种原因,真的在 Python 上苦苦挣扎,而使用 Tkinter 和 lxml 并没有帮助。
  • 我在StringVar 上使用trace 添加带有Radiobutton 的示例。我的知识来源:effbot.org/tkinterbook
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-09
  • 1970-01-01
  • 2016-07-28
  • 1970-01-01
  • 2016-01-16
  • 1970-01-01
相关资源
最近更新 更多