tkinter text widget not able to appear on desired window

So i have written a simple tkinter gui that will show some command output results when prompted, only issue i’m having is the command wont appear on any other screen.

def updateipt():
    text = Text(main_window, height=150, width=124)
    text.pack()
    with Popen(["iptables", '-L', '-v', '-n'], stdout=PIPE, stderr=PIPE) as p:
        for line in p.stdout:
            text.insert(END, line,)

text = Text(main_window, height=150, width=124)

i assumed that replacing the main_window argument above with another window would direct the text widget to a different window, but everytime i try this i get an error Which states that the window needs to be Tk(): AttributeError: ‘function’ object has no attribute ‘tk’

How do i work around this?

Full code posted below:

#!/bin/bash
import os
import subprocess as sub
from tkinter import *
from subprocess import Popen, PIPE


def main_window():
    global main_window
    main_window = Tk()
    main_window.title("Firewall test")
    main_window.geometry("1000x700")
    main_window.configure(bg="dark grey")
    label1 = Label(main_window, text="What Would you like to configure?", font=('arial', 12, 'bold'), bg="white",
                   fg="black")
    label1.pack(fill=X, pady=20)

    btnproc = Button(main_window, text="Process configuration", font=('arial', 9, 'bold'), bg="light grey", fg="black",
                     width="30", height="2", command=lambda: process_window())
    btnproc.pack(pady=20)
    btnfw = Button(main_window, text="Firewall", font=('arial', 9, 'bold'), bg="light grey", fg="black", width="30",
                   height="2", command=lambda: firewall_window())
    btnfw.pack(pady=20)
    btnmon = Button(main_window, text="AIDE", font=('arial', 9, 'bold'), bg="light grey", fg="black", width="30",
                    height="2", command=lambda: monitor_window())
    btnmon.pack(pady=20)

    main_window.mainloop()


def process_window():
    global process_window
    process_window = Toplevel(main_window)
    process_window.title("Process Configuration")
    process_window.geometry("800x600")
    process_window.configure(bg="dark grey")
    label1 = Label(process_window, text="What Would you like to configure?", font=('arial', 12, 'bold'), bg="white",
                   fg="black")
    label1.pack(fill=X, pady=20)



def firewall_window():
    firewall_window = Toplevel(main_window)
    firewall_window.title("Firewall Configuration")
    firewall_window.geometry("1000x600")
    firewall_window.configure(bg="dark grey")
    label1 = Label(firewall_window, text="What Would you like to configure?", font=('arial', 12, 'bold'), bg="white",
                   fg="black")
    label1.pack(fill=X, pady=20)

    updatebtn = Button(firewall_window, text="View Current Iptables layout", font=('arial', 9, 'bold'), bg="light grey",
                       fg="black", width="30",
                       height="2", command=lambda: updateipt())
    updatebtn.pack(pady=20)


def monitor_window():
    monitor_window = Toplevel(main_window)
    monitor_window.title("AIDE Configuration")
    monitor_window.geometry("800x600")
    monitor_window.configure(bg="dark grey")
    label1 = Label(monitor_window, text="What Would you like to configure?", font=('arial', 12, 'bold'), bg="white",
                   fg="black")
    label1.pack(fill=X, pady=20)


def updateipt():
    text = Text(firewall_window, height=150, width=124)
    text.pack()
    with Popen(["iptables", '-L', '-v', '-n'], stdout=PIPE, stderr=PIPE) as p:
        for line in p.stdout:
            text.insert(END, line,)




main_window()

Answer

In your code, the name of your functions are also the name of your windows (Toplevel). Please change all your function names. Also, in your updateipt function, you are giving a function to place the Text widget so it throws an error.

import os
import subprocess as sub
from tkinter import *
from subprocess import Popen, PIPE


def main_windows():
    global main_window
    main_window = Tk()
    main_window.title("Firewall test")
    main_window.geometry("1000x700")
    main_window.configure(bg="dark grey")
    label1 = Label(main_window, text="What Would you like to configure?", font=('arial', 12, 'bold'), bg="white",
                   fg="black")
    label1.pack(fill=X, pady=20)

    btnproc = Button(main_window, text="Process configuration", font=('arial', 9, 'bold'), bg="light grey", fg="black",
                     width="30", height="2", command=lambda: process_windows())
    btnproc.pack(pady=20)
    btnfw = Button(main_window, text="Firewall", font=('arial', 9, 'bold'), bg="light grey", fg="black", width="30",
                   height="2", command=lambda: firewall_windows())
    btnfw.pack(pady=20)
    btnmon = Button(main_window, text="AIDE", font=('arial', 9, 'bold'), bg="light grey", fg="black", width="30",
                    height="2", command=lambda: monitor_windows())
    btnmon.pack(pady=20)

    main_window.mainloop()


def process_windows():
    global process_window
    process_window = Toplevel()
    process_window.title("Process Configuration")
    process_window.geometry("800x600")
    process_window.configure(bg="dark grey")
    label1 = Label(process_window, text="What Would you like to configure?", font=('arial', 12, 'bold'), bg="white",
                   fg="black")
    label1.pack(fill=X, pady=20)



def firewall_windows():
    firewall_window = Toplevel()
    firewall_window.title("Firewall Configuration")
    firewall_window.geometry("1000x600")
    firewall_window.configure(bg="dark grey")
    label1 = Label(firewall_window, text="What Would you like to configure?", font=('arial', 12, 'bold'), bg="white",
                   fg="black")
    label1.pack(fill=X, pady=20)

    updatebtn = Button(firewall_window, text="View Current Iptables layout", font=('arial', 9, 'bold'), bg="light grey",
                       fg="black", width="30",
                       height="2", command=lambda: updateipt())
    updatebtn.pack(pady=20)


def monitor_windows():
    monitor_window = Toplevel(main_window)
    monitor_window.title("AIDE Configuration")
    monitor_window.geometry("800x600")
    monitor_window.configure(bg="dark grey")
    label1 = Label(monitor_window, text="What Would you like to configure?", font=('arial', 12, 'bold'), bg="white",
                   fg="black")
    label1.pack(fill=X, pady=20)


def updateipt():
    text = Text(main_window, height=150, width=124)
    text.pack()
    with Popen(["iptables", '-L', '-v', '-n'], stdout=PIPE, stderr=PIPE) as p:
        for line in p.stdout:
            text.insert(END, line)

    



main_windows()