我是第一次尝试tkinter。我想将所有输入框和内容对齐到对话框的中间,当前在左侧。我也希望图像中的整个内容都位于顶部和底部的中心。
这是我当前得到的输出的图像。
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()
答案 0 :(得分:0)
创建根窗口后,必须告诉它如何对大小变化做出反应:
_root.columnconfigure(0, weight=1)
_root.rowconfigure(0, weight=1)
表示使用窗口扩展第0行和第0列。
然后将_mainframe
居中放置在窗口中,不会发粘:
_mainframe.grid(row=0, column=0)