如何使网格系统中的画布占据前面的列

时间:2019-03-30 14:03:57

标签: python tkinter tkinter-canvas

我正在尝试使用URL列表建立一些超链接。为此,我对画布中的每个URL超链接使用canvas.create_window()并将该画布放入网格列1中,使其位于窗口的中心。

def display_result(link_url_list, headline_text_list):
    result_window = Tk()
    result_window.title("Filter result")
    result_window.geometry("800x800")
    canvas = Canvas(result_window)
    ver_bar = Scrollbar(result_window, orient='vertical', command=canvas.yview)
    hor_bar = Scrollbar(result_window, orient='horizontal', command=canvas.xview)

    for i in range(len(headline_text_list)):
        hyperlink = Label(canvas, text=headline_text_list[i], font="Helvetica 10 bold", fg="blue", cursor="hand2")
        article_url = link_url_list[i]
        hyperlink.bind("<Button-1>", lambda event, article_url=article_url: webbrowser.open(article_url))
        hyperlink.pack()
        canvas.create_window((0, i * 30), anchor='center', window=hyperlink, height=20)
    canvas.configure(scrollregion=canvas.bbox('all'), yscrollcommand=ver_bar.set)
    canvas.configure(scrollregion=canvas.bbox('all'), xscrollcommand=hor_bar.set)

    result_window.grid_columnconfigure(0, weight=1)
    result_window.grid_columnconfigure(1, weight=1)
    result_window.grid_columnconfigure(2, weight=1)
    result_window.grid_rowconfigure(0, weight=1)
    result_window.grid_rowconfigure(1, weight=1)

    canvas.grid(row=0, column=1, columnspan=3, rowspan=2, sticky="nsew")
    hor_bar.grid(row=1, column=0, columnspan=3, sticky=E+W+S)
    ver_bar.grid(row=0, column=2, rowspan=2, sticky=N+S+E)

但是,结果画布没有占据第0列,并且第1列中长句子中的某些单词“隐藏”在第0列的空白后面。我知道在回答的问题中

How to allow a widget in Tkinter to span multiple columns going backwards?

说放置小部件 在第0列中将其向前扩展与向后扩展具有相同的效果,但是当我尝试将其固定在窗口的左端时,并且由于调整窗口大小而不会扩展。 (这是可以理解的,因为画布位于窗口的最左端第0列)

我也尝试过

    def on_canvas_configure(event):
        canvas.configure(width=event.width, height=event.height)


    canvas.bind("<Configure>", on_canvas_configure)

但是这也不能解决。我该如何解决这个问题?

0 个答案:

没有答案