0

私が呼び出そうとしている関数は以下の通りです:

def numberitems(self):
    files = len(os.listdir(self.directory))
    print (items)

ボタンのコード:

button = Button(text = 'Count Items', command = Class1.numberItems()).pack()

クラスをインポートする場所:

from class import Class1

ディレクトリの定義:

def loadDirectory():
    return Class1(filedialog.askdirectory())
    dir = loadDirectory()
4

2 に答える 2

2

あなたが説明しているのは標準出力ではありません。printStdout は、ステートメントを使用するときの場所です。あなたが説明しているのは、関数の戻り値です。

Tkinter は他のすべての python パッケージと同様に、戻り値が呼び出し元に返されます。この状況での呼び出し元は、イベント ループです。イベント ループは、呼び出した関数の戻り値を使用する方法がわからないため、結果を破棄します。

于 2013-02-18T14:33:30.313 に答える
0

tcaswell が提供したコンテキストに基づいて、メイン コードにいくつかのエラーがあります。

a を次のように仮定chrome.pyします。

class Chrome:
    directory = ""

    def __init__(self, directory):
        self.directory = directory

    def numberOfFiles(self):
        return len(os.listdir(self.directory))

それを呼び出して の結果を出力するにはnumberOfFiles、次のようにします。

ファイル:q_14938600.py

import os
from chrome import Chrome
from Tkinter import *

class App(object):
    """Basic TK App."""

    def __init__(self, parent):
        f = Frame(parent)
        f.pack(padx=15, pady=15)

        # Here I initiate the Chrome class, and set its directory. I'm using os.getcwd() as an example.
        self.ch = Chrome(os.getcwd())

        # Here I tell the button what to call when clicked. Note I'm NOT passing arguments to the function, just a reference.
        button = Button(f, text='Count Files', command=self.printFileCount)
        button.pack(side=BOTTOM)

    def printFileCount(self):
        # And here, I print the output of ch.numberOfFiles, which was defined in the Chrome class
        print(self.ch.numberOfFiles())

if __name__ == "__main__":
    # This is just standard app stuff
    root = Tk()
    root.title('q_14938600')
    app = App(root)
    root.mainloop()
于 2013-02-18T15:32:18.813 に答える