【发布时间】: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 提供的类。
-
抱歉代码问题。我已经修复了代码并编辑了上面的条目。谢谢!