我正在开发一个程序,它可以获取一张图片并将其放入画布中,就像一个照片查看器。但我想让这张图片位于画布小部件的中心,而它似乎会进入左上角。为什么会这样?我该如何将其置于画布小部件的中心?
代码:
代码:
from Tkinter import *
from PIL import ImageTk, Image
import os
class Application(Frame):
def __init__(self, parent):
Frame.__init__(self,parent)
self.pack(fill=BOTH, expand=True)
self.create_Menu()
self.create_widgets()
def create_Menu(self):
self.menuBar = Menu(self)
self.fileMenu = Menu(self.menuBar, tearoff=0)
self.fileMenu.add_command(label="Open", command=self.getImage)
self.fileMenu.add_separator()
self.fileMenu.add_command(label="Exit", command=self.exitProgram)
self.menuBar.add_cascade(label="File", menu=self.fileMenu)
root.config(menu=self.menuBar)
def create_widgets(self):
self.viewWindow = Canvas(self, bg="white")
self.viewWindow.pack(side=TOP, fill=BOTH, expand=True)
def getImage(self):
imageFile = Image.open("C:/Users/Public/Pictures/Sample Pictures/Desert.jpg")
imageFile = ImageTk.PhotoImage(imageFile)
self.viewWindow.image = imageFile
self.viewWindow.create_image(0, 0, anchor=CENTER, image=imageFile, tags="bg_img")
def exitProgram(self):
os._exit(0)
root = Tk()
root.title("Photo Zone")
root.wm_state('zoomed')
app = Application(root)
root.mainloop()