【问题标题】:problem showing full image using .place() in tkinter在 tkinter 中使用 .place() 显示完整图像的问题
【发布时间】:2020-09-01 23:57:22
【问题描述】:

我的代码看起来像这样,我不得不说我正在尝试创建的程序将有几个选项卡 (8-10) 和其中的许多小部件,所以我选择使用 .地方()。我一直在尝试显示图片,但没有显示完整,我尝试使用画布和标签,但都显示了部分图片。

这是我的代码

from tkinter import * #GUI
from tkinter import ttk #GUI
from PIL import ImageTk,Image  
root = Tk()

root.title("Example") #Title

####get resolution
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
#### end get resolution
frame = Frame(root)
frame.pack(fill="both",expand=1) 


frame.config(width=screen_width,height=screen_height)

tabControl = ttk.Notebook(root)

tab1 = ttk.Frame(tabControl) 
tabControl.add(tab1, text='Tab 1')
tab2 = ttk.Frame(tabControl) 
tabControl.add(tab2, text='Tab 2')

tabControl.place(x=0,y=round(screen_height * .2), 
width=round(screen_width),height=round(screen_height * .9))

frame_one = LabelFrame(tab1, text="Image")
frame_one.place(x=round(screen_width * .6),y=round(screen_height * 
.01),width=round(screen_width * .3),height=round(screen_height * .3))

frame_two = LabelFrame(tab1, text="Image")
frame_two.place(x=round(screen_width * .6),y=round(screen_height * 
.35),width=round(screen_width * .3),height=round(screen_height * .3))

frame_three = LabelFrame(tab1, text="Labels, radios, checkbuttons, 
buttons")
frame_three.place(x=round(screen_width * .01),y=round(screen_height * 
.01),width=round(screen_width * .55),height=round(screen_height * 
.65))

button = Button(tab1,text="Button")
button.place(x=round(screen_width * 0.45), y=round(screen_height * 
.66))

canv = Canvas(tab1)
img = ImageTk.PhotoImage(Image.open("test.jpg"))  # PIL solution
canv.create_image(0, 0, image=img, anchor=NW)
canv.place(x=round(screen_width * .605),y=round(screen_height * 0.03), 
width=round(screen_width * .29), height=round(screen_height * .28))

canv2 = Canvas(tab1)
img2 = ImageTk.PhotoImage(Image.open("test.jpg"))  # PIL solution
canv2.create_image(0, 0, image=img2, anchor=NW)
canv2.place(x=round(screen_width * .605),y=round(screen_height * 
0.37), width=round(screen_width * .29), height=round(screen_height * 
.28))

root.mainloop() 

希望有人能帮我把图片完整展示一下

【问题讨论】:

  • 既然你在place(...)中限制了画布的大小,那么大小是否足够显示整个图像?
  • place 几乎总是比使用 packgrid 更难创建响应式 UI。
  • 我知道 place() 让它变得更难,但我有几个小部件并且需要以特定方式显示它们,这就是我使用 place 的原因。问题是我无法调整图片的大小,因为用户会选择一张并显示它。
  • 但是您可以调整加载图像的大小以适合画布。
  • 你能举个例子吗?我试着去做,但我做不到。我是python新手是我的第一个程序

标签: python-3.x image tkinter label


【解决方案1】:

由于您使用place(...) 限制了画布的大小,因此画布的大小可能不足以显示完整图像。

您可以使用PIL.Image.thumbnail() 调整加载图像的大小以适合画布:

canv = Canvas(frame_one) # put into LabelFrame
canv.pack(fill='both', expand=1)
canv.update() # required to get the real size of canvas
w, h = canv.winfo_width(), canv.winfo_height()
img = Image.open("test.jpg")
img.thumbnail((w, h))
img = ImageTk.PhotoImage(img)
canv.create_image(w//2, h//2, image=img, anchor=CENTER)

canv2 = Canvas(frame_two)
canv2.pack(fill='both', expand=1)
canv2.update()
w, h = canv2.winfo_width(), canv.winfo_height()
img2 = Image.open("test.jpg")
img2.thumbnail((w, h))
img2 = ImageTk.PhotoImage(img2)
canv2.create_image(w//2, h//2, image=img2, anchor=CENTER)

请注意thumbnail() 只会在图像大小大于函数给定大小时调整图像大小。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-07
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    • 2022-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多