本文整理汇总了Python中nltk.draw.util.CanvasFrame.destroy_widget方法的典型用法代码示例。如果您正苦于以下问题:Python CanvasFrame.destroy_widget方法的具体用法?Python CanvasFrame.destroy_widget怎么用?Python CanvasFrame.destroy_widget使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nltk.draw.util.CanvasFrame
的用法示例。
在下文中一共展示了CanvasFrame.destroy_widget方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: tree_to_ps
# 需要导入模块: from nltk.draw.util import CanvasFrame [as 别名]
# 或者: from nltk.draw.util.CanvasFrame import destroy_widget [as 别名]
def tree_to_ps(s, outfile):
global _canvas_frame
if _canvas_frame is None:
_canvas_frame = CanvasFrame()
# May throw ValueError:
widget = tree_to_widget(s, _canvas_frame.canvas())
_canvas_frame.canvas()['scrollregion'] = (0, 0, 1, 1)
_canvas_frame.add_widget(widget)
_canvas_frame.print_to_file(outfile)
bbox = widget.bbox()
_canvas_frame.destroy_widget(widget)
## Testing..
#for (key, val) in metrics.items():
# print key, '\n ', val
return bbox[2:]
示例2: __init__
# 需要导入模块: from nltk.draw.util import CanvasFrame [as 别名]
# 或者: from nltk.draw.util.CanvasFrame import destroy_widget [as 别名]
class TreeView:
def __init__(self, trees, root=None):
if len(trees) == 0:
print "No trees to display."
return
newroot = False
if root is None:
root = Tkinter.Tk()
window = root
newroot = True
else:
window = Tkinter.Toplevel(root)
window.title("Parse Tree")
window.geometry("600x400")
self.cf = CanvasFrame(window)
self.cf.pack(side='top', expand=1, fill='both')
buttons = Tkinter.Frame(window)
buttons.pack(side='bottom', fill='x')
self.spin = Tkinter.Spinbox(buttons, from_=1, to=len(trees),
command=self.showtree, width=3)
if len(trees) > 1: self.spin.pack(side='left')
self.label = Tkinter.Label(buttons, text="of %d" % len(trees))
if len(trees) > 1: self.label.pack(side='left')
self.done = Tkinter.Button(buttons, text="Done", command=window.destroy)
self.done.pack(side='right')
self.printps = Tkinter.Button(buttons, text="Print to Postscript", command=self.cf.print_to_file)
self.printps.pack(side='right')
self.trees = trees
self.treeWidget = None
self.showtree()
if newroot: root.mainloop()
def showtree(self):
try: n = int(self.spin.get())
except ValueError: n=1
if self.treeWidget is not None: self.cf.destroy_widget(self.treeWidget)
self.treeWidget = TreeWidget(self.cf.canvas(),
self.trees[n-1], draggable=1, shapeable=1)
self.cf.add_widget(self.treeWidget, 0, 0)
示例3: to_ps
# 需要导入模块: from nltk.draw.util import CanvasFrame [as 别名]
# 或者: from nltk.draw.util.CanvasFrame import destroy_widget [as 别名]
def to_ps(self, filename):
"""Export as a PostScript image.
This function is used by `_repr_png_`.
"""
_canvas_frame = CanvasFrame()
# WIP customization of visual appearance
# NB: conda-provided python and tk cannot access most fonts on the
# system, thus it currently falls back on the default font
widget = tree_to_treesegment(_canvas_frame.canvas(), self,
tree_yspace=35,
node_font=('Verdana', -18, 'bold'),
leaf_font=('Verdana', -18))
_canvas_frame.add_widget(widget)
x, y, w, h = widget.bbox()
# print_to_file uses scrollregion to set the width and height of the
# pdf
_canvas_frame.canvas()['scrollregion'] = (0, 0, w, h)
# print to file
_canvas_frame.print_to_file(filename)
_canvas_frame.destroy_widget(widget)
示例4: RecursiveDescentApp
# 需要导入模块: from nltk.draw.util import CanvasFrame [as 别名]
# 或者: from nltk.draw.util.CanvasFrame import destroy_widget [as 别名]
#.........这里部分代码省略.........
variable=self._animation_frames,
value=2, accelerator='+')
menubar.add_cascade(label="Animate", underline=1, menu=animatemenu)
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label='About', underline=0,
command=self.about)
helpmenu.add_command(label='Instructions', underline=0,
command=self.help, accelerator='F1')
menubar.add_cascade(label='Help', underline=0, menu=helpmenu)
parent.config(menu=menubar)
#########################################
## Helper
#########################################
def _get(self, widget, treeloc):
for i in treeloc: widget = widget.subtrees()[i]
if isinstance(widget, TreeSegmentWidget):
widget = widget.node()
return widget
#########################################
## Main draw procedure
#########################################
def _redraw(self):
canvas = self._canvas
# Delete the old tree, widgets, etc.
if self._tree is not None:
self._cframe.destroy_widget(self._tree)
for twidget in self._textwidgets:
self._cframe.destroy_widget(twidget)
if self._textline is not None:
self._canvas.delete(self._textline)
# Draw the tree.
helv = ('helvetica', -self._size.get())
bold = ('helvetica', -self._size.get(), 'bold')
attribs = {'tree_color': '#000000', 'tree_width': 2,
'node_font': bold, 'leaf_font': helv,}
tree = self._parser.tree()
self._tree = tree_to_treesegment(canvas, tree, **attribs)
self._cframe.add_widget(self._tree, 30, 5)
# Draw the text.
helv = ('helvetica', -self._size.get())
bottom = y = self._cframe.scrollregion()[3]
self._textwidgets = [TextWidget(canvas, word, font=self._font)
for word in self._sent]
for twidget in self._textwidgets:
self._cframe.add_widget(twidget, 0, 0)
twidget.move(0, bottom-twidget.bbox()[3]-5)
y = min(y, twidget.bbox()[1])
# Draw a line over the text, to separate it from the tree.
self._textline = canvas.create_line(-5000, y-5, 5000, y-5, dash='.')
# Highlight appropriate nodes.
self._highlight_nodes()
self._highlight_prodlist()
# Make sure the text lines up.
示例5: ShiftReduceApp
# 需要导入模块: from nltk.draw.util import CanvasFrame [as 别名]
# 或者: from nltk.draw.util.CanvasFrame import destroy_widget [as 别名]
#.........这里部分代码省略.........
self._lastoper2.pack(side='left', fill='x', expand=1)
def _init_canvas(self, parent):
self._cframe = CanvasFrame(parent, background='white',
width=525, closeenough=10,
border=2, relief='sunken')
self._cframe.pack(expand=1, fill='both', side='top', pady=2)
canvas = self._canvas = self._cframe.canvas()
self._stackwidgets = []
self._rtextwidgets = []
self._titlebar = canvas.create_rectangle(0,0,0,0, fill='#c0f0f0',
outline='black')
self._exprline = canvas.create_line(0,0,0,0, dash='.')
self._stacktop = canvas.create_line(0,0,0,0, fill='#408080')
size = self._size.get()+4
self._stacklabel = TextWidget(canvas, 'Stack', color='#004040',
font=self._boldfont)
self._rtextlabel = TextWidget(canvas, 'Remaining Text',
color='#004040', font=self._boldfont)
self._cframe.add_widget(self._stacklabel)
self._cframe.add_widget(self._rtextlabel)
#########################################
## Main draw procedure
#########################################
def _redraw(self):
scrollregion = self._canvas['scrollregion'].split()
(cx1, cy1, cx2, cy2) = [int(c) for c in scrollregion]
# Delete the old stack & rtext widgets.
for stackwidget in self._stackwidgets:
self._cframe.destroy_widget(stackwidget)
self._stackwidgets = []
for rtextwidget in self._rtextwidgets:
self._cframe.destroy_widget(rtextwidget)
self._rtextwidgets = []
# Position the titlebar & exprline
(x1, y1, x2, y2) = self._stacklabel.bbox()
y = y2-y1+10
self._canvas.coords(self._titlebar, -5000, 0, 5000, y-4)
self._canvas.coords(self._exprline, 0, y*2-10, 5000, y*2-10)
# Position the titlebar labels..
(x1, y1, x2, y2) = self._stacklabel.bbox()
self._stacklabel.move(5-x1, 3-y1)
(x1, y1, x2, y2) = self._rtextlabel.bbox()
self._rtextlabel.move(cx2-x2-5, 3-y1)
# Draw the stack.
stackx = 5
for tok in self._parser.stack():
if isinstance(tok, Tree):
attribs = {'tree_color': '#4080a0', 'tree_width': 2,
'node_font': self._boldfont,
'node_color': '#006060',
'leaf_color': '#006060', 'leaf_font':self._font}
widget = tree_to_treesegment(self._canvas, tok,
**attribs)
widget.label()['color'] = '#000000'
else:
widget = TextWidget(self._canvas, tok,
color='#000000', font=self._font)
widget.bind_click(self._popup_reduce)