0

I am trying tkinter for the first time. I want to align all the input box and the contents to the middle of the dialogue box.Currently, it is in the left side. I want the whole thing in the image to be at the center from top and bottom as well.

Here is the image of the output I am currently getting.

enter image description here.

if __name__ == "__main__":
    _root = Tk()

    style = ttk.Style()
    style.configure("TButton", foreground="black", background="seashell3")
    _root.title('Image Compression using K-means Clustering')
    _root.resizable(width=TRUE, height=TRUE)

    _mainframe = ttk.Frame(_root, padding='5 5 5 5')
    _mainframe.grid(row=0, column=0, sticky=(E, W, N, S))

    _url_frame = ttk.LabelFrame(_mainframe, text='URL', padding='5 5 5 5')
    _url_frame.grid(row=0, column=0, sticky=(E, W),columnspan=2)
    _url_frame.columnconfigure(0, weight=1)
    _url_frame.rowconfigure(0, weight=1)

    _url = StringVar()
    _url.set('http://')
    _url_entry = ttk.Entry(_url_frame, width=100, textvariable=_url)
    _url_entry.grid(row=0, column=0, sticky=(E, W, S, N), padx=5)
    _fetch_btn = ttk.Button(_url_frame, text='Fetch info from URL', command=fetch_url)

    _fetch_btn.grid(row=0, column=1, sticky=W, padx=5)

    _img_frame = ttk.LabelFrame(_mainframe, text='Content', padding='9 0 0 0')
    _img_frame.grid(row=1, column=0, sticky=(N, S, E, W),columnspan=2)
    _images = StringVar()
    _img_listbox = Listbox(_img_frame, listvariable=_images, height=6, width=100, selectmode='multiple')
    _img_listbox.grid(row=0, column=0, sticky=(E, W), pady=5)
    _img_listbox.bind('<<ListboxSelect>>', fetch_selected_images)

    _scrollbar = ttk.Scrollbar(_img_frame, orient=VERTICAL, command=_img_listbox.yview)
    _scrollbar.grid(row=0, column=1, sticky=(S, N), pady=6)
    _img_listbox.configure(yscrollcommand=_scrollbar.set)

    _radio_frame = ttk.Frame(_img_frame)
    _radio_frame.grid(row=0, column=2, sticky=(N, S, W, E))

    _choice_lbl = ttk.Label(_radio_frame, text='')
    _choice_lbl.grid(row=0, column=0, padx=5, pady=5)
    _save_method = StringVar()
    _save_method.set('img')

    _img_only_radio = ttk.Radiobutton(_radio_frame, text='As Images', variable=_save_method, value='img')
    _img_only_radio.grid(row=1, column=0, padx=5, pady=2, sticky=W)
    _img_only_radio.configure(state='normal')

    _scrape_btn = ttk.Button(_mainframe, text='Scrape!', command=save,style='TButton')
    _scrape_btn.grid(row=2, column=0, sticky=(N,E), pady=2)

    _compress_btn = ttk.Button(_mainframe, text='Compress!', command=compress)
    _compress_btn.grid(row=2, column=1, sticky=W, pady=2)

    _status_frame = ttk.Frame(_root, relief='sunken', padding='2 2 2 2')
    _status_frame.grid(row=1, column=0, sticky=(E, W, S))
    _status_msg = StringVar()
    _status_msg.set('Type a URL to start scraping...')
    _status = ttk.Label(_status_frame, textvariable=_status_msg, anchor=W)
    _status.grid(row=0, column=0, sticky=(E, W))

    # context menu

    _menubar = Menu(_root)
    filemenu = Menu(_menubar, tearoff=0)
    filemenu.add_command(label='Fetch images from URL', command=fetch_url)
    filemenu.add_command(label='Scrape images', command=save)
    filemenu.add_command(label="Compress image", command=compress)
    filemenu.add_separator()
Bussller
  • 1,961
  • 6
  • 36
  • 50
  • 1
    I don't think `tkinter` supports what you're looking for. You could render text centered, but having an input box with centered text (that stays centered during input) may not be (easily) possible. – NotAnAmbiTurner Nov 18 '18 at 09:07

1 Answers1

0

After you create the root window you must tell it how to react to size changes:

_root.columnconfigure(0, weight=1)
_root.rowconfigure(0, weight=1)

which means to expand row 0 and column 0 with the window.

Then to center the _mainframe just put it in the window without any sticky:

_mainframe.grid(row=0, column=0)
figbeam
  • 7,001
  • 2
  • 12
  • 18