Tkinter - 使第二个画布显示另一个画布的内容

发布于 2024-12-02 02:30:41 字数 297 浏览 1 评论 0原文

我正在寻找一种方法,使一个画布显示另一个画布的内容,而不复制其中绘制的对象。第二个画布只需作为一个显示,它将第一个画布上绘制的内容作为输入。

我知道 .postscript 方法将内容写入文件或变量中,但我不知道如何(如果可能)使另一个画布将其显示为图像。

编辑:我忘记说了,我的操作系统是windows。如果有人也能告诉我一个跨平台的解决方案,那就太好了。

Edit2:我不想通过复制对象来做到这一点的原因是因为它们正在移动并且在某个时间间隔其中一些被破坏并创建了新的对象。而且它是一个实时系统,复制对象及其行为可能会减慢速度。

I'm looking for a way to make one canvas display the contents of another WITHOUT duplicating the objects that are drawn in it. The second canvas needs to be just a display which takes as input what is drawn on the first one.

I know that the .postscript method writes the contents in a file or a variable, but I don't know how(if it's possible) to make another canvas display it as an image.

Edit: I forgot to mention, my operating system is windows. It would be great if someone can tell me a cross-platform solution also.

Edit2: the reason I don't want to do it by duplicating the objects is because they are moving and at some interval some of them are destroyed and new ones are created. Also it's a real-time system and duplicating the objects with their behaviors might slow things down.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷雨优美回忆 2024-12-09 02:30:41

没有内置的方法可以做到这一点。画布不支持任何类型的对等或复制。但是,如果您没有任何嵌入的小部件,则将所有对象的副本保留在第二个画布中非常容易且快速。

一个简单的方法是对画布小部件进行子类化,然后创建draw_line、draw_oval、coords 等的新实现以在对等画布上进行绘制。以下示例展示了如何以这种方式协调三个画布,其中 1000 个对象以不同的速率向下移动:

import Tkinter as tk
import random

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.canvas1 = PeeredCanvas(self, width=300, height=300, border=1, relief="sunken")
        self.canvas2 = PeeredCanvas(self, width=300, height=300, border=1, relief="sunken")
        self.canvas3 = PeeredCanvas(self, width=300, height=300, border=1, relief="sunken")
        self.canvas1.add_peer(self.canvas2)
        self.canvas1.add_peer(self.canvas3)
        toolbar = tk.Frame(self)
        clear_button = tk.Button(self, text="Refresh", command=self.refresh)
        clear_button.pack(in_=toolbar, side="left")
        toolbar.pack(side="top", fill="x")
        self.canvas1.pack(side="left", fill="both", expand=True)
        self.canvas2.pack(side="left", fill="both", expand=True)
        self.canvas3.pack(side="left", fill="both", expand=True)
        self.animate(10)

    def animate(self, delay):
        '''Move all items down at a random rate'''
        for item in self.canvas1.find_all():
            delta_x = 0
            delta_y = random.randrange(1, 4)
            self.canvas1.move(item, delta_x, delta_y)
        self.after(delay, self.animate, delay)

    def refresh(self, count=100):
        '''Redraw 'count' random circles'''
        self.canvas1.delete("all")
        width=self.canvas1.winfo_width()
        height=self.canvas1.winfo_height()
        for i in range(count):
            if i%2 == 0:
                tags = ("even",)
            else:
                tags = ("odd",)
            x = random.randrange(10, width-10)
            y = random.randrange(10, height-10)
            radius = random.randrange(10,100, 10)/2
            self.canvas1.create_oval([x,y, x+radius, y+radius], tags=tags)
        self.canvas1.itemconfigure("even", fill="red", outline="white")
        self.canvas1.itemconfigure("odd", fill="white", outline="red")

class PeeredCanvas(tk.Canvas):
    '''A class that duplicates all objects on one or more peer canvases'''
    def __init__(self, *args, **kwargs):
        self.peers = []
        tk.Canvas.__init__(self, *args, **kwargs)

    def add_peer(self, peer):
        if self.peers is None:
            self.peers = []
        self.peers.append(peer)

    def move(self, *args, **kwargs):
        tk.Canvas.move(self, *args, **kwargs)
        for peer in self.peers:
            peer.move(*args, **kwargs)

    def itemconfigure(self, *args, **kwargs):
        tk.Canvas.itemconfigure(self, *args, **kwargs)
        for peer in self.peers:
            peer.itemconfigure(*args, **kwargs)

    def delete(self, *args, **kwargs):
        tk.Canvas.delete(self, *args)
        for peer in self.peers:
            peer.delete(*args)

    def create_oval(self, *args, **kwargs):
        tk.Canvas.create_oval(self, *args, **kwargs)
        for peer in self.peers:
            peer.create_oval(*args, **kwargs)


app = SampleApp()
app.mainloop()

There is no built-in way to do this. The canvas doesn't support any sort of peering or duplication. However, keeping copies of all the objects in a second canvas is pretty easy and fast if you don't have any embedded widgets.

A simple way to do this is to subclass the canvas widget, then create new implementations of draw_line, draw_oval, coords, etc to draw on a peer canvas. The following example shows how to coordinate three canvases in this manner, with 1000 objects that are moving downward at different rates:

import Tkinter as tk
import random

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.canvas1 = PeeredCanvas(self, width=300, height=300, border=1, relief="sunken")
        self.canvas2 = PeeredCanvas(self, width=300, height=300, border=1, relief="sunken")
        self.canvas3 = PeeredCanvas(self, width=300, height=300, border=1, relief="sunken")
        self.canvas1.add_peer(self.canvas2)
        self.canvas1.add_peer(self.canvas3)
        toolbar = tk.Frame(self)
        clear_button = tk.Button(self, text="Refresh", command=self.refresh)
        clear_button.pack(in_=toolbar, side="left")
        toolbar.pack(side="top", fill="x")
        self.canvas1.pack(side="left", fill="both", expand=True)
        self.canvas2.pack(side="left", fill="both", expand=True)
        self.canvas3.pack(side="left", fill="both", expand=True)
        self.animate(10)

    def animate(self, delay):
        '''Move all items down at a random rate'''
        for item in self.canvas1.find_all():
            delta_x = 0
            delta_y = random.randrange(1, 4)
            self.canvas1.move(item, delta_x, delta_y)
        self.after(delay, self.animate, delay)

    def refresh(self, count=100):
        '''Redraw 'count' random circles'''
        self.canvas1.delete("all")
        width=self.canvas1.winfo_width()
        height=self.canvas1.winfo_height()
        for i in range(count):
            if i%2 == 0:
                tags = ("even",)
            else:
                tags = ("odd",)
            x = random.randrange(10, width-10)
            y = random.randrange(10, height-10)
            radius = random.randrange(10,100, 10)/2
            self.canvas1.create_oval([x,y, x+radius, y+radius], tags=tags)
        self.canvas1.itemconfigure("even", fill="red", outline="white")
        self.canvas1.itemconfigure("odd", fill="white", outline="red")

class PeeredCanvas(tk.Canvas):
    '''A class that duplicates all objects on one or more peer canvases'''
    def __init__(self, *args, **kwargs):
        self.peers = []
        tk.Canvas.__init__(self, *args, **kwargs)

    def add_peer(self, peer):
        if self.peers is None:
            self.peers = []
        self.peers.append(peer)

    def move(self, *args, **kwargs):
        tk.Canvas.move(self, *args, **kwargs)
        for peer in self.peers:
            peer.move(*args, **kwargs)

    def itemconfigure(self, *args, **kwargs):
        tk.Canvas.itemconfigure(self, *args, **kwargs)
        for peer in self.peers:
            peer.itemconfigure(*args, **kwargs)

    def delete(self, *args, **kwargs):
        tk.Canvas.delete(self, *args)
        for peer in self.peers:
            peer.delete(*args)

    def create_oval(self, *args, **kwargs):
        tk.Canvas.create_oval(self, *args, **kwargs)
        for peer in self.peers:
            peer.create_oval(*args, **kwargs)


app = SampleApp()
app.mainloop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文