2017-06-02 66 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而是使用正確的進口。