TypeError: Menus.__init__() 采用 1 个位置参数,但给出了 2 个

发布于 2025-01-16 08:25:37 字数 2442 浏览 2 评论 0原文

我对他的错误 TypeError: Menus.__init__() gets 1positional argument but 2 were Gived 感到震惊,请帮助我解决他的错误

main file: -->;

from tkinter import *
from meanu_items import *


def main():
    windows = Tk()
    windows.title("NotePad")
    windows.minsize(width=60, height=80)
    windows.config()
    # Create place to write a text
    text = Text(windows)
    text.focus()
    text.grid(pady=0.5, padx=10)

    # Creating Scale
    scroll = Scrollbar(windows, orient=VERTICAL, )
    scroll.grid(row=0, column=1, sticky=NS + SE)
    text.config(yscrollcommand=scroll.set)
    scroll.config(command=text.yview)

    menu = Menus(windows)

    windows.mainloop()


if __name__ == '__main__':
    main()

meanu_items 文件:-->

 from tkinter import *
from tkinter import filedialog

SIDE_R = RIGHT


class Menus(Frame):

    def __init__(self):
        Frame.__init__()
        self.filetypes = (
            ('text files', '*.txt'),
            ('All files', '*.*'))
        self.text = Text()
        self.menubar()

    def menubar(self):
        # Menu bar

        # # File
        menubar = Menu(self)

        file = Menu(menubar, tearoff=0)

        file.add_command(label="New")
        file.add_command(label="Open", command=self.opens)
        file.add_command(label="Save")
        file.add_command(label="close")

        file.add_separator()

        file.add_command(label="Exit", command=self.quit)
        menubar.add_cascade(label="File", menu=file)

        # # Edit

        edit = Menu(menubar, tearoff=0)

        edit.add_command(label="Copy")
        edit.add_command(label="Paste")
        edit.add_command(label="Cute")
        edit.add_command(label="Delete")
        edit.add_command(label="Select All")

        menubar.add_cascade(label="Edit", menu=edit)

        # # Help
        helps = Menu(menubar, tearoff=0)
        helps.add_command(label="About")
        menubar.add_cascade(label="Help", menu=help)

    def opens(self):
        dlg = filedialog.Open(self, filetypes=self.filetypes)
        fl = dlg.show()

        if fl != '':
            text = self.readFile(fl)
            self.text.insert(END, text)

上面的文件中也存在一些小错误(meanU_items):-->

Expected type 'Menu', got '(args: tuple[Any, ...], kwds: dict[str, Any]) -> None' instead : 50
Unresolved attribute reference 'readFile' for class 'Menus' :57

I'm shucked at his error TypeError: Menus.__init__() takes 1 positional argument but 2 were given any one please help me to solve his error

main file: -->

from tkinter import *
from meanu_items import *


def main():
    windows = Tk()
    windows.title("NotePad")
    windows.minsize(width=60, height=80)
    windows.config()
    # Create place to write a text
    text = Text(windows)
    text.focus()
    text.grid(pady=0.5, padx=10)

    # Creating Scale
    scroll = Scrollbar(windows, orient=VERTICAL, )
    scroll.grid(row=0, column=1, sticky=NS + SE)
    text.config(yscrollcommand=scroll.set)
    scroll.config(command=text.yview)

    menu = Menus(windows)

    windows.mainloop()


if __name__ == '__main__':
    main()

meanu_items file: -->

 from tkinter import *
from tkinter import filedialog

SIDE_R = RIGHT


class Menus(Frame):

    def __init__(self):
        Frame.__init__()
        self.filetypes = (
            ('text files', '*.txt'),
            ('All files', '*.*'))
        self.text = Text()
        self.menubar()

    def menubar(self):
        # Menu bar

        # # File
        menubar = Menu(self)

        file = Menu(menubar, tearoff=0)

        file.add_command(label="New")
        file.add_command(label="Open", command=self.opens)
        file.add_command(label="Save")
        file.add_command(label="close")

        file.add_separator()

        file.add_command(label="Exit", command=self.quit)
        menubar.add_cascade(label="File", menu=file)

        # # Edit

        edit = Menu(menubar, tearoff=0)

        edit.add_command(label="Copy")
        edit.add_command(label="Paste")
        edit.add_command(label="Cute")
        edit.add_command(label="Delete")
        edit.add_command(label="Select All")

        menubar.add_cascade(label="Edit", menu=edit)

        # # Help
        helps = Menu(menubar, tearoff=0)
        helps.add_command(label="About")
        menubar.add_cascade(label="Help", menu=help)

    def opens(self):
        dlg = filedialog.Open(self, filetypes=self.filetypes)
        fl = dlg.show()

        if fl != '':
            text = self.readFile(fl)
            self.text.insert(END, text)

some minor error are also there in above file(meanU_items):-->

Expected type 'Menu', got '(args: tuple[Any, ...], kwds: dict[str, Any]) -> None' instead : 50
Unresolved attribute reference 'readFile' for class 'Menus' :57

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

权谋诡计 2025-01-23 08:25:37

这很难说,但也许您打算传入 windows 参数以将其传递到 Frame 超类:

class Menus(Frame):

    def __init__(self, windows):
        super().__init__(windows)
        ...

Its difficult to tell, but perhaps you meant to pass in the windows parameter to pass it onto the Frame super class:

class Menus(Frame):

    def __init__(self, windows):
        super().__init__(windows)
        ...
黑凤梨 2025-01-23 08:25:37

Menu 类正在询问“self”,

class Menus(Frame):

def __init__(self):

但您给它的是一个窗口对象,而不是 Menus 对象。在 init: 中写入此内容

class Menus(Frame):

def __init__(self, windows):

并创建 Menus 对象,如下所示:-

menu = Menus(menus_items.Menus, windows)

Menu class is asking 'self'

class Menus(Frame):

def __init__(self):

but instead of a Menus object you gave it a window object. Write this in init:

class Menus(Frame):

def __init__(self, windows):

and create Menus object like this:-

menu = Menus(menus_items.Menus, windows)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文