Python 如何在tkinter窗口上显示在图像上绘制线条的图像?

Python 如何在tkinter窗口上显示在图像上绘制线条的图像?,python,matplotlib,tkinter,tkinter-canvas,Python,Matplotlib,Tkinter,Tkinter Canvas,该程序读取位于位置C:/Square.png的图像,并在其上绘制线条。还定义了绘图标题。我想在tkinter窗口中显示整个图像。我该怎么做 这是图像。名称必须更改,我们可以运行代码。 导入数学 将matplotlib.pyplot作为plt导入 def绘图输出(opt_w、opt_h、n_x、n_y): y_开始,y_结束=100425 x_开始,x_结束=25400 img=plt.imread(“C:/Square.png”)#请更改位置 图,ax=plt.子批次(图尺寸=(10,10))

该程序读取位于位置C:/Square.png的图像,并在其上绘制线条。还定义了绘图标题。我想在tkinter窗口中显示整个图像。我该怎么做

这是图像。名称必须更改,我们可以运行代码。

导入数学
将matplotlib.pyplot作为plt导入
def绘图输出(opt_w、opt_h、n_x、n_y):
y_开始,y_结束=100425
x_开始,x_结束=25400
img=plt.imread(“C:/Square.png”)#请更改位置
图,ax=plt.子批次(图尺寸=(10,10))
打印轴(“关闭”)
ax.imshow(img)
x_间隔=(x_结束-x_开始)/n_x*2
h_x=范围(x_开始,x_结束,5)
对于范围(0,int(n_y))内的i:
如果我0:
ax.绘图(h_x,[y_start+(y_end-y_start)/n_y*i]*len(h_x),'--',线宽=5,颜色为耐火砖)
plt.title(str(int(n_x*n_y))+'ABCD\n'+'TYUI:'+str(opt_w)+',Yummy:'+str(opt_h))
def get(最小值、最小值、最大值、最大值、PL、PH、最小值、最大值、成本、成本):
x=1
如果最大值w
您需要添加plt.show()

导入数学
将matplotlib.pyplot作为plt导入
导入matplotlib
matplotlib.use(“Tkagg”)
def绘图输出(opt_w、opt_h、n_x、n_y):
y_开始,y_结束=100425
x_开始,x_结束=25400
img=plt.imread(“C:/Square.png”)#请更改位置
图,ax=plt.子批次(图尺寸=(10,10))
打印轴(“关闭”)
ax.imshow(img)
x_间隔=(x_结束-x_开始)/n_x*2
h_x=范围(x_开始,x_结束,5)
对于范围(0,int(n_y))内的i:
如果我0:
ax.绘图(h_x,[y_start+(y_end-y_start)/n_y*i]*len(h_x),'--',线宽=5,颜色为耐火砖)
plt.title(str(int(n_x*n_y))+'ABCD\n'+'TYUI:'+str(opt_w)+',Yummy:'+str(opt_h))
plt.show()
def get(最小值、最小值、最大值、最大值、PL、PH、最小值、最大值、成本、成本):
x=1
如果最大值w

编辑:我忘了将后端更改为tkinter

您想将图像嵌入另一个tk窗口还是只想显示绘图?@pask只想在弹出窗口中显示带有绘制线的图像您可以尝试以下操作:@AnilKumar我不知道这如何解决我在tkinter窗口上的问题。
import math
import matplotlib.pyplot as plt
def plot_output(opt_w, opt_h, n_x, n_y):
    y_start, y_end = 100, 425
    x_start, x_end = 25, 400

    img = plt.imread("C:/Square.png") #Please change the location
    fig, ax = plt.subplots(figsize=(10, 10))
    plt.axis('off')
    ax.imshow(img)

    x_interval = (x_end - x_start)/n_x*2

    h_x = range(x_start, x_end, 5)
    for i in range(0,int(n_y)):
        if i != 0:
            ax.plot(h_x, [y_start + (y_end-y_start)/n_y*i]*len(h_x), '--', linewidth=5, color='firebrick')
    plt.title(str(int(n_x*n_y)) + ' ABCD\n'+'TYUI:'+str(opt_w)+', Yummy:'+str(opt_h))

def get_get(min_w, min_h, max_w, max_h, PL, PH, min_t, max_t, cost_m, cost_a):
    x = 1
    if max_w < PL:
        x = math.ceil(PL / max_w)
    cost_rest = cost_m * PL * PH * (max_t + min_t) / 2 + cost_a * PH * x

    cost_y = float("inf")
    y = None
    if min_h == 0:
        min_h = 1
    for i in range(math.ceil(PH / max_h), math.floor(PH / min_h)+1):
        tmp_cost = cost_m * PL * PH * (max_t - min_t) / 2 / i + cost_a * PL * i
        if tmp_cost < cost_y:
            cost_y = tmp_cost
            y = i

    opt_w, opt_h, opt_cost = PL/x, PH/y, cost_rest + cost_y
    plot_output(opt_w, opt_h, x, y)
    return opt_w, opt_h, opt_cost

PL=30
PH=10
min_t=0.1
max_t=0.3
cost_m=0.1
cost_a=0.1
min_w=0.5
min_h=0.5
max_w=4
max_h=3

get_get(min_w, min_h, max_w, max_h, PL, PH, min_t, max_t, cost_m, cost_a)
import math
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use("Tkagg")

def plot_output(opt_w, opt_h, n_x, n_y):
    y_start, y_end = 100, 425
    x_start, x_end = 25, 400

    img = plt.imread("C:/Square.png") #Please change the location
    fig, ax = plt.subplots(figsize=(10, 10))
    plt.axis('off')
    ax.imshow(img)

    x_interval = (x_end - x_start)/n_x*2

    h_x = range(x_start, x_end, 5)
    for i in range(0,int(n_y)):
        if i != 0:
            ax.plot(h_x, [y_start + (y_end-y_start)/n_y*i]*len(h_x), '--', linewidth=5, color='firebrick')
    plt.title(str(int(n_x*n_y)) + ' ABCD\n'+'TYUI:'+str(opt_w)+', Yummy:'+str(opt_h))
    plt.show()

def get_get(min_w, min_h, max_w, max_h, PL, PH, min_t, max_t, cost_m, cost_a):
    x = 1
    if max_w < PL:
        x = math.ceil(PL / max_w)
    cost_rest = cost_m * PL * PH * (max_t + min_t) / 2 + cost_a * PH * x

    cost_y = float("inf")
    y = None
    if min_h == 0:
        min_h = 1
    for i in range(math.ceil(PH / max_h), math.floor(PH / min_h)+1):
        tmp_cost = cost_m * PL * PH * (max_t - min_t) / 2 / i + cost_a * PL * i
        if tmp_cost < cost_y:
            cost_y = tmp_cost
            y = i

    opt_w, opt_h, opt_cost = PL/x, PH/y, cost_rest + cost_y
    plot_output(opt_w, opt_h, x, y)
    return opt_w, opt_h, opt_cost

PL=30
PH=10
min_t=0.1
max_t=0.3
cost_m=0.1
cost_a=0.1
min_w=0.5
min_h=0.5
max_w=4
max_h=3

get_get(min_w, min_h, max_w, max_h, PL, PH, min_t, max_t, cost_m, cost_a)