简体   繁体   English

在角落中加载Tkinter中的图像

[英]Load in a corner an image in Tkinter

I'm beginner in Python and Tkinter. 我是Python和Tkinter的初学者。 I'm trying to put an image in the top-left corner but I couldn't. 我想把图像放在左上角,但我不能。 I was tried with the property "justify" and "Anchor". 我被审判了属性“justify”和“Anchor”。 Here is my code: 这是我的代码:

logo_upb = PhotoImage(file="upb.gif")
label = Label(root, image=logo_upb, justify=LEFT)
label.image = logo_upb
label.place(bordermode=INSIDE, x=0, y=0)
label.pack()

I would be thankful with any solution. 任何解决方案我都会感激不尽。

Have you tried grid geometry manager. 你有没有尝试过网格几何管理器

The grid geometry manager puts the widgets in a 2-dimensional table. 网格几何管理器将小部件放在二维表中。 You'll be surprised how much easier it is to use the grid manager instead of packer. 您会惊讶地发现使用网格管理器而不是打包器更容易。

Lets take this example to show what grid can do. 让我们举个例子来说明网格可以做什么。 Creating this layout using the pack manager is possible, but it takes a number of extra frame widgets. 可以使用包管理器创建此布局,但它需要许多额外的框架小部件。

tkinter网格示例

But with grid geometry manager you can also have the widgets span more than one cell. 但是使用网格几何管理器,您还可以让窗口小部件跨越多个单元格。 The columnspan option is used to let a widget span more than one column < checkbutton > and < image >, and the rowspan option lets it span more than one row < image >. columnspan选项用于让窗口小部件跨越多个列<checkbutton>和<image>,而rowspan选项允许它跨越多行<image>。

The following code creates the shown layout: 以下代码创建显示的布局:

label1.grid(sticky=E)
label2.grid(sticky=E)

entry1.grid(row=0, column=1)
entry2.grid(row=1, column=1)

checkbutton.grid(columnspan=2, sticky=W)

image.grid(row=0, column=2, columnspan=2, rowspan=2,
           sticky=W+E+N+S, padx=5, pady=5)

button1.grid(row=2, column=2)
button2.grid(row=2, column=3)

So your answer will be identical to position of < label 1 > in other words grid would be row = 0 , and column = 1 . 所以你的答案将与<label 1>的位置相同,换句话说,grid将是row = 0 ,而column = 1 Tkinter has native support for GIFs, so there is no need for additional library. Tkinter本身支持GIF,因此不需要额外的库。

import Tkinter as tk

root = tk.Tk()
img = tk.PhotoImage(file ="somefile.gif")
panel = tk.Label(root, image = img)
panel.grid(row=0,column=1)
root.mainloop()

My personal recommendation is using Python Imaging Library (PIL) link: http://www.pythonware.com/products/pil/ adding more supported files formats to the game. 我个人的建议是使用Python Imaging Library(PIL)链接: http ://www.pythonware.com/products/pil/为游戏添加更多支持的文件格式。 List of supported files formats: http://infohost.nmt.edu/tcc/help/pubs/pil/formats.html 支持的文件格式列表: http//infohost.nmt.edu/tcc/help/pubs/pil/formats.html

In this example I used .jpg file format which is not natively supported in tkinter, and all works perfectly because we are using PIL. 在这个例子中,我使用的是.jpg文件格式,这在tkinter中并不是本机支持的,并且一切都很完美,因为我们使用的是PIL。

import Tkinter as tk
from PIL import ImageTk,Image

root = tk.Tk()
img = ImageTk.PhotoImage(Image.open("somefile.jpg"))
panel = tk.Label(root, image = img)
panel.grid(row=0,column=1)
root.mainloop()

Warning: Never mix grid and pack in the same master window. 警告:切勿在同一主窗口中混合网格和打包。 Tkinter will happily spend the rest of your lifetime trying to negotiate a solution that both managers are happy with. Tkinter很乐意度过余生,试图协商一个管理人员都满意的解决方案。 Instead of waiting, kill the application, and take another look at your code. 而不是等待,杀死应用程序,再看看你的代码。 A common mistake is to use the wrong parent for some of the widgets. 一个常见的错误是对某些小部件使用了错误的父级。

LINK: http://effbot.org/tkinterbook/grid.htm 链接: http//effbot.org/tkinterbook/grid.htm

The specific reason for your problem is that you use place more-or-less correctly, then throw all that placement away when you call pack() . 问题的具体原因是你或多或少地使用了正确的位置,然后在调用pack()时抛弃所有的位置。 place , pack and grid aren't complimentary -- you must use only one to manage any particular widget. placepackgrid不是免费的 - 您必须只使用一个来管理任何特定的小部件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM