Could not call function from another class; Error: FrameLft.on_clickMB() missing 1 required positional argument: ‘self’


import tkinter as tk
from tkinter import *

frameLH = 120


def main_window():  # ========================== Main Window ==============================
    window = tk.Tk()
    window.geometry("950x650")
    window.minsize(750, 625)
    window.configure(bg='#65A8E1')

    FrameTop(window)
    FrameBot(window)
    FrameLft(window)
    FrameRgt(window)
    window.mainloop()


class FrameTop:  # ============================ Top Frame =================================
    def __init__(self, root):
        self.root = root
        self.frame_top = tk.Frame(root, height=100, bg="#70AD47", relief=tk.RAISED, borderwidth=5)
        self.frame_top.pack(fill=tk.X, side=tk.TOP)

        self.btnM = (tk.Button(self.frame_top, bg="#4472C4", fg="White", bd=2, font=("Calibre", 12, 'bold'), activebackground='#ED7D31', text="Menu", command=FrameLft.on_clickMB))
        
`# could not call function from other class FrameLft ====`
        self.btnM.place(x=10, y=45)


class FrameBot:  # ============================ Bottom Frame =================================
    def __init__(self, root):
        self.root = root
        frame_bot = Frame(root, height=45, bg="#4472C4", relief=tk.RAISED, borderwidth=5)  # blue
        frame_bot.pack(fill=tk.X, side=tk.BOTTOM)


class FrameLft:  # ============================ Left Frame =================================
    def __init__(self, root):
        self.root = root
        self.frame_lft = tk.Frame(root, width=120, bg="#ED7D31", relief=tk.RAISED, borderwidth=5) 
        self.frame_lft.pack(fill=tk.Y, side=tk.LEFT)

    def on_clickMB(self):  # ========================= Minimize/Maximize Menu ====================
        global frameLH

        if frameLH == 120:
            self.frame_lft.config(width=70)
            frameLH = 70
        elif frameLH == 70:
            self.frame_lft.config(width=120)
            frameLH = 120


class FrameRgt:  # ============================ Right Frame =================================
    def __init__(self, root):
        self.root = root
        frame_rgt = tk.Frame(self.root, width=130, bg="#FFC000", relief=tk.RAISED, borderwidth=5) 
        frame_rgt.pack(fill=tk.Y, side=tk.RIGHT)


if __name__ == "__main__":
    main_window()


I have defined a function “on_clickMB(self)” in class “FrameLft”.

I want to call this function by tk.Button placed in class “FrameTop”.

On clicking the tk.Button, the error shows: TypeError: FrameLft.on_clickMB() is missing 1 required positional argument: ‘self’.

The frames should be in sequence: FrameTop, FrameBot, FrameLft, and FrameRgt.

New contributor

Rafiq Panja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

This error occurs because with the button you call a new class and not the old class that you have initialised before. You should save the classes in variables and then call the functions via the variables.
This code should work:

import tkinter as tk
from tkinter import *

frameLH = 120


def main_window():  # ========================== Main Window ==============================
    global frame_top, frame_bottom, frame_left, frame_right
    window = tk.Tk()
    window.geometry("950x650")
    window.minsize(750, 625)
    window.configure(bg='#65A8E1')

    frame_left = FrameLft(window)
    frame_right = FrameRgt(window)
    frame_top = FrameTop(window)
    frame_bottom = FrameBot(window)
    window.mainloop()


class FrameTop:  # ============================ Top Frame =================================
    def __init__(self, root):
        self.root = root
        self.frame_top = tk.Frame(root, height=100, bg="#70AD47", relief=tk.RAISED, borderwidth=5)
        self.frame_top.pack(fill=tk.X, side=tk.TOP)

        self.btnM = (tk.Button(self.frame_top, bg="#4472C4", fg="White", bd=2, font=("Calibre", 12, 'bold'), activebackground='#ED7D31', text="Menu", command=frame_left.on_clickMB()))
        
# could not call function from other class FrameLft ====
        self.btnM.place(x=10, y=45)


class FrameBot:  # ============================ Bottom Frame =================================
    def __init__(self, root):
        self.root = root
        frame_bot = Frame(root, height=45, bg="#4472C4", relief=tk.RAISED, borderwidth=5)  # blue
        frame_bot.pack(fill=tk.X, side=tk.BOTTOM)


class FrameLft:  # ============================ Left Frame =================================
    def __init__(self, root):
        self.root = root
        self.frame_lft = tk.Frame(root, width=120, bg="#ED7D31", relief=tk.RAISED, borderwidth=5) 
        self.frame_lft.pack(fill=tk.Y, side=tk.LEFT)

    def on_clickMB(self):  # ========================= Minimize/Maximize Menu ====================
        global frameLH

        if frameLH == 120:
            self.frame_lft.config(width=70)
            frameLH = 70
        elif frameLH == 70:
            self.frame_lft.config(width=120)
            frameLH = 120


class FrameRgt:  # ============================ Right Frame =================================
    def __init__(self, root):
        self.root = root
        frame_rgt = tk.Frame(self.root, width=130, bg="#FFC000", relief=tk.RAISED, borderwidth=5) 
        frame_rgt.pack(fill=tk.Y, side=tk.RIGHT)


if __name__ == "__main__":
    main_window()

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật