2016-07-31 73 views
1

我正在編寫一個辛普森瑣事遊戲作爲我的第一個大型編程項目。我的問題是雙重的:tkinter窗口和背景圖像不正確對齊

  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

回答

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