簡體   English   中英

如何讓 tkinter canvas 動態調整到 window 寬度?

[英]How to get tkinter canvas to dynamically resize to window width?

我需要在 tkinter 中獲取一個 canvas 以將其寬度設置為 window 的寬度,然后在用戶使 window 變小/變大時動態調整 canvas 的大小。

有什么辦法(容易)做到這一點?

我想我會添加一些額外的代碼來擴展@fredtantini 的答案,因為它不涉及如何更新在Canvas上繪制的小部件的形狀。

為此,您需要使用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()

您可以使用.pack幾何管理器:

self.c=Canvas(…)
self.c.pack(fill="both", expand=True)

應該做的伎倆。 如果您的畫布在框架內,請對框架執行相同操作:

self.r = root
self.f = Frame(self.r)
self.f.pack(fill="both", expand=True)
self.c = Canvas(…)
self.c.pack(fill="both", expand=True)

有關更多信息,請參閱effbot

編輯:如果你不想要一個“全尺寸”的畫布,你可以將你的畫布綁定到一個函數:

self.c.bind('<Configure>', self.resize)

def resize(self, event):
    w,h = event.width-100, event.height-100
    self.c.config(width=w, height=h)

再次查看effbot以了解事件和綁定

這樣做 - 至少在我的 Python 版本中。 畫布水平(sticky=E+W & rowconfigure)和垂直(sticky=N+S & columnconfigure)調整大小。 我已將畫布背景設置為令人作嘔的顏色 - 但至少您可以看到它何時起作用。

from tkinter import *
root=Tk()
root.rowconfigure(0,weight=1)
root.columnconfigure(0,weight=1)
canv=Canvas(root, width=600, height=400, bg='#f0f0c0')
canv.grid(row=0, column=0, sticky=N+S+E+W)
root.mainloop()

要調整畫布對象的大小以適應新的窗口大小,只需正確使用 tkinter 幾何管理器。

打包有擴展和填充選項,網格你需要使用畫布母版(包含畫布的小部件)的 columnconfigure 和 rowconfigure 方法,地方管理器有 relwidth 和 relheight 選項。 另一方面,這只會調整畫布尺寸而不更改其對象的尺寸。 要調整畫布對象的大小,您需要使用建議的縮放方法。 另一件需要考慮的事情是一些畫布對象(如文本)不會受到縮放畫布方法的影響。

這是響應代碼稍作更改:

import tkinter as tk


# a subclass of Canvas for dealing with resizing of windows
class ResizingCanvas(tk.Canvas):
    def __init__(self, parent, **kwargs):
        tk.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 = event.width/self.width
        hscale = event.height/self.height
        self.width = event.width
        self.height = event.height
        # rescale all the objects
        self.scale("all", 0, 0, wscale, hscale)


def main():
    root = tk.Tk()
    myframe = tk.Frame(root)
    myframe.pack(fill=tk.BOTH, expand=tk.YES)
    mycanvas = ResizingCanvas(myframe,width=850, height=400, bg="light blue")#, highlightthickness=0)
    mycanvas.pack(fill=tk.BOTH, expand=tk.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
    root.mainloop()

if __name__ == "__main__":
    main()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM