gpt4 book ai didi

python - Tkinter 如何将图像放入框架中

转载 作者:太空宇宙 更新时间:2023-11-03 11:14:15 25 4
gpt4 key购买 nike

我看过其他关于这个主题的帖子,但他们不是很有结论,他们只是提供了使用其他模块来做这件事的建议。

我正在尝试将我的 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()

当我尝试这样做时,主要有两个问题:

  • 我所有的框架都错位了
  • 我的角色可能太大了

编辑 - 为什么现在有白线:

GUI

最佳答案

你可以按照这个:

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()

关于python - Tkinter 如何将图像放入框架中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54716337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com