Tkinter TypeError:setvar()只取1个参数(给定2个)

时间:2016-09-03 02:54:23

标签: python tkinter

我正在用Tkinter编写一个GUI程序。除了键绑定之外,一切都运行顺利。当我测试它时我得到

  

“TypeError:setvar()只取1个参数(给定2个)”

问题是我在这些实例中没有传递任何参数,所以我不明白问题所在。我陪审团通过在有问题的函数中添加额外的参数来操纵它。这使得绑定工作,但导致共享命令的按钮回退反向类型错误

  

“TypeError:setvar()只需要2个参数(给定1个)”

代码如下。

有问题的约束符合行:18,27,186,187,212,230。

他们调用方法:manipulate.setvar(),manipulate.searching(),manipulate.stuff()和manipulate.newent()

#!/usr/bin/python2.7

from Tkinter import *
import ttk
import os
import json

global inventory
global where
where = False
class widgets:

    def partnumber(self, parent, mode):
            global PN
            do = manipulate()
            PN_in = ttk.Entry(parent, width=11, textvariable=PN)
            if mode == 1:
                PN_in.bind('<1>', do.setvar)
            PN_in.grid(column=1, row=1, sticky=(W, E, S))
            ttk.Label(parent, text='Part#').grid(column=1, row=2, sticky=(N, W))

    def description(self, parent, mode):
            global DESC
            do = manipulate()
            DESC_in = ttk.Entry(parent, width=20, textvariable=DESC)
            if mode == 1:
                DESC_in.bind('<1>', do.setvar)
            DESC_in.grid(column=2, row=1, sticky=(E, S))
            ttk.Label(parent, text='Description').grid(column=2, row=2, sticky=(N, W))

    def quantity(self, parent):
            global QTY
            QTY_in = ttk.Entry(parent, width=3, textvariable=QTY)
            QTY_in.grid(column=3, row=1, sticky=(S))
            ttk.Label(parent, text='Qty.').grid(column=3, row=2, sticky=(N))

    def cost(self, parent):
            global COST
            COST_in = ttk.Entry(parent, width=6, textvariable=COST)
            COST_in.grid(column=4, row=1, sticky=(S))
            ttk.Label(parent, text='Cost').grid(column=4, row=2, sticky=(N))

    def quanchangebox(self, parent):
        global MANQUAN
        MANQUAN_in = Spinbox(parent, from_=1, to=10, textvariable=MANQUAN)
        MANQUAN_in.pack()

class manipulate:

    global name
    name = str('BNEIDATA.txt')

    def searching(self):
        do = manipulate()
        do.search(0)

    def changevar(self, PN):
        item = inventory[PN.get()]
        DESC.set(item[0])
        QTY.set(item[1])
        COST.set(item[2])

    def getresults(self):
        global result, RESULT, inventory, PN, DESC, QTY, COST
        hit = inventory[PN.get()]
    do = manipulate()
    do.changevar(PN)
        bit = str(PN.get())
        result.insert(0, bit)
        bit = str(hit[0])
        RESULT.insert(0, bit)
        result.activate(1)

    def search(self, mode):
        global inventory, PN, DESC
        do = manipulate()
        do.load()
        popup = boxes()
        if inventory.get(PN.get(), False) != False:
            do.getresults()
            if mode == 1:
                popup.howmany(mode)
            if mode == 2:
                popup.howmany(mode)
        if inventory.get(PN.get(), False) == False:
            ghostdict = {}
            for key in iter(inventory):
                entry = inventory[key]
                ghostdict[entry[0].encode()] = key
            if ghostdict.get(DESC.get(), False) != False:
                PN.set(ghostdict[DESC.get()])
                do.search(mode)
            if ghostdict.get(DESC.get(), False) == False:
                popup = boxes()
                popup.additem()

    def newent(self):
        do = manipulate()
        global addbox, PN, DESC, QTY, COST
        temp = [DESC.get(), QTY.get(), COST.get()]
            inventory[PN.get()] = temp
        do.writ()
        addbox.destroy()

    def writ(self):
            global inventory
            os.remove(name)
            with open(name, 'a') as datafile:
                    datafile.write(json.dumps(inventory))
            reset = manipulate()
            reset.setvar()

    def create(self):
            with open(name, 'a') as datafile:
                    bogusfirstentry = {'049007042':['Nylon Bushing', '75', '.47']}
                    datafile.write(json.dumps(bogusfirstentry))

    def find(self, name, breed, path):
            global where
            if breed == 1:
                    for root, dirs, files in os.walk(path):
                            if name in files:
                                    where = root
            if breed == 2:
                    for root, dirs, files in os.walk(path):
                            if name in dirs:
                                    where = os.path.join(root, name)

    def load(self):
            with open(name, 'r') as datafile:
                    temp = datafile.read()
                    global inventory
                    inventory = json.loads(temp)
                    ghostdict = {}
                    for key in iter(inventory):
                        entry=inventory[key]
                        ghostdict[key] = [entry[0].encode(), entry[1], entry[2]]
                    inventory = ghostdict

    def setvar(self):
            global PN, DESC, QTY, COST
            PN.set('')
            DESC.set('')
            QTY.set(0)
            COST.set(0)

    def recieve(self):
        do = manipulate()
        do.search(1)

    def use(self):
        do = manipulate()
        do.search(2)

    def quan_up(self):
        global MANQUAN, PN, inventory, quanbox
        do = manipulate()
        quan = inventory[PN.get()]
            temp = [quan[0], (quan[1]+MANQUAN.get()), quan[2]]
            inventory[PN.get()] = temp
            do.writ()
            quanbox.destroy()
            do.setvar()

    def quan_dn(self):
        global MANQUAN, PN, inventory, quanbox
        do = manipulate()
        quan = inventory[PN.get()]
            temp = [quan[0], (quan[1]-MANQUAN.get()), quan[2]]
            inventory[PN.get()] = temp
            do.writ()
            quanbox.destroy()
            do.setvar()

    def stuff(self):
        global result, RESULT, PN
        do = manipulate()
        PN.set(result.get(result.curselection()[0]))
        do.changevar(PN)

class boxes:
    def main(self):
        do = manipulate()
            root = Tk()
            root.title('BNE Inventory Database')
            root.bind('<Return>', do.searching)
            root.bind('<BackSpace>', do.setvar)
            mainframe = ttk.Frame(root, padding='3 3 3 3')
            mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
            mainframe.columnconfigure(0, weight=1)
            mainframe.rowconfigure(0, weight=1)
            global result, RESULT, PN, DESC, QTY, COST
            PN, DESC, QTY, COST = StringVar(), StringVar(), IntVar(), DoubleVar()

            field = widgets()
            field.partnumber(mainframe, 1)
            field.description(mainframe, 1)
            field.quantity(mainframe)
            field.cost(mainframe)

            ttk.Button(mainframe, text='Search', command=do.searching).grid(column=3, row=6, sticky=(W, E, N))
        ttk.Button(mainframe, text='Recieved', command=do.recieve).grid(column=3, row=6, sticky=(W, E, S))
            ttk.Button(mainframe, text='Adj Cost', command=root.destroy).grid(column=4, row=6, sticky=(W, E, N))
            ttk.Button(mainframe, text='Used', command=do.use).grid(column=3, row=6, sticky=(W, E))
            ttk.Button(mainframe, text='Clear Fields', command=do.setvar).grid(column=4, row=6, sticky=(W, E, S))
            ttk.Button(mainframe, text='Reports', command=root.destroy).grid(column=4, row=6, sticky=(W, E))

            RESULT = Listbox(mainframe, height=5)
            RESULT.grid(column=2, row=6, sticky=(W, E, S))
            result = Listbox(mainframe, height=5, width=11)
            result.grid(column=1, row=6, sticky=(W, E, S,))
            result.bind('<<ListboxSelect>>', do.stuff)

            root.mainloop()

    def additem(self):
            global PN, DESC, QTY, COST, addbox
            addbox = Toplevel()
            addbox.title('Add New Item')
            addboxframe = ttk.Frame(addbox, padding='3 3 3 3')
            addboxframe.grid(column=0, row=0, sticky=(N, W, E, S))
            addboxframe.columnconfigure(0, weight=1)
            addboxframe.rowconfigure(0, weight=1)
            field = widgets()
            field.partnumber(addboxframe, 0)
            field.description(addboxframe, 0)
            field.quantity(addboxframe)
            field.cost(addboxframe)
            do = manipulate()
            addbox.bind('<Return>', do.newent)
            ttk.Button(addboxframe, text='Add New Item', command=do.newent).grid(columnspan=5, row=3, sticky=(W, E))

    def howmany(self, mode):
        global MANQUAN, quanbox
        field = widgets()
        do = manipulate()
        MANQUAN = IntVar()
        quanbox = Toplevel()
        quanboxframe = ttk.Frame(quanbox, padding='3 3 3 3')
        quanboxframe.grid(column=0, row=0, sticky=(N, W, E, S))
        quanboxframe.columnconfigure(0, weight=1)
            quanboxframe.rowconfigure(0, weight=1)
            field.quanchangebox(quanboxframe)
            if mode == 1:
                quanbox.title('Item(s) Recieved')
                ttk.Button(quanboxframe, text=' + Parts', command=do.quan_up).pack()
        if mode == 2:
                quanbox.title('Item(s) Used')
                ttk.Button(quanboxframe, text=' - Parts', command=do.quan_dn).pack()

if __name__ == '__main__':
    check = manipulate()
    gui = boxes()
    check.find('BNEIDATA.txt', 1, '/')
    if where == False:
            check.create()
            check.load()
    if where != False:
            check.load()
    gui.main()

2 个答案:

答案 0 :(得分:1)

将事件绑定到面向对象的处理程序时:

PN_in.bind('<1>', do.setvar)

它将使用两个参数(self, event)

调用处理程序

但是你只定义了一个人拿一个:

def setvar(self):

如果您不需要event参数中包含的信息,您可以将其定义为具有默认值但不使用它的参数:

def setvar(self, event=None):

通过将值默认为None,您仍然可以将其称为代码中没有参数的方法(隐含self参数):

do.setvar()

另一种方法是将它分成两个方法,一个是两个参数,用于绑定,它只是调用其他方面使用的一个参数版本。

答案 1 :(得分:0)

代码中发生的事情是您将事件绑定到PN_in,但是您忘记了将事件对象传递给事件处理程序。

def setvar(self):
            global PN, DESC, QTY, COST
            PN.set('')
            DESC.set('')
            QTY.set(0)
            COST.set(0)

问题在于setvar方法没有采用事件参数,因此没有错误。要解决此问题,只需将event参数添加到方法声明中。

def setvar(self, event):
            global PN, DESC, QTY, COST
            PN.set('')
            DESC.set('')
            QTY.set(0)
            COST.set(0)