如何从另一个函数中调用函数?

时间:2017-03-31 17:46:50

标签: python

我正在创建一个简单的GUI程序来管理优先级。我在从另一个函数中访问函数时遇到了麻烦。在我的程序中,我尝试创建一个GuiPart类的实例,然后调用g.addItem,但它似乎不像那样工作。我收到一个错误:

/usr/bin/python3.5 /home/cali/PycharmProjects/priorities/priorities.py
Traceback (most recent call last):
 File "/home/cali/PycharmProjects/priorities/priorities.py", line 70, in > <module>
   g.display()
 File "/home/cali/PycharmProjects/priorities/priorities.py", line 39, in > display
   command = g.addItem)
 File "/usr/lib/python3.5/tkinter/__init__.py", line 2077, in grid_configure
   + self._options(cnf, kw))
_tkinter.TclError: bad option "-command": must be -column, -columnspan, -in, > -ipadx, -ipady, -padx, -pady, -row, -rowspan, or -sticky

这就是我所做的:

# priorities.py
#   GUI program to manage priorities

from tkinter import *

class Priority:

    def __init__(self, subject, priority):
        self.subject = subject
        self.priority = priority

    def subject(self):
        return self.subject

    def priority(self):
        return self.priority


class GuiPart:

    def __init__(self):
        self.root = self.createWindow()

    def createWindow(self):

        root = Tk()
        root.resizable(width = False, height = False)
        root.title("Priorities")

        return root

    def createWidgets(self):

        listBox = Listbox().grid(row=1)

        buttonAdd = Button(text = "Add").grid(row = 2,
                                  column = 0,
                                  sticky = W,
                                  command = g.addItem)

        buttonRemove = Button(text="Remove").grid(row = 2,
                                   column = 0,
                                   sticky = W,
                                   command = g.removeItem)

        buttonEdit = Button(text="Edit").grid(row = 2,
                                 column = 0,
                                 sticky = E,
                                 command = g.editItem)

        textBox = Text().grid(row = 3)

    def addItem(self, item):
        item = Priority(item.subject, item.priority)
        item.subject = g.textBox.get("1.0", 'end-1c')

        g.listBox.insert(END, self)

    def removeItem(self):
       pass

    def editItem(self):
        pass

class Client:
    pass

if __name__ == "__main__":
    g = GuiPart()
    g.createWidgets()
    g.root.mainloop()

2 个答案:

答案 0 :(得分:2)

让我们来看看你的部分代码......

    buttonAdd = Button(text = "Add").grid(row = 2,
                              column = 0,
                              sticky = W,
                              command = g.addItem)

这里的主要问题是commandButton()构造函数的参数,而不是.grid()方法。这就是你当前的错误所在。

第二个问题是,您分配给buttonAdd的值是.grid()(始终为None)的结果,而不是按钮本身。如果您确实需要对按钮的引用以供以后使用(通常,您没有),则必须将其拆分为两个语句:

    buttonAdd = Button(text = "Add", command = g.addItem)
    buttonAdd.grid(row = 2,
                   column = 0,
                   sticky = W)

答案 1 :(得分:0)

见:

buttonAdd = Button(text = "Add").grid(row = 2,
                             column = 0,
                             sticky = W,
                             command = g.addItem)
#                                   ^^^^^^^^^

buttonRemove = Button(text="Remove").grid(row = 2,
                             column = 0,
                             sticky = W,
                             command = g.removeItem)
#                                    ^^^^^^^^^^^^

buttonEdit = Button(text="Edit").grid(row = 2,
                            column = 0,
                            sticky = E,
                            command = g.editItem)
#                                   ^^^^^^^^^^

调用分配这些功能。 为了调用它们,你必须将参数传递给它们。

解释发生的事情的一个小例子:

>>> id(print)
1769760
>>> some_var = print
>>> id(some_var)
1769760
>>> some_var
<built-in function print>
>>> some_var('we assigned the built-in function "print" to the variable 
... some_var')
we assigned the built-in function "print" to the variable some_var