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

Meghan M.

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

Bryan Oakley

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)
#                                                      ^^

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive