따라서 캔버스 크기에 맞게 자동으로 업데이트되는 창에 세로 및 가로 스크롤 막대를 갖고 싶습니다. 나는 그것을 부분적으로 얻었고 지금 코드는 각 업데이트에 대해 추가 스크롤 막대를 만들고 가로 스크롤 막대는 전체 캔버스의 길이에 걸쳐 있지 않습니다. 이를 설명하기 위해 만든 샘플은 다음과 같습니다.
import tkinter
from tkinter import ttk
root = tkinter.Tk()
root.state("zoomed")
bigframe = tkinter.Frame(root)
bigframe.pack(fill="both", expand=1)
canvas = tkinter.Canvas(bigframe)
canvas.pack(side="left", fill="both", expand=1)
scrollbarv = ttk.Scrollbar(bigframe, orient="vertical", command=canvas.yview)
scrollbarh = ttk.Scrollbar(bigframe, orient="horizontal", command=canvas.xview)
scrollbarv.pack(side="right", fill="y")
scrollbarh.pack(side="bottom", fill="x")
canvas.configure(yscrollcommand=scrollbarv.set, xscrollcommand=scrollbarh.set)
canvas.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all")))
mainframe = tkinter.Frame(canvas)
canvas.create_window((0, 0), window=mainframe, anchor="nw")
i = 1
text = "Hello World! "
def click():
global i
global text
for t in range(10):
i += 1
text += "Hello World! "
tkinter.Label(mainframe, text=text).pack()
scrollbar = ttk.Scrollbar(bigframe, orient="vertical", command=canvas.yview)
scrollbar.pack(side="right", fill="y")
button = tkinter.Button(mainframe, text="Click me!", command=click)
button.pack()
root.mainloop()
I believe the horizontal scrollbar being small is due to me saying canvas.pack(side="left", fill="both", expand=1)
, but if I say side="top
then the vertical scroll bars don't span the height of the canvas. Is there some specific piece of code that is causing the multiple scrollbars issue or the horizontal bar issue? And if so, how should I fix it?
You have to destroy the old scrollbar before you add a new one. Just add scrollbar.destroy() to your click function, like I did below:
def click():
global i
global text
for t in range(10):
i += 1
text += "Hello World! "
tkinter.Label(mainframe, text=text).pack()
scrollbar = ttk.Scrollbar(bigframe, orient="vertical", command=canvas.yview)
scrollbar.destroy()
scrollbar.pack(side="right", fill="y")
I made the following changes to your code. At first it doesn't work, however if I resize the window it works perfectly.
import tkinter
from tkinter import ttk
root = tkinter.Tk()
root.state("zoomed")
bigframe = tkinter.Frame(root)
bigframe.pack(fill="both", expand=1)
canvas = tkinter.Canvas(bigframe)
canvas.pack(side="left", fill="both", expand=1)
scrollbarv = ttk.Scrollbar(bigframe, orient="vertical",
command=canvas.yview)
scrollbarh = ttk.Scrollbar(root, orient="horizontal",
command=canvas.xview)
scrollbarv.pack(side="right", fill="y")
scrollbarh.pack(side="bottom", fill="x")
canvas.configure(yscrollcommand=scrollbarv.set,
xscrollcommand=scrollbarh.set)
canvas.bind("<Configure>", lambda e:
canvas.configure(scrollregion=canvas.bbox("all")))
mainframe = tkinter.Frame(canvas)
canvas.create_window((0, 0), window=mainframe, anchor="nw")
i = 1
text = "Hello World! "
def updateScrollRegion():
canvas.update_idletasks()
canvas.config(scrollregion=mainframe.bbox())
def click():
global i, scrollbarv, text
for t in range(10):
i += 1
text += "Hello World! "
tkinter.Label(mainframe, text=text).pack()
button = tkinter.Button(mainframe, text="Click me!", command=click)
button.pack()
root.mainloop()
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다