如何在 tkinter 窗口上的图像上显示带有线条的图像?

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


这是图像。名称必须更改,我们可以运行代码。 https://imgur.com/RkV02yY


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)


素胚勾勒不出你
浏览 171回答 1
1回答

精慕HU

您需要添加 plt.show()import mathimport matplotlib.pyplot as pltimport matplotlibmatplotlib.use("Tkagg")def plot_output(opt_w, opt_h, n_x, n_y):&nbsp; &nbsp; y_start, y_end = 100, 425&nbsp; &nbsp; x_start, x_end = 25, 400&nbsp; &nbsp; img = plt.imread("C:/Square.png") #Please change the location&nbsp; &nbsp; fig, ax = plt.subplots(figsize=(10, 10))&nbsp; &nbsp; plt.axis('off')&nbsp; &nbsp; ax.imshow(img)&nbsp; &nbsp; x_interval = (x_end - x_start)/n_x*2&nbsp; &nbsp; h_x = range(x_start, x_end, 5)&nbsp; &nbsp; for i in range(0,int(n_y)):&nbsp; &nbsp; &nbsp; &nbsp; if i != 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ax.plot(h_x, [y_start + (y_end-y_start)/n_y*i]*len(h_x), '--', linewidth=5, color='firebrick')&nbsp; &nbsp; plt.title(str(int(n_x*n_y)) + ' ABCD\n'+'TYUI:'+str(opt_w)+', Yummy:'+str(opt_h))&nbsp; &nbsp; plt.show()def get_get(min_w, min_h, max_w, max_h, PL, PH, min_t, max_t, cost_m, cost_a):&nbsp; &nbsp; x = 1&nbsp; &nbsp; if max_w < PL:&nbsp; &nbsp; &nbsp; &nbsp; x = math.ceil(PL / max_w)&nbsp; &nbsp; cost_rest = cost_m * PL * PH * (max_t + min_t) / 2 + cost_a * PH * x&nbsp; &nbsp; cost_y = float("inf")&nbsp; &nbsp; y = None&nbsp; &nbsp; if min_h == 0:&nbsp; &nbsp; &nbsp; &nbsp; min_h = 1&nbsp; &nbsp; for i in range(math.ceil(PH / max_h), math.floor(PH / min_h)+1):&nbsp; &nbsp; &nbsp; &nbsp; tmp_cost = cost_m * PL * PH * (max_t - min_t) / 2 / i + cost_a * PL * i&nbsp; &nbsp; &nbsp; &nbsp; if tmp_cost < cost_y:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cost_y = tmp_cost&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y = i&nbsp; &nbsp; opt_w, opt_h, opt_cost = PL/x, PH/y, cost_rest + cost_y&nbsp; &nbsp; plot_output(opt_w, opt_h, x, y)&nbsp; &nbsp; return opt_w, opt_h, opt_costPL=30PH=10min_t=0.1max_t=0.3cost_m=0.1cost_a=0.1min_w=0.5min_h=0.5max_w=4max_h=3get_get(min_w, min_h, max_w, max_h, PL, PH, min_t, max_t, cost_m, cost_a)编辑:我忘记将后端更改为 tkinter
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python