tkinter窗口和背景图像没有正确对齐

时间:2016-07-31 04:07:24

标签: python tkinter python-3.4

我正在写一个辛普森一家的琐事游戏作为我的第一个大型编程项目。我的问题有两个:

  1. 这是创建背景图片的正确方法吗?请记住,我的计划是在背景中播放辛普森一家主题曲以及背景图像顶部的一个或两个按钮。
  2. 假设下面的代码是我想要完成的正确方法,为什么我的图像和窗口左边会出现一条细灰线? IE浏览器。为什么图像没有像在右侧那样完全填满窗户?
  3. 这是我的代码:

    from tkinter import *
    from tkinter import ttk
    from PIL import Image, ImageTk
    
    root = Tk()
    root.title("The Simpsons Trivia Game")
    root.geometry('400x600')
    root.resizable(0,0)
    
    def resize_image(event):
    new_width = event.width
    new_height = event.height
    image = copy_of_image.resize((new_width, new_height))
    photo = ImageTk.PhotoImage(image)
    label.config(image = photo)
    label.image = photo 
    
    image = Image.open('E:\Simpsons Trivia Game\SimpsonsPic.png')
    copy_of_image = image.copy()
    photo = ImageTk.PhotoImage(image)
    label = ttk.Label(root, image = photo)
    label.bind('<Configure>', resize_image)
    label.pack(fill=BOTH, expand = YES)
    
    root.mainloop()
    

    tkinter window with background image (left side of window not perfectly alligned with background image

1 个答案:

答案 0 :(得分:0)

我不确定我是否了解所有内容,但我设法通过以下方式摆脱边界(至少在Linux上):

from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk

root = Tk()
root.title("The Simpsons Trivia Game")
root.geometry("400x600")
root.resizable(0,0)

image = Image.open('/tmp/foobar.png')
photo = ImageTk.PhotoImage(image)
label = ttk.Label(root, image = photo)
label.pack()
label.place(relx=0.5, rely=0.5, anchor="center")

root.mainloop()