如何同时使用图形模块和Tkinter模块?

时间:2015-11-29 02:06:20

标签: python tkinter

from Tkinter import *
root = Tk()
root.title("hello world")
root.geometry('300x200')
root.mainloop()

我想在Tkinter中使用一些功能,而我熟悉'图形'。 如何在此Tkinter窗口中使用图形功能?

这是图形的定义。

# Graphics classes start here

class GraphWin(tk.Canvas):

    """A GraphWin is a toplevel window for displaying graphics."""

    def __init__(self, title="Graphics Window",
                 width=200, height=200, autoflush=True):
        master = tk.Toplevel(_root)
        master.protocol("WM_DELETE_WINDOW", self.close)
        tk.Canvas.__init__(self, master, width=width, height=height)
        self.master.title(title)
        self.pack()
        master.resizable(0,0)
        self.foreground = "black"
        self.items = []
        self.mouseX = None
        self.mouseY = None
        self.bind("<Button-1>", self._onClick)
        self.bind_all("<Key>", self._onKey)
        self.height = height
        self.width = width
        self.autoflush = autoflush
        self._mouseCallback = None
        self.trans = None
        self.closed = False
        master.lift()
        self.lastKey = ""
        if autoflush: _root.update()

http://mcsp.wartburg.edu/zelle/python/graphics.py

上有更多内容

1 个答案:

答案 0 :(得分:0)

graphics.py库将GraphWin对象打包为顶层窗口。如果你想将GraphWin集成到带有其他框架和wigets的tkinter窗口中,你需要修改graphics.py库并删除GraphWin类周围的顶层包装器,以便可以将其视为tkinter Canvas。这意味着您需要在源文件中附加修改后的库,或者重命名并适当导入它。

以下是一个如何在App中完成的示例,该应用程序在一个名为“root”的根窗口中集成了Graphwin和另一个tk.Frame。我不会在这里包含整个修改过的库,只包括修改后的GraphWin init 方法,但是注意 - 仅此一项是不够的 - 您将需要完成GraphWin类的其余部分并且所有您要使用的库中的类和方法; Point,Oval等..每次你将“_root”改为“self.master”时都会改变,而且每次你​​读“master”时都应该是“self.master”,任何“self.update()”应该是“self.master.update()”也是。

您可以决定删除update()方法,因为您现在可以使用自己的self.pauseLength = time.sleep(pauseLength)从yourApp类内部调用self.root.update()。你的app可能会调用你的图形类方法中所有出现的_root.update()都会改为self.master.update(),所以你会看到在修改后的GraphWin类中,self.master被赋值参数root,现在必须在实例化GraphWin时提供,如此示例实现中那样。

###################### SAMPLE IMPLEMENTATION #######################

import tkinter as tk

class yourApp:
     def __init__(self, yourParameters):
          self.root= tk.Tk()
          self.canvas = GraphWin(self.root, yourParameters)
          self.canvas.pack
          self.frameOfWidgets = tk.Frame(self.root, yourParameters)
          self.frameOfWidgets.pack()

App = yourApp(yourParameters)
App.root.mainloop()

################## MODIFIED GRAPHWIN CLASS #######################


class GraphWin(tk.Canvas):

    """A GraphWin is no longer in a toplevel wrapper and can be treated like a tk.Canvas in the parent window called root."""`

    def __init__(self, root, title="Graphics Window",
                 width=200, height=200, borderwidth=0, autoflush=True):
        self.master = root
        self.master.protocol("WM_DELETE_WINDOW", self.close)
        tk.Canvas.__init__(self, self.master, width=width, height=height, borderwidth=borderwidth)
        self.master.title(title)
        self.pack()
        self.master.resizable(0, 0)
        self.foreground = "black"
        self.items = []
        self.mouseX = None
        self.mouseY = None
        self.bind("<Button-1>", self._onClick)
        self.bind_all("<Key>", self._onKey)
        self.height = height
        self.width = width
        self.autoflush = autoflush
        self._mouseCallback = None
        self.trans = None
        self.closed = False
        self.lastKey = ""