我剛剛開始使用 Tkinter,并且我有一個使用 create_window 函式放置的畫布的框架 insdie,以便我可以為我的程式提供一個作業滾動條。問題是我放置在畫布內部框架中的文本小部件沒有在畫布內水平跨越,我不知道如何實作這一點。我曾嘗試使用 pack() 或 grid() 和 grid_rowconfigure,但如果我不使用 create_window,那么滾動條上的欄就會消失。我似乎找不到任何解決此問題的帖子,但我可能錯了。這是我處理此視窗 GUI 的代碼片段:
root = Tk()
# create top part
top=Frame(root)
# create a canvas to scroll
holder = Canvas(top)
# create scroll bar
scroll = Scrollbar(top, orient=VERTICAL, command=holder.yview)
# configure scrollbar for canvas
holder.configure(yscrollcommand=scroll.set)
holder.bind('<Configure>', lambda e: holder.configure(scrollregion=holder.bbox("all")))
# create frame for content inside canvas and add it to the canvas
content = Frame(holder, relief=RAISED)
holder.create_window((0,0), window=content, anchor='nw')
# create bottom part
bottom = Frame(root, relief=SUNKEN)
root.rowconfigure(0, weight=18)
root.rowconfigure(1, weight=1, minsize=50)
root.columnconfigure(0, weight=1)
holder.pack(side=LEFT, fill=BOTH, expand=1)
scroll.pack(side=RIGHT, fill=Y)
top.grid(row=0, column=0, sticky='NSEW')
bottom.grid(row=1, column=0, sticky='NSEW')
num=0
for site in sites:
temp=Text(content, height = 5)
temp.configure(state= DISABLED)
temp.pack(fill=X, side=TOP, padx= 5, pady= 5)
siteBoxes.append(temp)
num = 1
root.mainloop()
uj5u.com熱心網友回復:
首先,您應該更新內部框架(而scrollregion不是畫布)的大小。holdercontentholder
content其次,您可以在調整畫布holder大小時更新內部框架的寬度。
以下是更新后的代碼:
root = Tk()
# create top part
top=Frame(root)
# create a canvas to scroll
holder = Canvas(top)
# create scroll bar
scroll = Scrollbar(top, orient=VERTICAL, command=holder.yview)
# configure scrollbar for canvas
holder.configure(yscrollcommand=scroll.set)
### --- expand the width of the internal frame to the width of canvas
holder.bind('<Configure>', lambda e: holder.itemconfigure(internal, width=e.width))
# create frame for content inside canvas and add it to the canvas
content = Frame(holder, relief=RAISED)
### --- update scrollregion whenever the internal frame is resized
content.bind('<Configure>', lambda e: holder.configure(scrollregion=holder.bbox("all")))
### --- save the item ID of the internal frame
internal = holder.create_window((0,0), window=content, anchor='nw')
# create bottom part
bottom = Frame(root, relief=SUNKEN)
root.rowconfigure(0, weight=18)
root.rowconfigure(1, weight=1, minsize=50)
root.columnconfigure(0, weight=1)
holder.pack(side=LEFT, fill=BOTH, expand=1)
scroll.pack(side=RIGHT, fill=Y)
top.grid(row=0, column=0, sticky='NSEW')
bottom.grid(row=1, column=0, sticky='NSEW')
num=0
for site in sites:
temp=Text(content, height = 5)
temp.configure(state= DISABLED)
temp.pack(fill=X, side=TOP, padx= 5, pady= 5)
siteBoxes.append(temp)
num = 1
root.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/506296.html
下一篇:將物件值設定為物件鍵
