簡體   English   中英

python:缺少 1 個必需的位置參數:'self'

[英]python: missing 1 required positional argument: 'self'

我嘗試在谷歌中找到解決方案,但谷歌中的所有解決方案都不適合我,也許在這里我得到了正確的答案。

我有這個代碼:

from tkinter import *


class gui_main:
    def __init__(self):

        self.win_about = Tk() #creat new windows

        self.win_about.title("About software") # Get title to new windows -> "About software"

        self.win_about.geometry("400x120+600+200") # Get size to window

        self.win_about.resizable(width=False, height=False) # Off option Increase / decrease window

        lab = Label(self.win_about, text="""
        welcome to app 
        For Enter to app click on button Next.
        """, justify='center') # Create new text in the window
        lab.place(x=-18, y=-11) # Position of text in window (x - up/down , y - left/right)

        btn_next = Button(self.win_about, text="Next", fg="red", command=gui_main.screen_menu()) # Create new button
        btn_next.place(x=350, y=90) # Position of button in window (x - up/down , y - left/right)

        btn_exit = Button(self.win_about, text="Exit", fg="red", command=self.win_about.destroy) # Create new button
        btn_exit.place(x=10, y=90) # Position of button in window (x - up/down , y - left/right)

        self.win_about.mainloop() # Run the cod in loop

    def screen_menu(self):
        self.win_menu = Tk()
        self.win_menu.title("Menu")
        self.win_menu.geometry("500x500+400+400")
        self.win_about.destroy()
        self.win_menu.mainloop()

if __name__ == "__main__":
    g = gui_main()
    g.win_about()

我得到這個錯誤:

回溯(最近一次調用最后):文件“位置文件”,第 42 行,在 g = gui_main() 文件“位置文件”,第 26 行,在init btn_next = Button(self.win_about, text="Next", fg= "red", command=gui_main.screen_menu()) # 創建新按鈕 TypeError: screen_menu() missing 1 required positional argument: 'self'

Ty尋求幫助,我希望找到任何解決方案

Button command中刪除括號並引用self而不是gui_main

btn_next = Button(self.win_about, text="Next", fg="red", command=self.screen_menu) # Create new button

您想傳遞callable本身,而不是gui_main.screen_menu的返回值

問題是你應該使用self而不是 class 名稱,除了我更喜歡和許多人更喜歡在command=中使用lambda方法。 在 class 中,您必須使用self調用每個方法和變量。 為了在 class 內部工作。
除此之外,我還對您的代碼進行了一些更改。 你不需要調用g.about_screen()因為你在def __init__(self):方法中運行mainloop() 因為它在您初始化 class object 時已經運行g = class_name(); .
這是代碼

from tkinter import *


class gui_main:
    def __init__(self):

        self.win_about = Tk() #creat new windows

        self.win_about.title("About software") # Get title to new windows -> "About software"

        self.win_about.geometry("400x120+600+200") # Get size to window

        self.win_about.resizable(width=False, height=False) # Off option Increase / decrease window

        lab = Label(self.win_about, text="""
        welcome to app 
        For Enter to app click on button Next.
        """, justify='center') # Create new text in the window
        lab.place(x=-18, y=-11) # Position of text in window (x - up/down , y - left/right)

        btn_next = Button(self.win_about, text="Next", fg="red", command=lambda:self.screen_menu()) # Create new button
        btn_next.place(x=350, y=90) # Position of button in window (x - up/down , y - left/right)

        btn_exit = Button(self.win_about, text="Exit", fg="red", command=lambda:self.quit()) # Create new button
        btn_exit.place(x=10, y=90) # Position of button in window (x - up/down , y - left/right)

        self.win_about.mainloop() # Run the cod in loop
    def quit(self):
        self.win_about.quit()
    def screen_menu(self):
        self.win_menu = Tk()
        self.win_menu.title("Menu")
        self.win_menu.geometry("500x500+400+400")
        # self.win_about.destroy()
        self.win_menu.mainloop()

if __name__ == "__main__":
    g = gui_main()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM