How set TreeCtrl width

I put my TreeCtrl into a Sizer but I can’t seem to set the width of the tree.

Any help appreciated.

import wx
import os

class MainFrame(wx.Frame):
    def __init__(self, *args, **kw):
        super(MainFrame, self).__init__(*args, **kw)

def traverse_directory_tree(parent, path):
    global glist_tree_node_id
    global glist_tree_filenames
    gstr_show_folders = '0'
    if gstr_show_folders == '1':
        for folder_name in filter(os.path.isdir, os.listdir(os.getcwd())):
            # full_path = os.path.join(path, d)
            isdir = os.path.isdir(path)
            if isdir:
                if not folder_name.startswith('.'):
                    wx.tree.AppendItem(parent, folder_name)
    for fn in os.listdir(path):
            if fn.endswith('.py'):
                if ".bak" in fn:
                    pass
                else:
                    id2 = wx.tree.AppendItem(parent, fn)

def main():
    app = wx.App()
    win = MainFrame(None, title='Tree Demo')
    win.SetPosition(wx.Point(900, 400))

    wx.tree = wx.TreeCtrl(win, wx.ID_ANY)
    default_path = r'C:\Users\jsmith\Documents'
    root = wx.tree.AddRoot(default_path)
    traverse_directory_tree(root, default_path)
    wx.tree.ExpandAll()

    sizer = wx.BoxSizer(wx.HORIZONTAL)
    wx.tree.SetSize((300, 300))
    sizer.Add(wx.tree, 0, wx.EXPAND)
    win.SetSizer(sizer)

    win.Show()
    app.MainLoop()

if __name__ == '__main__':
    main()

Use SetMinSize and SetMaxSize.
Or pass the size to the widget initialization function:
wx.TreeCtrl(win, wx.ID_ANY, size=(300, 300))

P.S.:
wx.tree = wx.TreeCtrl(win, wx.ID_ANY) ? wx.tree???

P.P.S:
Widgets need to be defined in the MainFrame class. See examples. Using global variables is bad!

Thank you for the tip regarding widgets being defined in the MainFrame class.

I was finally able to get the tree size width to work with the sizer.

import wx
import os

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, parent=None, size=wx.Size(600, 400), pos=wx.Point(-900, 400), title='TreeCtrl Demo')
        #                                                     x    y
        self.tree = wx.TreeCtrl(self, wx.ID_ANY, size=wx.Size(240, 400), style=wx.TR_FULL_ROW_HIGHLIGHT | wx.TR_HAS_BUTTONS | wx.TR_NO_LINES)

        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(self.tree,
                1) # make vertically stretchable
        self.SetSizer(vbox)

        default_path = r'C:\Users\johnsmith\Documents'
        self.root = self.tree.AddRoot(default_path)
        traverse_directory_tree(self.root, default_path, self.tree)
        self.tree.ExpandAll()

        self.Show()

def traverse_directory_tree(parent, path, tree):
    global glist_tree_node_id
    global glist_tree_filenames
    gstr_show_folders = '0'
    if gstr_show_folders == '1':
        for folder_name in filter(os.path.isdir, os.listdir(os.getcwd())):
            # full_path = os.path.join(path, d)
            isdir = os.path.isdir(path)
            if isdir:
                if not folder_name.startswith('.'):
                    tree.AppendItem(parent, folder_name)
    for fn in os.listdir(path):
            if fn.endswith('.py'):
                if ".bak" in fn:
                    pass
                else:
                    id2 = tree.AppendItem(parent, fn)

if __name__ == '__main__':
    app = wx.App()
    main_frame = MainFrame()
    app.MainLoop()