链接两个 OptionMenu 小部件 Tkinter [英] Linking two OptionMenu widgets Tkinter

查看:28
本文介绍了链接两个 OptionMenu 小部件 Tkinter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面显示的简单代码段中有两个 OptionMenu 小部件:

I have two OptionMenu widgets in the simple pieces of code shown below:

    variable = StringVar(win1)                               
    variable.set(number(number2))
    type = OptionMenu(win1, variable, "None", "Clear", "Dark", "Heavy", )
    type.grid(row=i, column=3, sticky="nsew", padx=1, pady=1)


    variableunit = StringVar(win1)
    variableunit.set(unit)
    unit = OptionMenu(win1, variableunit, "colour", "shade")
    unit.grid(row=i, column=5, sticky="nsew", padx=1, pady=1)

我已经尝试过使用回调函数进行跟踪,但到目前为止还没有奏效.我想在第一个菜单中选择重"时进行链接,第二个菜单始终是颜色".对于其余选项,第二个菜单必须始终是默认的shade",但可以更改.

I have tried traces with a callback function and so far hasn't worked. I would like to link when "Heavy" is chosen in the first menu, the second menu is always "colour". For the rest of the choices, the second menu must always be a default of "shade" but can be changed.

如果有人可以帮助我,我将不胜感激.我已经用变量和跟踪查看了 effbot 站点,但仍然卡住了.

I would be grateful is anyone could help me. I've already looked at the effbot site with variables and traces but am still stuck.

推荐答案

目前还不清楚您想要什么,但我认为应该这样做.

It's not exactly clear what you want, but I think this should do it.

当在第一个菜单中选择重"时,在第二个菜单中选择颜色"并且该菜单被禁用(不能选择任何其他内容).当在第一个菜单中选择其他内容时,第二个菜单会转到mm"并再次启用.

When "Heavy" is selected in the first menu, "colour" is selected in the second one and that menu is disabled (can't select anything else). When something else is selected in the first menu, the second one goes to "mm" and is enabled again.

from Tkinter import *

class app:
    def __init__(self, root):
        win1 = Frame(root)
        win1.grid(row=0,column=0)

        self.variable = StringVar(win1)                               
        self.variable.set(42)
        self.type = OptionMenu(win1, self.variable,
                          "None", "Clear", "Dark", "Heavy",
                          command = self.varMenu)
        self.type.grid(row=1, column=3, sticky="nsew", padx=1, pady=1)


        self.variableunit = StringVar(win1)
        self.variableunit.set('mm')
        self.unit = OptionMenu(win1,
                          self.variableunit, "mm", "colour", "shade")
        self.unit.grid(row=1, column=5, sticky="nsew", padx=1, pady=1)

    def varMenu(self, selection):
        if selection == "Heavy":
            self.variableunit.set("colour")
            self.unit.config(state = DISABLED)
        else:
            self.variableunit.set("mm")
            self.unit.config(state = NORMAL)

root = Tk()
a = app(root)
root.mainloop()

这篇关于链接两个 OptionMenu 小部件 Tkinter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆