如何在Tkinter中找出PhotoImage的大小?

时间:2016-03-16 01:28:52

标签: python tkinter photoimage

如何访问PhotoImage()类对象的宽度和高度信息? 我尝试了PhotoImage(...).winfo_width()PhotoImage(...)["Width"]。他们俩都没有用。

3 个答案:

答案 0 :(得分:7)

PhotoImage对象有widthheight方法:

import Tkinter as tk

image_data = '''
    R0lGODlhEAAQAMQZAMPDw+zs7L+/v8HBwcDAwLW1teLi4t7e3uDg4MLCwuHh4e7u7t/f38TExLa2
    tre3t7i4uL6+vu/v77q6uu3t7b29vby8vLm5ubu7u+3t7QAAAAAAAAAAAAAAAAAAAAAAACH5BAEA
    ABkALAAAAAAQABAAAAWNYCaOZFlWV6pWZlZhTQwAyYSdcGRZGGYNE8vo1RgYCD2BIkK43DKXRsQg
    oUQiFAkCI3iILgCLIEvJBiyQiOML6GElVcsFUllD25N3FQN51L81b2ULARN+dhcDFggSAT0BEgcQ
    FgUicgQVDHwQEwc+DxMjcgITfQ8Pk6AlfBEVrjuqJhMOtA4FBRctuiUhADs=
'''

root = tk.Tk()
image = tk.PhotoImage(data=image_data)
dimensions = "image size: %dx%d" % (image.width(), image.height())
label = tk.Label(root, compound="top", image=image, text=dimensions)
label.pack()
root.mainloop()

答案 1 :(得分:1)

可能是tkinter个错误。更好地使用PIL / Pillow ImageImageTk.PhotoImage而不仅仅是tk.PhotoImage

In [30]: import tkinter as tk
In [31]: from PIL import Image, ImageTk

In [32]: tk_img = tk.PhotoImage('./pixel-art-2237058.gif')
In [35]: tk_img.width()
Out[35]: 0

In [36]: pil_img = Image.open('./pixel-art-2237058.gif')
In [37]: tk_pil_img = ImageTk.PhotoImage(pil_img)
In [39]: tk_pil_img.width()
Out[39]: 811

答案 2 :(得分:0)

尝试:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    long third = 123654123654123LL;
    float fourth = 12335.67890;

    std::ios initialState(nullptr);
    initialState.copyfmt(std::cout);

    // 4
    cout << third << scientific<< endl;

    // 5
    cout << showpoint << fixed << setprecision(4) << right << showpos << fourth << endl;

    cout.copyfmt(initialState);

    // 6
    cout << setprecision(4) << fourth << endl;

    return 0;
}