如何调整tkinter画布的位置

时间:2016-01-24 19:25:13

标签: python tkinter

在我的程序中,我想创建一个固定在屏幕左上角的tkinter画布,但画布默认为屏幕上的其他位置。以下是这种情况的图像:

Link to Image

这是我目前的代码:

 #!/usr/bin/python
 import tkinter
 from tkinter import *
 from PIL import Image, ImageTk

root = Tk()
root.title("Infrared Camera Interface")
root.resizable(width=FALSE, height=FALSE)



class MyApp:
  def __init__(self, parent):

    self.C = tkinter.Canvas(root, bg="white", height=560, width=450)


    #Set the dimensions of the window
    parent.minsize(width=600, height=600)
    parent.maxsize(width=600, height=600)


    #Prepare the image object being loaded into the stream camera
    self.imgName = 'Dish.png'
    self.img = Image.open(self.imgName)
    self.img = self.img.resize((560, 450), Image.ANTIALIAS)     

    #Display the image onto the stream
    self.displayimg = ImageTk.PhotoImage(self.img)

    self.C.create_image(0,0,image = self.displayimg, anchor = NW)

    self.C.pack()

myapp = MyApp(root)
root.mainloop()

如何将此画布锚定到屏幕的左上角?

1 个答案:

答案 0 :(得分:1)

我建议使用grid()而不是pack(),因为从长远来看,当你的程序获得更多碎片时,网格将为你提供更多自定义。

这个程序的

grid()看起来像这样:

self.C.grid(row=0, column = 0, rowspan = 0, columnspan = 0, sticky='nsew')

注意 - “sticky ='nsew'”意味着图像将定位在东南西北部,老实说,我还没有看到它以任何其他方式编写。

现在当然,“rowspan”和“columnspan”对于一个图像不是必需的,但是如果你想在这个图像的旁边或下面放几个东西,那么这些东西会派上用场