首先,我是一名法国学生,所以我不能说真正的英语。
尝试根据画布上的文字数量调整画布的滚动条时,我遇到了问题。
如果画布中的文字对于屏幕而言太大,我需要滚动条。
from tkinter import *
from tkinter.filedialog import *
from tkinter.messagebox import *
from tkinter.scrolledtext import *
def writeincanvas():
canvas.insert(canvas_id, 5000, "Hi !" + "\n")
win_crypt = Tk()
win_crypt.title("Canvas adjustable")
canvas = Canvas(win_crypt, width=960, background="white", scrollregion=(0,0,5000,5000))
### here is "scrollregion=(0,0,5000,50000)" but I don't
### need that because I need auto-adjustable**
scroll = Scrollbar(win_crypt, orient=VERTICAL)
scroll.pack(side=RIGHT, fill=Y)
scroll.config(command=canvas.yview)
canvas.config(width=960, yscrollcommand=scroll.set)
canvas.pack(side=RIGHT, fill=BOTH)
canvas_id = canvas.create_text(10, 10, anchor="nw")
Label(win_crypt, text="Clic on the button :").pack(pady=20)
Button(win_crypt, text="Press !", command=writeincanvas).pack(pady=20)
win_crypt.mainloop()
答案 0 :(得分:0)
canvas方法bbox
返回作为参数传入的项目或项目的边界框的坐标。如果传入特殊参数"all'
,它将返回适合所有内容的最小可能矩形的坐标。将项目添加到画布后,可以将此值用于scrollregion
属性。
def writeincanvas():
canvas.insert(canvas_id, 5000, "Hi !" + "\n")
canvas.configure(scrollregion=canvas.bbox("all"))