python - Tkinter 和 pyplot 问题中的显示

标签 python matplotlib tkinter

这是上下文:

import numpy as np
import matplotlib.pyplot as pl
from matplotlib.widgets import RectangleSelector

#####################
#  MOUSE CLICK      #
#####################

def line_select_callback(eclick, erelease):
    'eclick and erelease are the press and release events'
    cadre.append(eclick.xdata)
    cadre.append(eclick.ydata)
    cadre.append(erelease.xdata)
    cadre.append(erelease.ydata)

def toggle_selector(event):
    print (' Key pressed.')
    if event.key in ['enter', 'Q', 'q'] and toggle_selector.RS.active:
        print ('Selection done ')
        toggle_selector.RS.set_active(False)
        pl.close()


###########
#  Input  #
###########
from Tkinter import *
import tkFileDialog
import matplotlib.image as mpimg

root = Tk()
root.withdraw()
path = tkFileDialog.askopenfilename(filetypes = [("Image fibre", "*")])

''' some code .... '''

################
#  Select ROI  #
################
fig, current_ax = pl.subplots()                    # make a new plotingrange
cadre = []
pl.imshow(Image, interpolation='none')
# drawtype is 'box' or 'line' or 'none'
toggle_selector.RS = RectangleSelector(current_ax, line_select_callback,
                                       drawtype='box', useblit=True,
                                       button=[1,3], # don't use middle button
                                       minspanx=5, minspany=5,
                                       spancoords='pixels',
                                       rectprops = dict(facecolor='red',
                                           edgecolor ='white',
                                           alpha =0.2,
                                           fill = True),
                                         )
pl.connect('key_press_event', toggle_selector)
pl.title('Select ROI and press \'Enter\'')
pl.show()

print "After closing"

这里的目的是在 pyplot 中设置一个阻塞窗口,直到按“Q”或“enter”键或关闭窗口。当我使用 Tkinter 窗口关闭但未显示 “关闭后” 时出现问题(程序似乎被阻止)。 我尝试用 fig.show() 替换 pl.show() 但它不是同一个函数,因此窗口不会阻塞并且程序会继续。

注意:如果我删除它,它几乎可以正常工作:

root = Tk()
root.withdraw()

在这种情况下会出现一个空窗口,我必须将其关闭才能正常工作。

有什么想法吗?

最佳答案

我实际上需要销毁我的对象:

root = Tk()
root.withdraw()
path = tkFileDialog.askopenfilename(filetypes = [("Image fibre", "*")])
root.destroy()

关于python - Tkinter 和 pyplot 问题中的显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24200994/

相关文章:

python - Homebrew 使用 python 模块安装 libxml2

python - 如何关闭 Matplotlib 3D 散点图中的透明度?

python - 如何在 Python 中绘制聚类散点图

python - Tkinter(动态)框架不显示

python-3.x - 希伯来语与 tkinter - BiDi

python - 使 Tkinter.Listbox 选择持久化

python - 接收与多个外键关联的 View 中的对象。 Django 、Python

python - 将动态python对象转换为json

Python 请求包 : Handling xml response

python - 绘制半透明线/16 位 alpha 值