我的图片无法缩放到右侧像素

时间:2017-12-05 13:45:02

标签: python python-2.7 tkinter

要求帮助因为这让我现在变得疯狂。 背景图像似乎不适合我的窗口大小。无法查看我的代码中缺少的内容。我尝试过更改某些x和y以及其他参数,尝试搜索但仍然无法正常工作。谢谢!

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()

这是我遇到问题的代码的一部分:

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

1 个答案:

答案 0 :(得分:0)

取代这个:

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

这样做:

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

它将涵盖背景。然后对此进行评论以查看我在谈论的内容#buttons.create_image(0, 0, image=img)

更好地为画布中的图像做了

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

以下完整代码

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()