每次更改时,如何打印所选的OptionMenu项目?

时间:2018-07-12 02:49:58

标签: python tkinter

我是在python中使用类的新手,并且正在为此任务而苦苦挣扎。最初运行该代码时,它的运行方式符合我的期望,但是,当我在OptionMenu中选择任一选项时,会出现一个很大的长错误,并且我无法理解问题的描述。

from tkinter import *
from tkinter import ttk


def changeChoice(instance, *args):
    if instance.getChoice() == instance.getOptions(0):
        print(instance.getOptions(0))
    else:
        print(instance.getOptions(1))

class ClassA:
    def __init__(self, parent):
        self._parent = parent
        self._text = "Text"
        self._choice = StringVar()
        self._choice.set("two")
        self._options = ["one", "two"]
        self._choice.trace("w", changeChoice(self))

    def getParent(self):
        return self._parent

    def getChoice(self):
        return self._choice.get()

    def getOptions(self, listNumber):
        if listNumber == "list":
            return self._options
        else:
            return self._options[listNumber]

    def createMenu(self):
        optionMenu = OptionMenu(ClassA.getParent(self), self._choice, 
*ClassA.getOptions(self, "list"))
        optionMenu.pack()


if __name__ == "__main__":
    root = Tk()
    instance1 = ClassA(root)
    instance1.createMenu()
    root.mainloop()

1 个答案:

答案 0 :(得分:0)

以下修改没有问题

from tkinter import *
from tkinter import ttk

class ClassA:
    def __init__(self, parent):
        self._parent = parent
        self._text = "Text"
        self._choice = StringVar()
        self._choice.set("two")
        self._options = ["one", "two"]
        self._choice.trace("w", self.changeChoice)

    def changeChoice(self, *args):
        if self.getChoice() == self.getOptions(0):
            print(self.getOptions(0))
        else:
            print(self.getOptions(1))

    def getParent(self):
        return self._parent

    def getChoice(self):
        return self._choice.get()

    def getOptions(self, listNumber):
        if listNumber == "list":
            return self._options
        else:
            return self._options[listNumber]

    def createMenu(self):
        optionMenu = OptionMenu(self._parent, self._choice, *self._options)
        optionMenu.pack()

if __name__ == "__main__":
    root = Tk()
    instance1 = ClassA(root)
    instance1.createMenu()
    root.mainloop()

修改

寻找选项菜单的ttk版本,我发现tkinter.OptionMenuttk.OptionMenu有一个command参数。 当您选择一个选项时,该调用。回调的第一个参数是textvariable的值。

使用它将看起来像

import tkinter as tk
from tkinter import ttk

class ClassA:
    def __init__(self, parent):
        self._parent = parent
        self._choice = tk.StringVar(value="two")
        self._options = ["one", "two"]

    def changeChoice(self, value, *args):
        print(value)

    def getParent(self):
        return self._parent

    def getChoice(self):
        return self._choice.get()

    def getOptions(self, listNumber=None):
        if listNumber is None:
            return self._options
        return self._options[listNumber]

    def createOptionMenu(self):
        optionMenu = tk.OptionMenu(self._parent, self._choice,
                     *self._options, command=self.changeChoice)
        optionMenu.pack()

if __name__ == "__main__":
    root = Tk()
    instance1 = ClassA(root)
    instance1.createOptionMenu()
    root.mainloop()

ttk.OptionMenu

  • 使用菜单中显示复选标记的单选按钮
  • 奇怪地使用default参数,如果未指定,则第一个值是默认值,并且将从值中将其删除。 textvariable控制默认设置。
  • 限制传递给ttk.Menubutton的参数

我写了另一种ttk.OptionMenu

使用此类,您可以添加带有图像和子菜单的完全自建菜单(无需将MenuButton设置为Menu的父菜单)。如果没有提供菜单,它将基于作为Menu.add_command参数输入的列表来构建带有values的菜单。如果您指定menu自变量,则commandvalues自变量将被忽略。

import tkinter as tk
from tkinter import ttk

class ttkOptionMenu(ttk.Menubutton):
    def __init__(self, master=None, **kwargs):
        values = kwargs.pop('values', [])
        command = kwargs.pop('command', None)
        textvariable = kwargs.get('textvariable')
        self.menu = kwargs.get('menu')
        if not self.menu:
            self.menu = tk.Menu()
            kwargs['menu'] = self.menu
            for v in values:
                self.menu.add_command(label=v,
                              command=tk._setit(textvariable, v, command))

        self.menu['tearoff'] = 0
        ttk.Menubutton.__init__(self, master, **kwargs)

def optionCommand(value, *args):
    print('Changed:', value)

if __name__ == "__main__":
    root = Tk()
    optionVar = tk.StringVar(value='two')
    optMenu = ttkOptionMenu(root, textvariable=optionVar,
                            command=optionCommand, values=['one', 'two'],
                            direction='right')
    optMenu.pack()
    root.mainloop()

为什么ttk.OptionMenutextvariable方法破坏destroy?它不是此变量的所有者。