Tkinter OptionsMenu问题

时间:2017-02-20 21:28:16

标签: python tkinter

我有一个Tkinter选项菜单,它根据所选变量运行一个函数。我遇到的错误是:

 Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\ArcGIS10.3\lib\lib-tk\Tkinter.py", line 1486, in __call__
return self.func(*args)
  File "C:\Python27\ArcGIS10.3\lib\lib-tk\Tkinter.py", line 3217, in __call__
self.__callback(self.__value, *args)
TypeError: unbound method func() must be called with RootGUI instance as first argument (got str instance instead)

这是代码:

from Tkinter import *
import subprocess
import webbrowser
import threading


class RootGUI:
    def __init__(self, master):
        self.master = master
        master.title("Greetings")



        self.label = Label(master, text="Hello Mr. Swordy. What would you like to do?")
        self.label.pack()

    def func(self, value):
        if "EC Network Manual" in value:
            url = "https://eneprojects.enengineering.com/XCEL/_layouts/15/WopiFrame.aspx?sourcedoc=/XCEL/Shared%20Documents/Xcel%20Distribution%20Design%20Project/Xcel%20Design%20Standards/Electric%20Manuals/2014_NETWORK_MANUAL.pdf&action=default"
            webbrowser.open_new(url)
        elif "EC Field Manual" in value:
            url = 'https://eneprojects.enengineering.com/XCEL/_layouts/15/WopiFrame.aspx?sourcedoc=/XCEL/Shared%20Documents/Xcel%20Distribution%20Design%20Project/Xcel%20Design%20Standards/Electric%20Manuals/2016_FIELD_MANUAL.pdf&action=default'
            webbrowser.open_new(url)
        elif "EC ODL Manual" in value:
            url = 'https://eneprojects.enengineering.com/XCEL/_layouts/15/WopiFrame.aspx?sourcedoc=/XCEL/Shared%20Documents/Xcel%20Distribution%20Design%20Project/Xcel%20Design%20Standards/Electric%20Manuals/2016_ODL_MANUAL.pdf&action=default'
            webbrowser.open_new(url)
        elif "EC OH Manual" in value:
            url = 'https://eneprojects.enengineering.com/XCEL/_layouts/15/WopiFrame.aspx?sourcedoc=/XCEL/Shared%20Documents/Xcel%20Distribution%20Design%20Project/Xcel%20Design%20Standards/Electric%20Manuals/2016_OH_MANUAL.pdf&action=default'
            webbrowser.open_new(url)
        elif "EC UG Manual" in value:
            url = 'https://eneprojects.enengineering.com/XCEL/_layouts/15/WopiFrame.aspx?sourcedoc=/XCEL/Shared%20Documents/Xcel%20Distribution%20Design%20Project/Xcel%20Design%20Standards/Electric%20Manuals/2016_UG_MANUAL.pdf&action=default'
            webbrowser.open_new(url)
        elif "EC Blue Book" in value:
            url = 'https://eneprojects.enengineering.com/XCEL/_layouts/15/WopiFrame.aspx?sourcedoc=/XCEL/Shared%20Documents/Xcel%20Distribution%20Design%20Project/Xcel%20Design%20Standards/Electric%20Manuals/Xcel-Energy-Standard-For-Electric-Installation-and-Use.pdf&action=default'
            webbrowser.open_new(url)    
        elif "Gas Materials Manual" in value:
            url = 'https://eneprojects.enengineering.com/XCEL/_layouts/15/WopiFrame.aspx?sourcedoc=/XCEL/Shared%20Documents/Xcel%20Distribution%20Design%20Project/Xcel%20Design%20Standards/Gas%20Manuals/Gas%20Materials%20Manual%208-31-14.pdf&action=default'
            webbrowser.open_new(url)
        elif "Gas Design Manual" in value:
            url = 'https://eneprojects.enengineering.com/XCEL/_layouts/15/WopiFrame.aspx?sourcedoc=/XCEL/Shared%20Documents/Xcel%20Distribution%20Design%20Project/Xcel%20Design%20Standards/Gas%20Manuals/Gas%20Design%20Manual_V3.1.pdf&action=default'
            webbrowser.open_new(url)
        elif "Gas Compliance and Standards Manual" in value:
            url = 'https://eneprojects.enengineering.com/XCEL/_layouts/15/WopiFrame.aspx?sourcedoc=/XCEL/Shared%20Documents/Xcel%20Distribution%20Design%20Project/Xcel%20Design%20Standards/Gas%20Manuals/Compliance%20and%20Stds%20Manual%20Nov%202016.pdf&action=default'
            webbrowser.open_new(url)
        elif "Gas Material Catalog" in value:
            url = 'https://eneprojects.enengineering.com/XCEL/_layouts/15/WopiFrame.aspx?sourcedoc=/XCEL/Shared%20Documents/Xcel%20Distribution%20Design%20Project/Xcel%20Design%20Standards/Gas%20Manuals/GasMaterialCatalog-%20with%20drilldown.xlsm&action=default'
            webbrowser.open_new(url)




root = Tk()
options = ["EC Network Manual", "EC Field Manual", "EC ODL Manual","EC OH Manual",
       "EC UG Manual", "EC Blue Book", "Gas Materials Manual", "Gas Design Manual",
       "Gas Compliance and Standards Manual","Gas Material Catalog"]
var = StringVar()
var.set("Documents")
drop = OptionMenu(root, var, *options, command=RootGUI.func)
drop.place(x=10, y=10)

my_gui = RootGUI(root)
root.mainloop()

它声明我需要传递RootGUI作为参数,我该怎么做?任何建议将不胜感激。

注意:我意识到这可能不符合样式标准,我为我不熟悉Python的草率代码道歉。

1 个答案:

答案 0 :(得分:0)

错误告诉您正在调用类上的方法,但是您需要在类的实例上调用该方法。

您需要在创建选项菜单之前创建根GUI,然后根据实例

引用该方法
my_gui = RootGUI(root)
...
drop = OptionMenu(root, var, *options, command=my_gui.func)