網站首頁 編程語言 正文
0、問題
在用Tkinter進行編程時,需要在一個Frame下顯示多個圖片,但是不管怎么設置都是只顯示最后一張,就像這樣:
代碼
for i in range(3):
...
image=ImageTk.PhotoImage(f'img{i}.png')#分別打開img1,img2,img3并顯示
Label(window, image=image, bg='green').place(x=60 + rw * i, y=500)
結果
本來紅線處還有兩幅圖,總共三幅,但是現在只顯示了最后一幅。
1、原因
在上述代碼最后一行,我們這樣寫:
Label(window, image=image, bg='green').place(x=60 + rw * i, y=500)
在執行過程中,每個image變量會覆蓋上一個image變量,導致運行過程中總是只有1個image參與了繪制,從而只顯示一個image
2、解決
給不同的image分別命不同的名,或者干脆用一個List:
image=[]
for i in range(3):
...
image.append(ImageTk.PhotoImage(f'img{i}.png')
Label(window,image=image[i],bg='green').place(x=60+rw*i,y=500)
補充
pack與place是沒有返回值的,所以在需要對要素進行后續操作時,不要直接將該要素的生成和放置寫在同一句話中,就像這樣:
Label(window, text='直方圖:', font=('宋體', 16)).place(x=0, y=60)
最好分開寫:
L1=Label(window, text='直方圖:', font=('宋體', 16))
L1.place(x=0, y=60)
如果像一開始那樣寫L1=XXX.place(),那么最終得到的L1將是NoneType
原文鏈接:https://www.cnblogs.com/ShineLeBlog/p/15776945.html
相關推薦
- 2023-03-26 react組件實例屬性props實例詳解_React
- 2022-05-05 Entity?Framework使用LINQ操作實體_實用技巧
- 2022-03-28 用python實現一個文件搜索工具_python
- 2022-10-19 python基礎教程之csv文件的寫入與讀取_python
- 2022-07-29 Golang學習之反射機制的用法詳解_Golang
- 2023-10-17 react跨域請求數據(proxy)
- 2022-11-02 Kotlin掛起函數的詳細介紹_Android
- 2022-12-08 c++只保留float型的小數點后兩位問題_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支