我看过其他有关此主题的文章,但它们不是很确定,它们只是建议使用其他模块来完成此任务。
我正在尝试将maincharacter.png
(http://pluspng.com/img-png/png-character--600.png)图像插入我的top_left
框架中。
代码(运行它):
from tkinter import *
from PIL import ImageTk, Image
class GameScreen:
def __init__(self, master):
# create all of the main containers
top_left = Frame(master, bg='black', width=200, height=200)
top_middle = Frame(master, bg='green', width=200, height=200)
top_right = Frame(master, bg="green", width=200, height=200)
middle_left = Frame(master, bg='green', width=200, height=200)
middle = Frame(master, bg='green', width=200, height=200)
middle_right = Frame(master, bg='green', width=200, height=200)
bottom_left = Frame(master, bg='green',width=200, height=200 )
bottom_middle = Frame(master, bg='green', width=200, height=200 )
bottom_right = Frame(master, bg='green', width=200, height=200)
# layout all of the main containers
top_left.grid(row=0)
top_middle.grid(row=0, column = 1)
top_right.grid(row=0, column=2)
middle_left.grid(row=1)
middle.grid(row=1, column=1)
middle_right.grid(row=1, column=2)
bottom_left.grid(row=2)
bottom_middle.grid(row=2, column=1)
bottom_right.grid(row=2, column=2)
c1 = ImageTk.PhotoImage(Image.open('maincharacter.png'))
c1_label = Label(top_left, image=c1)
c1_label.pack()
root = Tk()
root.title("RPG Game")
root.geometry("600x600")
display = GameScreen(root)
root.mainloop()
当我尝试执行此操作时,主要存在两个问题:
编辑-为什么现在会有白线:
答案 0 :(得分:2)
您可以按照以下步骤操作:
from tkinter import *
from PIL import ImageTk, Image
class GameScreen:
def __init__(self, master):
# create all of the main containers
top_left = Frame(master, bg='black', width=200, height=200)
top_middle = Frame(master, bg='green', width=200, height=200)
top_right = Frame(master, bg="green", width=200, height=200)
middle_left = Frame(master, bg='green', width=200, height=200)
middle = Frame(master, bg='green', width=200, height=200)
middle_right = Frame(master, bg='green', width=200, height=200)
bottom_left = Frame(master, bg='green', width=200, height=200)
bottom_middle = Frame(master, bg='green', width=200, height=200)
bottom_right = Frame(master, bg='green', width=200, height=200)
# layout all of the main containers
top_left.grid(row=0, column=0, padx=0, pady=0)
top_middle.grid(row=0, column=1)
top_right.grid(row=0, column=2)
middle_left.grid(row=1, column=0)
middle.grid(row=1, column=1)
middle_right.grid(row=1, column=2)
bottom_left.grid(row=2, column=0)
bottom_middle.grid(row=2, column=1)
bottom_right.grid(row=2, column=2)
image = Image.open("maincharacter.png")
photo = ImageTk.PhotoImage(image.resize((196, 196), Image.ANTIALIAS))
label = Label(top_left, image=photo, bg='green')
label.image = photo
label.pack()
root = Tk()
root.title("RPG Game")
root.geometry("600x600")
display = GameScreen(root)
root.mainloop()
我认为这是一个更好的选择:
from tkinter import *
from PIL import ImageTk, Image
class GameScreen:
def __init__(self, master):
# create all of the main containers
top_left = Frame(master, bg='black', width=200, height=200)
top_middle = Frame(master, bg='green', width=200, height=200)
top_right = Frame(master, bg="green", width=200, height=200)
middle_left = Frame(master, bg='green', width=200, height=200)
middle = Frame(master, bg='green', width=200, height=200)
middle_right = Frame(master, bg='green', width=200, height=200)
bottom_left = Frame(master, bg='green', width=200, height=200)
bottom_middle = Frame(master, bg='green', width=200, height=200)
bottom_right = Frame(master, bg='green', width=200, height=200)
# layout all of the main containers
top_left.grid(row=0, column=0, padx=0, pady=0)
top_middle.grid(row=0, column=1)
top_right.grid(row=0, column=2)
middle_left.grid(row=1, column=0)
middle.grid(row=1, column=1)
middle_right.grid(row=1, column=2)
bottom_left.grid(row=2, column=0)
bottom_middle.grid(row=2, column=1)
bottom_right.grid(row=2, column=2)
# create a canvas to show image on
canvas_for_image = Canvas(top_left, bg='green', height=200, width=200, borderwidth=0, highlightthickness=0)
canvas_for_image.grid(row=0, column=0, sticky='nesw', padx=0, pady=0)
# create image from image location resize it to 200X200 and put in on canvas
image = Image.open('maincharacter.png')
canvas_for_image.image = ImageTk.PhotoImage(image.resize((200, 200), Image.ANTIALIAS))
canvas_for_image.create_image(0, 0, image=canvas_for_image.image, anchor='nw')
root = Tk()
root.title("RPG Game")
root.geometry("600x600")
display = GameScreen(root)
root.mainloop()