반응형
tkinter에서 캔버스를 가져 와서 너비를 창의 너비로 설정 한 다음 사용자가 창을 더 작게 / 더 크게 만들 때 캔버스의 크기를 동적으로 조정해야합니다.
(쉽게) 할 수있는 방법이 있습니까?
해결 방법
이렇게하려면 scale
메서드를 사용하고 모든 위젯에 태그를 지정해야합니다. 완전한 예는 다음과 같습니다.
from Tkinter import *
# a subclass of Canvas for dealing with resizing of windows
class ResizingCanvas(Canvas):
def __init__(self,parent,**kwargs):
Canvas.__init__(self,parent,**kwargs)
self.bind("<Configure>", self.on_resize)
self.height = self.winfo_reqheight()
self.width = self.winfo_reqwidth()
def on_resize(self,event):
# determine the ratio of old width/height to new width/height
wscale = float(event.width)/self.width
hscale = float(event.height)/self.height
self.width = event.width
self.height = event.height
# resize the canvas
self.config(width=self.width, height=self.height)
# rescale all the objects tagged with the "all" tag
self.scale("all",0,0,wscale,hscale)
def main():
root = Tk()
myframe = Frame(root)
myframe.pack(fill=BOTH, expand=YES)
mycanvas = ResizingCanvas(myframe,width=850, height=400, bg="red", highlightthickness=0)
mycanvas.pack(fill=BOTH, expand=YES)
# add some widgets to the canvas
mycanvas.create_line(0, 0, 200, 100)
mycanvas.create_line(0, 100, 200, 0, fill="red", dash=(4, 4))
mycanvas.create_rectangle(50, 25, 150, 75, fill="blue")
# tag all of the drawn widgets
mycanvas.addtag_all("all")
root.mainloop()
if __name__ == "__main__":
main()
참조 페이지 https://stackoverflow.com/questions/22835289
반응형
'파이썬' 카테고리의 다른 글
파이썬 Python 함수 포인터 (0) | 2020.12.17 |
---|---|
파이썬-목록에없는 경우 (0) | 2020.12.17 |
파이썬 범위 내에서 'n'개의 고유 한 난수 생성 (0) | 2020.12.17 |
파이썬 numpy / scipy의 제곱 차이 합계 (SSD) (0) | 2020.12.17 |
파이썬 (python) [Errno 11001] getaddrinfo 실패 (0) | 2020.12.17 |
댓글