Tkinter-新窗口中的画布,图像调整为新窗口分辨率

时间:2019-08-02 18:01:10

标签: python-3.x tkinter tkinter-canvas

我正在尝试做一个Canvas,通过按一个按钮在新窗口中显示图片,这实际上是可行的,但是我需要在新窗口中显示图像的显示器调整为新窗口尺寸。

我已经设法设法将尺寸调整为特定大小,但对我来说真的没有任何解决办法。

代码如下:

from tkinter import *
import requests
import shutil
from xml.dom.minidom import parse
import fileinput
import os.path as path
import os
import tempfile


class Window(Frame):
    def __init__(self, master = None):
        Frame.__init__(self, master)

        self.master = master

root = Tk()
root.title("Etiquetas")
root.configure(width=1280,height=720)
root.geometry('{}x{}'.format(427, 600))
root.resizable(width=False, height=False)
canvas = Canvas(root, width=500, height=500)
canvas.pack(fill="both", expand=True)
my_image = PhotoImage(file='label.png')

def new_winF(): # new window definition
    newwin = Toplevel(root)
    display = Label(newwin, text="Humm, see a new window !")
    display.pack()


def retrieve_input():
    f = open('DataXML.xml', 'w+')
    f.truncate(0)


    inputParamString = ParamString.get("1.0","end-1c")
    inputAlto = Alto.get("1.0","end-1c")
    inputAncho = Ancho.get("1.0","end-1c")
    inputTemplateLabel = TemplateLabel.get("1.0","end-1c")

    #StringEtiqueta = open("DataXML.xml", "a+")
    f.write(inputParamString)
    f.close()


    doc = parse("DataXML.xml")
    my_node_list = doc.getElementsByTagName("label_param")

    LabelResultado = open("Template.txt", "w")
    LabelResultado.write(inputTemplateLabel)
    LabelResultado.close()

    for node in my_node_list:
        name_node = node.getElementsByTagName("name")
        value_node = node.getElementsByTagName("value")
        y = ("[" + name_node[0].firstChild.data + "]")
        z = y.upper()
        print(z)
        if (value_node[0].firstChild != None):
            print(value_node[0].firstChild.data)
            x = value_node[0].firstChild.data
        else:
            print("")
            x = ""


        with fileinput.FileInput("Template.txt", inplace=True) as file:
            for line in file:
                print(line.replace(z, x), end='')
    w = open('Template.txt.bak', 'w+')
    w.close()

    codigoZPL = open("Template.txt", "r")
    zpl = codigoZPL.read()
    codigoZPL.close()

    url = ('http://api.labelary.com/v1/printers/8dpmm/labels/'+inputAlto+'x'+inputAncho+'/0/')
    files = {'file': zpl}
    #headers = {'Accept': 'application/pdf'}
    response = requests.post(url, headers=0, files=files, stream=True)

    if response.status_code == 200:
        response.raw.decode_content = True
        with open('label.png', 'wb') as out_file:  # change file name for PNG images
            shutil.copyfileobj(response.raw, out_file)
    else:
        print('Error: ' + response.text)


    newwin = Toplevel(root)
    ncanvas = Canvas(newwin, width=500, height=500)
    canvas.pack(fill="both", expand=True)
    my_image = PhotoImage(file='label.png')
    display = canvas.create_image(850, 50, anchor=NE, image=my_image)
    line = canvas.create_line(0, 0, 0, 0)
    display.pack()

    root.mainloop()


def click_limpiar():
    Alto.delete(0.0, END)
    Ancho.delete(0.0, END)
    ParamString.delete(0.0, END)
    TemplateLabel.delete(0.0, END)

def close_window():
    root.destroy()
    exit()

#Crear un label
Label (text="Ingresar ParamString: ") .place(x=10,y=15)
Label (text="Ingresar Tamaño (pulgadas): ") .place(x=10,y=160)
Label (text="Ingresar TemplateLabel: ") .place(x=10,y=218)

#Crear un textbox

ParamString = Text(root)
ParamString.pack()
ParamString.place(x=12, y=50, height=100, width=400)

Alto = Text(root)
Alto.pack()
Alto.place(x=12, y=190, height=20, width=20)

Ancho = Text(root)
Ancho.pack()
Ancho.place(x=52, y=190, height=20, width=20)

TemplateLabel = Text(root)
TemplateLabel.pack
TemplateLabel.place(x=12, y=250, height=300, width=400)

#Botones
Button(root, text="Aceptar", width=16, command=retrieve_input) .place(x=20,y=560)
Button(root, text="Limpiar", width=16, command=click_limpiar) .place(x=150,y=560)
Button(root, text="Salir", width=16, command=close_window) .place(x=280,y=560)


app = Window(root)

root.mainloop()

画布部分是这个:

newwin = Toplevel(root)
ncanvas = Canvas(newwin, width=500, height=500)
canvas.pack(fill="both", expand=True)
my_image = PhotoImage(file='label.png')
display = canvas.create_image(850, 50, anchor=NE, image=my_image)
line = canvas.create_line(0, 0, 0, 0)
display.pack()

预先感谢

0 个答案:

没有答案