我正在嘗試學習一些Python和Tkinter。下面的示例代碼旨在在屏幕上放置兩個窗口,幾個按鈕和一個帶有圖像的畫布,並在其上繪製一些線條。Tkinter畫布不顯示
窗口和按鈕顯示得很好,但我沒有看到畫布圖像或畫布線。我很感謝一些幫助,弄清楚什麼是需要讓我的畫布顯示。
from Tkinter import *
import Image, ImageTk
class App:
def __init__(self, master):
def scrollWheelClicked(event):
print "Wheel wheeled"
frame = Frame(master)
frame.pack()
self.button = Button(frame, text = "QUIT", fg="red", command=frame.quit)
self.button.pack(side=LEFT)
self.hi_there = Button(frame, text="Hello", command=self.say_hi)
self.hi_there.pack(side=LEFT)
top = Toplevel()
canvas = Canvas(master=top, width=600, height=600)
image = Image.open("c:\lena.jpg")
photo = ImageTk.PhotoImage(image)
item = canvas.create_image(0, 0, image=photo)
canvas.create_line(0, 0, 200, 100)
canvas.create_line(0, 100, 200, 0, fill="red", dash=(4, 4))
canvas.create_rectangle(50, 25, 150, 75, fill="blue")
canvas.pack
testBtn = Button(top, text = "test button")
testBtn.pack()
def say_hi(self):
print "hi there everyone!"
root = Tk()
app = App(root)
root.mainloop()
對於圖像,在將包改爲pack()後,將圖像線修改爲: self.image = Image.open(「c:\ lena.jpg」) self.photo = ImageTk.PhotoImage(self.image) canvas.create_image(0,0,image = self.photo) 我認爲這與垃圾回收有關 – SooDesuNe 2010-02-08 19:01:39