【问题标题】:Python:: tkinter OptionMenu Method Inside of Class Structure Not WorkingPython:: 类结构内部的 tkinter OptionMenu 方法不起作用
【发布时间】:2017-06-02 15:05:01
【问题描述】:

编辑::

我是使用 tkinter 的新手,并且在让 OptionMenu 方法在我的类结构中工作时遇到问题。如果我在类之外使用选项菜单,它可以正常工作,但由于某种原因,它不喜欢我的类中包含的代码。我正在使用的简化代码如下所示:

from tkinter import *
from tkinter.tix import *

class myClass():

    def __init__(self, master):
        self.master = master
        master.title('option menu test')

        #create tool bar and set custom color
        toolbarColor = '#%02x%02x%02x' % (117, 117, 119)
        self.toolbar = Frame(master, bg=toolbarColor)

        #add instructions button
        self.addInstructionButton = Button(self.toolbar, text='Add Instruction', command=self.addNewFrame)
        self.addInstructionButton.pack(side=LEFT, padx=4, pady=4)

        #pack tool bar
        self.toolbar.pack(side=TOP, fill=X)

        #initialize new frames to add and counters
        self.newInstructionCount = 0
        self.newInstructionFrame = []
        self.instructionCloseButton = []
        self.instructionFrame = Frame(self.master,height=410,width=780)
        self.instructionFrame.pack(side=TOP)

    def addNewFrame(self):
        #create new frame and append
        self.newInstructionFrame.append(Frame(self.instructionFrame, width=785, height=100,bd=1,relief=SUNKEN)) #width and height are pixels
        tempFrame = self.newInstructionFrame
        self.instructionFrame.pack_propagate(False)
        self.instructionFrame.grid_propagate(False)
        self.newInstructionFrame[self.newInstructionCount].pack(side=TOP,fill=X)
        #add drop down menu for modifications
        self.modChoices  = ['option 0',
            'option 1',
            'option 2',
            'option 3',
            'option 4',
            'option 5']
        self.modStringVar = StringVar()

        ##### OPTION MENU ERROR HERE #####
        self.modPopupMenu = OptionMenu(tempFrame,StringVar(),self.modStringVar,self.modChoices[0],*self.modChoices)

        self.modLabel     = Label(self.newInstructionFrame[self.newInstructionCount], text='Option Label')
        self.modLabel.pack(side=LEFT)
        self.modPopupMenu.pack(side=LEFT)
        self.newInstructionCount = self.newInstructionCount+1

## MAIN ##
root = Tk()
runGUI = myClass(root)
root.mainloop()

文件“C:/Users/me/Desktop/myFolder/myProject/GUI_code.py”,第 192 行,在 addNewFrame 中

self.modPopupMenu = OptionMenu(tempFrame,StringVar(),self.modStringVar,self.modChoices[0],*self.modChoices)

TypeError: init() 接受 2 到 3 个位置参数,但给出了 11 个

任何有关此错误的帮助或见解将不胜感激!感谢您的宝贵时间!

山姆

【问题讨论】:

  • 请修复缩进和语法错误,使其可运行
  • 问题。 self.UpdateStatusBar('Adding new instruction...') 是指函数/方法吗?如果是这样,它不存在。此声明会阻止进一步测试以及其他错误。在解决您的其他错误之前,我什至无法开始解决 OptionMenu 问题。
  • 向我们展示完整的错误。我怀疑你在某处创建了一个名为“OptionMenu”的类并覆盖了 tkinter 提供的类。
  • 抱歉代码问题。我已经修复了代码并编辑了上面的条目。谢谢!

标签: python tkinter


【解决方案1】:

您确实覆盖了 OptionMenu。通过使用邪恶的通配符导入,您可以使用来自 tix 的 OptionMenu 覆盖来自 tkinter 的 OptionMenu。但是您仍在使用 tkinter OptionMenu 中的语法。使用正确的导入:

import tkinter as tk
from tkinter import tix

那么如果你想使用tkinter版本:

self.modPopupMenu = tk.OptionMenu(tempFrame,StringVar(),self.modStringVar,self.modChoices[0],*self.modChoices)

BTW tix 已弃用,python 建议您改用 ttk。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多