日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python tkinter canvas 顯示圖片的示例

系統 1868 0

先來看一下該方法的說明

            
create_image(position, **options) [#]
Draws an image on the canvas.

position
Image position, given as two coordinates.
**options
Image options.
activeimage=
anchor=
Where to place the image relative to the given position. Default is CENTER.
disabledimage=
image=
The image object. This should be a PhotoImage or BitmapImage, or a compatible object (such as the PIL PhotoImage). The application must keep a reference to the image object.
state=
Item state. One of NORMAL, DISABLED, or HIDDEN.
tags=
A tag to attach to this item, or a tuple containing multiple tags.
Returns:
The item id.

          

關于image有兩個重要的點要注意,一個是格式,第二是要保持持續引用

The image object. This should be a

1.This should be a PhotoImage or BitmapImage, or a compatible object (such as the PIL PhotoImage).

2.The application must keep a reference to the image object.

因此代碼應該這樣寫,并且變量im應該是全局變量

            
image = Image.open("img.jpg") 
im = ImageTk.PhotoImage(image) 

canvas.create_image(300,50,image = im) 

          

但如果我就是想要在方法里調用怎么辦?

那么可以提前聲明全局變量

            
image = None
im = None
          

之后在方法里使用global來聲明變量為全局變量

即:

            
def method():
  global image
  global im
  image = Image.open("img.jpg") 
  im = ImageTk.PhotoImage(image) 
  ...
          

以上這篇python tkinter canvas 顯示圖片的示例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 太白县| 寻乌县| 承德县| 宿迁市| 永济市| 长垣县| 景德镇市| 长汀县| 大城县| 沁阳市| 石嘴山市| 莱州市| 吴桥县| 台州市| 观塘区| 古丈县| 阜阳市| 和硕县| 扬中市| 望谟县| 遂川县| 永昌县| 晋中市| 合肥市| 泾源县| 吐鲁番市| 晋宁县| 育儿| 五常市| 宁晋县| 柏乡县| 芒康县| 泸州市| 息烽县| 柳江县| 金溪县| 台湾省| 平原县| 海安县| 偏关县| 芦山县|