0
我在Python中實現了k-NN算法,該算法返回輸入的類標籤。我需要的是在輸出即將到來時(通過刷新窗口)在一個窗口中顯示分配給類標籤的圖像。問題是,我在GUI編程方面不是很有經驗,因此我需要一些資源和幫助來開始。你可以推薦哪些圖書館,書籍和教程?代碼片斷也將讚賞。在一個窗口中顯示基於輸出的圖像(Python)
我在Python中實現了k-NN算法,該算法返回輸入的類標籤。我需要的是在輸出即將到來時(通過刷新窗口)在一個窗口中顯示分配給類標籤的圖像。問題是,我在GUI編程方面不是很有經驗,因此我需要一些資源和幫助來開始。你可以推薦哪些圖書館,書籍和教程?代碼片斷也將讚賞。在一個窗口中顯示基於輸出的圖像(Python)
有很多庫允許你添加圖像到你的程序,如Turtle,PIL.ImagTk和Canvas,你甚至可以使用Pygame來獲得最佳性能。
PIL.ImagTk和Canvas:
from Tkinter import *
from PIL import ImageTk
backgroundImage = PhotoImage("image path (gif/PPM)")
canvas = Canvas(width = 200, height = 200, bg = 'blue')
canvas.pack(expand = YES, fill = BOTH)
image = ImageTk.PhotoImage(file = "C:/Python27/programas/zimages/gato.png")
canvas.create_image(10, 10, image = image, anchor = NW)
mainloop()
龜:
import turtle
screen = turtle.Screen()
# click the image icon in the top right of the code window to see
# which images are available in this trinket
image = "rocketship.png"
# add the shape first then set the turtle shape
screen.addshape(image)
turtle.shape(image)
screen.bgcolor("lightblue")
move_speed = 10
turn_speed = 10
# these defs control the movement of our "turtle"
def forward():
turtle.forward(move_speed)
def backward():
turtle.backward(move_speed)
def left():
turtle.left(turn_speed)
def right():
turtle.right(turn_speed)
turtle.penup()
turtle.speed(0)
turtle.home()
# now associate the defs from above with certain keyboard events
screen.onkey(forward, "Up")
screen.onkey(backward, "Down")
screen.onkey(left, "Left")
screen.onkey(right, "Right")
screen.listen()
現在,讓我們改變背景
import turtle
screen = turtle.Screen()
# this assures that the size of the screen will always be 400x400 ...
screen.setup(400, 400)
# ... which is the same size as our image
# now set the background to our space image
screen.bgpic("space.jpg")
# Or, set the shape of a turtle
screen.addshape("rocketship.png")
turtle.shape("rocketship.png")
move_speed = 10
turn_speed = 10
# these defs control the movement of our "turtle"
def forward():
turtle.forward(move_speed)
def backward():
turtle.backward(move_speed)
def left():
turtle.left(turn_speed)
def right():
turtle.right(turn_speed)
turtle.penup()
turtle.speed(0)
turtle.home()
# now associate the defs from above with certain keyboard events
screen.onkey(forward, "Up")
screen.onkey(backward, "Down")
screen.onkey(left, "Left")
screen.onkey(right, "Right")
screen.listen()