Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在另一个文件中执行类函数?_Python_Class_Tkinter - Fatal编程技术网

Python 如何在另一个文件中执行类函数?

Python 如何在另一个文件中执行类函数?,python,class,tkinter,Python,Class,Tkinter,我试图调用一个类函数,它将向Tkinter中的控制台窗口写入一些文本 然而,当我尝试运行它时。我得到下面的错误 TypeError: write() missing 1 required positional argument: 'txt' 这是我的全部代码: main.py from tkinter import * from tkinter.filedialog import askdirectory import os import nam class Window(Frame):

我试图调用一个类函数,它将向Tkinter中的控制台窗口写入一些文本

然而,当我尝试运行它时。我得到下面的错误

TypeError: write() missing 1 required positional argument: 'txt'
这是我的全部代码:

main.py

from tkinter import *
from tkinter.filedialog import askdirectory
import os
import nam


class Window(Frame):

    def __init__(self, master = None):
        Frame.__init__(self, master)
        self.master = master
        self.init_window()


    def init_window(self):
        self.master.title("Source Data Checker")
        self.pack(fill=BOTH, expand=1)

        self.pathLabel = Label(text='Select the location of the source data below and press "Generate Excel"')
        self.pathLabel.place(x=110, y=40)

        self.selectFolderButton = Button(self, text='Select Folder', command=self.openfile)
        self.selectFolderButton.place(x=180, y=350)

        self.executeButton= Button(self, text='Generate Excel', command=self.run)
        self.executeButton.config(state=DISABLED)
        self.executeButton.place(x=330, y=350)

        self.outputWindow = Text()
        self.outputWindow.place(x=100, y=80)
        self.outputWindow.config(width=50, height=15)

    def openfile(self): #open the file
        self.directory = askdirectory()
        if self.directory != '':
            nam.panels_count(self.directory)
            self.executeButton.config(state=NORMAL)
            print(nam.a_nam)

    def run(self, txt):
        pass

    def write(self, txt):
        self.outputWindow.insert(END, str(txt))
        self.update_idletasks()

if __name__ == '__main__':
    root = Tk()
    root.geometry("600x400")
    app = Window(root)
    root.mainloop()
南平

from main import *

def panels_count(folder):

    Window.write('test')
我在想我可能需要实例化它。但是当我这样做的时候,程序甚至不会运行


我缺少什么?

您需要在实例上调用该方法。您正在类上调用它,此时,
self
没有绑定的对象,因此
Window.write('test')
需要两个参数,但没有
txt
的值

openfile
方法可以通过
self
访问实例;将其传递给其他函数:

def openfile(self): #open the file
    self.directory = askdirectory()
    if self.directory != '':
        nam.panels_count(self, self.directory)
        self.executeButton.config(state=NORMAL)
        print(nam.a_nam)


您需要在实例上调用该方法。您正在类上调用它,此时,
self
没有绑定的对象,因此
Window.write('test')
需要两个参数,但没有
txt
的值

openfile
方法可以通过
self
访问实例;将其传递给其他函数:

def openfile(self): #open the file
    self.directory = askdirectory()
    if self.directory != '':
        nam.panels_count(self, self.directory)
        self.executeButton.config(state=NORMAL)
        print(nam.a_nam)