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.
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()