Meghan M.
Meghan M.

Reputation: 309

tkinter, python: Button not showing up after Listbox, and Listbox not responding at all

I'm a tkinter newb, making slow progress. This question is about my parse_html func but I included relevant main code too. I can't figure out why the OK button isn't even showing up after the Listbox. While I do have a correct list of options showing up in my Listbox, and retval prints out the correct value, I'm stuck at this point because nothing is happening in the lbox section, and the ok_btn button is missing. I'm not even sure if one is causing the other.
Any help would truly be appreciated. Thank you!

Updates: Noticed the banner Label also disappears when the Listbox is put in. Added function get_html_source_dirs to code below, just in case it's part of the problem.

Resolved: Added a max_len of choices to use as width of Listbox per Answer below. My problem was a symptom of Resizing tkinter listbox to width of largest item, using .grid

    choices = StringVar(value=get_html_source_dirs())
    max_len = max([len(c) for c in get_html_source_dirs()])
    lbox = Listbox(master=new, listvariable=choices, width=max_len+2, selectmode=SINGLE)
 
## `test_write` is a decorator, just substitute `print`. Sorry... 
def parse_html(win):

    def get_html_source_dirs():
        ''' CACHED_DIR and CACHE_ARCHIVES are WindowsPath type'''
        dirs = [CACHE_DIR.stem] 
        dirs.extend([name for name in os.listdir(CACHE_ARCHIVES) if (CACHE_ARCHIVES/name).is_dir()])
        return dirs

    def on_selection(event):
        retval = [lbox.get(index) for index in lbox.curselection()]
        # test_write(f"{retval}")
        ## ?? Assume I only need the one value for selectmode=SINGLE??
        return retval[0] 
  
    def click_ok(event):
        ''' Will launch the parser with the selected directory as the argument '''
        pass 

    new = Toplevel(win)
    new.geometry("450x300")
    new.title("Parse HTML")
    banner = Label(master=new, text="Select source directory and click OK")
    banner.grid(row=0, column=1)
    choices = StringVar(value=get_html_source_dirs()) ## There are fewer than 10 options
    lbox = Listbox(master=new, listvariable=choices, width=400, selectmode=SINGLE)
    lbox.grid(row=2, column=1)
    lbox.selection_set(0) # Default setting
    ## ?? Not sure if '<ButtonRelease>' or '<<ListboxSelection>>' is more correct??
    lbox.bind('<ButtonRelease>', on_selection)
    test_write(f"{lbox.get(lbox.curselection())}")

    ## ?? Not sure if I need this step??
    # lbox.insert(0, *get_html_files()) 

    ## ?? This print statement only shows the very first time, not even after lbox.bind
    # test_write(f"{lbox.index('active')}")    

    ok_btn = Button(master=new, text='OK')
    ok_btn.grid(row=3, column=1)
    ok_btn.configure(command=lambda: click_ok(win))

    new.mainloop()


def main():
    win = Tk()
    frame1 = Frame(win)
    frame1.pack(fill= BOTH, expand= True, padx= 10, pady=20)

    ... # other buttons

    PARSE_HTML = Label(frame1, text='Process saved files:', padx=10, pady=10)
    PARSE_HTML.grid(row=3, column=0, sticky=W)
    process_button = Button(frame1, text='PROCESS HTML', padx=10, pady=5)
    process_button.grid(row=3, column=1)
    process_button.configure(command=lambda: parse_html(win))

    ... # more buttons

    win.mainloop()


if __name__=="__main__":
    main()

enter image description here

Upvotes: 0

Views: 176

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385980

The problem is that you're setting the listbox width to 400. That's 400 characters, not pixels. On my machine that makes it about 3600 pixels wide. Since you don't use any other options when calling grid, the label and button will be centered, meaning they will be roughly at pixel position 1800.

Change the width to something more sane and the label and button should appear.

lbox = Listbox(master=new, listvariable=choices, width=40, selectmode=SINGLE)
#                                                      ^^

Upvotes: 1

Related Questions