初始設定了Label的width和height。按下按鈕后,就在Label上顯示圖片,同時設定了圖片的尺寸。這時圖片尺寸顯示不對,刪掉Label初始設定的width和height就對了,這是為啥?但還是要設定Label的初始尺寸的,那么這個尺寸問題怎么解決呀?
然后前后尺寸的單位好像不一樣?
import tkinter as tk
from PIL import Image, ImageTk
class test(tk.Tk):
def __init__(self):
super(test, self).__init__()
self.label = tk.Label(self, width=100, height=10, text='這是一個無辜的label') # 初始設定Label的width和height
self.label.pack()
self.button = tk.Button(self,text='無辜的按鈕',command=self.show_img)
self.button.pack()
def show_img(self):
img_open = Image.open('圖片')
resized = img_open.resize((600, 700), Image.ANTIALIAS) # 設定圖片尺寸
img = ImageTk.PhotoImage(resized)
self.label.config(image=img)
self.label.image = img
if __name__ == '__main__':
gui = test()
gui.mainloop()
求助各位大佬嗚嗚嗚,救救孩子
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/283277.html
標籤:其他技術專區
上一篇:Qt5.5 報C1041怎么解決
