简体   繁体   English

使用Tkinter将内容与中心对齐

[英]Align the content to the center using Tkinter

I am trying tkinter for the first time. 我是第一次尝试tkinter。 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. 这是我当前得到的输出图像。

在此处输入图片说明 .

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()

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. 这意味着用窗口扩展行0和列0。

Then to center the _mainframe just put it in the window without any sticky: 然后将_mainframe放置在窗口中,没有任何粘滞:

_mainframe.grid(row=0, column=0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM