【问题标题】:tkinter window and background image don't align properlytkinter 窗口和背景图像未正确对齐
【发布时间】:2016-12-05 11:03:35
【问题描述】:

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

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

这是我的代码:

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

【问题讨论】:

    标签: python tkinter python-3.4


    【解决方案1】:

    我不确定我是否理解所有内容,但我通过以下方式设法摆脱了边界(至少在 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()
    

    【讨论】:

      猜你喜欢
      • 2020-08-08
      • 2014-07-26
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-26
      • 1970-01-01
      相关资源
      最近更新 更多