[英]tkinter to display the images
我是python的新手,我有一個代碼不會在Tkinter上打印圖像,因此請幫助我如何顯示圖像以及Button和Textbox。
碼:
import Tkinter
from Tkinter import *
class myproject(Tkinter.Tk):
def __init__(self,parent):
Tkinter.Tk.__init__(self)
self.button2()
self.text()
self.image()
def button2(self):
button2 = Tkinter.Button(self, text = "hello")
button2.grid(column=5,row=7)
def text(self):
text = Tkinter.Text(self, height=3, width=31) # self.text
text.grid(column=1,row=3)
text.insert(END, "Wiilliam Skakespeare")
def image(self):
logo = PhotoImage(file="linux.gif")
w1 = Tkinter.Label(self, image=logo).pack(side="right")
app = myproject(None)
app.mainloop()
請幫助!答案將不勝感激!
您必須保存對照片圖像的引用。
但是,您發布的代碼還有很多其他問題。 例如,您需要在函數和類聲明后加冒號。 發布代碼時,類中也不需要多余的方法,只會使它們更難理解
您也不能混合使用管理器,否則整個程序可能會停頓。 這意味着您不應該在同一程序中使用pack和grid 。 通讀effbot教程,這真的很有幫助!
class myproject(Tkinter.Tk):
def __init__(self,parent):
Tkinter.Tk.__init__(self)
self.image()
def image(self):
logo = Tkinter.PhotoImage(file='linux.gif')
self.logo = logo # You always need a reference to the image or it gets garbage collected
w1 = Tkinter.Label(self, image=logo).grid()
app = myproject(None)
app.mainloop()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.