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 个

对此错误的任何帮助或见解将不胜感激!谢谢你的时间!

山姆

小说

您确实覆盖了 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)

顺便说一句,不推荐使用 tix,python 建议您改用 ttk。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章