2017-06-02 67 views
0

编辑::的Python :: Tkinter的OptionMenu方法内不工作

我是新来使用Tkinter的,我一直有得到OptionMenu方法到我的阶级结构的内部工作的问题。如果我在类的外面使用Option菜单,它可以正常工作,但由于某种原因,它不喜欢我的类中包含的代码。简化的代码,我有工作如下所示:

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)

类型错误:INIT()从2到3的位置参数,但需要分别给予11

任何帮助或洞察这个呃ror将不胜感激!谢谢你的时间!

山姆

+2

请修复缩进和语法错误以使其可运行 –

+0

问题。是'self.UpdateStatusBar('添加新指令...')'引用一个函数/方法?如果是这样,它不存在。该声明阻止了进一步的测试以及其他错误。直到解决其他错误,我甚至无法开始解决'OptionMenu'问题。 –

+0

向我们展示完整的错误。我怀疑你在名为“OptionMenu”的地方创建了一个类,并且覆盖了tkinter提供的类。 – Novel

回答

0

您确实重写了OptionMenu。通过使用邪恶的通配符导入,您可以从tix中的OptionMenu覆盖tkinter的OptionMenu。但是你仍然在使用tkinter OptionMenu的语法。如果你想使用Tkinter的版本

import tkinter as tk 
from tkinter import tix 

然后:

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

BTW TIX已被弃用,蟒蛇建议您使用TTK而是使用正确的进口。