Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在tkinter中使用.place()显示完整图像时出现问题_Python 3.x_Image_Tkinter_Label - Fatal编程技术网

Python 3.x 在tkinter中使用.place()显示完整图像时出现问题

Python 3.x 在tkinter中使用.place()显示完整图像时出现问题,python-3.x,image,tkinter,label,Python 3.x,Image,Tkinter,Label,我的代码看起来是这样的,我必须说我正在尝试创建的程序将有几个选项卡(8-10)和许多小部件,因此我选择使用.place()设计图形界面。我一直试图显示一张图片,但它没有显示完整的,我尝试使用画布和标签,但都显示了部分图片 这是我的密码 from tkinter import * #GUI from tkinter import ttk #GUI from PIL import ImageTk,Image root = Tk() root.title("Example")

我的代码看起来是这样的,我必须说我正在尝试创建的程序将有几个选项卡(8-10)和许多小部件,因此我选择使用.place()设计图形界面。我一直试图显示一张图片,但它没有显示完整的,我尝试使用画布和标签,但都显示了部分图片

这是我的密码

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(…)
限制了画布的大小,画布的大小可能不足以显示完整的图像

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


请注意,
thumbnail()
仅当图像的大小大于函数的给定大小时才会调整图像的大小。

因为您已在
位置(…)限制画布的大小。
,大小是否足以显示整个图像?
place
几乎总是比使用
pack
grid
更难创建响应的UI。我知道place()更难,但我有几个小部件,需要以特定的方式显示它们,这就是我使用place的原因。问题是,我无法调整图片大小,因为用户将选择一个图片并显示它。但您可以调整加载的图像大小以适应画布。您能给我一个例子吗?我试着去做,但做不到。我是python新手,这是我的第一个程序
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)