根据 tcaswell 给出的上下文,您的主代码中有几个错误。
假设一个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()