链接两个选项菜单小部件Tkin

2024-09-19 23:27:24 发布

您现在位置:Python中文网/ 问答频道 /正文

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

    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)

我尝试过用回调函数跟踪,但到目前为止还没有成功。我想链接当“重”是在第一个菜单,第二个菜单总是“颜色”。对于其余选项,第二个菜单必须始终是默认的“着色”,但可以更改。在

如果有人能帮助我,我将不胜感激。我已经看了efffbot网站的变量和痕迹,但仍然卡住。在


Tags: type菜单unitcolumnvariablegridrowset
1条回答
网友
1楼 · 发布于 2024-09-19 23:27:24

你想要什么还不太清楚,但我认为这应该可以。在

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

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()

相关问题 更多 >