简体   繁体   中英

My image doesnt scale to its right pixel

Gonna ask for help coz this is making me crazy now. The background image doesn't seem to fit in my window size. Can't see what i'm missing in my code. I've tried changing some x and y and other parameters, tried searching too but it still doesn't work. Thanks!

import Tkinter as tk
from Tkinter import *
import ttk
from PIL import Image, ImageTk


def callback():
    mainmenu.deiconify


def genkeymenu():

    mainmenu.withdraw()
    generatemenu = tk.Toplevel(mainmenu)

    bg1 = ImageTk.PhotoImage(file="key2.jpg")
    background_label = ttk.Label(generatemenu, image=bg1)
    background_label.place(x=0, y=0, relwidth=1, relheight=1)
    background_label.image = bg1

    keynamelabel = ttk.Label(generatemenu, text="Enter your key name")

    keynameEntry = ttk.Entry(generatemenu)

    keynameButton = ttk.Button(generatemenu, text="Enter")


    check1024= Checkbutton(generatemenu, text="1024 bit")
    check2048= Checkbutton(generatemenu, text="2048 bit")
    check4096= Checkbutton(generatemenu, text="4096 bit")

    background_label.pack()

    keynamelabel.pack()
    keynameEntry.pack()
    keynameButton.pack()

    check1024.pack()
    check2048.pack()
    check4096.pack()

    generatemenu.title("Generate Key")
    ttk.eval('tk::PlaceWindow %s center' % generatemenu.winfo_pathname(generatemenu.winfo_id()))
    generatemenu.mainloop()

class Buttons:
    def __init__(self, buttons):
        self.buttons = buttons
        self.genkeybutton = Button(buttons, text= "Generate Key Pair", fg="black", command=genkeymenu)
        self.id = buttons.create_window(50, 100, width=0, height=0,window=self.genkeybutton)

mainmenu = tk.Tk()
mainmenu.minsize(width=600, height=400)

imgpath = "key.jpg"
img=Image.open(imgpath)
img=img.resize((600,400), Image.ANTIALIAS)
photo = ImageTk.PhotoImage(img)


buttons = tk.Canvas(mainmenu)
buttons.pack()
buttons.create_image(0, 0, image=photo)
Buttons(buttons)

mainmenu.title("RSA ENCRYPTION")
#mainmenu.wm_attributes('-type', 'splash')
#mainmenu.eval('tk::PlaceWindow %s center' % 
mainmenu.winfo_pathname(mainmenu.winfo_id()))
mainmenu.mainloop()

This is the part of the code that i have problem with:

class Buttons:
    def __init__(self, buttons):
        self.buttons = buttons
        self.genkeybutton = Button(buttons, text= "Generate Key Pair", fg="black", command=genkeymenu)
        self.id = buttons.create_window(50, 100, width=0, height=0,window=self.genkeybutton)

mainmenu = tk.Tk()
mainmenu.minsize(width=600, height=400)

imgpath = "key.jpg"
img=Image.open(imgpath)
img=img.resize((600,400), Image.ANTIALIAS)
photo = ImageTk.PhotoImage(img)


buttons = tk.Canvas(mainmenu)
buttons.pack()
buttons.create_image(0, 0, image=photo)
Buttons(buttons)

mainmenu.title("RSA ENCRYPTION")
#mainmenu.wm_attributes('-type', 'splash')
#mainmenu.eval('tk::PlaceWindow %s center' % 
mainmenu.winfo_pathname(mainmenu.winfo_id()))
mainmenu.mainloop()

Here is the screenshot of the problem:

这是问题的屏幕截图

In place of this:

imgpath = "key2.jpg"
img=Image.open(imgpath)
img=img.resize((600,400), Image.ANTIALIAS)
photo = ImageTk.PhotoImage(img) 

do this :

imgpath = PhotoImage(file="key2.jpg")
img = Label(mainmenu, image=imgpath)
img.place(x=0, y=0, relwidth=1, relheight=1)

it will cover the background.Then comment this to see what am talking about #buttons.create_image(0, 0, image=img)

better still do for image in the canvas

buttons = tk.Canvas(mainmenu)
buttons.pack()
buttons.create_image(0, 0, image=imgpath)
Buttons(buttons)

Full code below

import Tkinter as tk
from Tkinter import *
import ttk
from PIL import Image, ImageTk


def callback():
    mainmenu.deiconify


def genkeymenu():

    mainmenu.withdraw()
    generatemenu = tk.Toplevel(mainmenu)

    bg1 = ImageTk.PhotoImage(file="key2.jpg")
    background_label = ttk.Label(generatemenu, image=bg1)
    background_label.place(x=0, y=0, relwidth=1, relheight=1)
    background_label.image = bg1

    keynamelabel = ttk.Label(generatemenu, text="Enter your key name")

    keynameEntry = ttk.Entry(generatemenu)

    keynameButton = ttk.Button(generatemenu, text="Enter")


    check1024= Checkbutton(generatemenu, text="1024 bit")
    check2048= Checkbutton(generatemenu, text="2048 bit")
    check4096= Checkbutton(generatemenu, text="4096 bit")

    background_label.pack()

    keynamelabel.pack()
    keynameEntry.pack()
    keynameButton.pack()

    check1024.pack()
    check2048.pack()
    check4096.pack()

    generatemenu.title("Generate Key")
    ttk.eval('tk::PlaceWindow %s center' % 
    generatemenu.winfo_pathname(generatemenu.winfo_id()))
   generatemenu.mainloop()

class Buttons:
    def __init__(self, buttons):
        self.buttons = buttons
        self.genkeybutton = Button(buttons, text= "Generate Key Pair", fg="black", command=genkeymenu)
        self.id = buttons.create_window(50, 100, width=0, height=0,window=self.genkeybutton)

mainmenu = tk.Tk()
mainmenu.minsize(width=600, height=400)

imgpath = PhotoImage(file="key2.jpg")
img = Label(mainmenu, image=imgpath)
img.place(x=0, y=0, relwidth=1, relheight=1)

buttons = tk.Canvas(mainmenu)
buttons.pack()
buttons.create_image(0, 0, image=imgpath)
Buttons(buttons)

mainmenu.title("RSA ENCRYPTION")
#mainmenu.wm_attributes('-type', 'splash')
#mainmenu.eval('tk::PlaceWindow %s center' %
mainmenu.winfo_pathname(mainmenu.winfo_id())
mainmenu.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM