如何在 tkinter canvas 网格中随机放置图像
How to randomly place images in a tkinter canvas grid
我正在尝试创建一个将图像放置在网格的随机单元格中的游戏。
我设法创建了一个网格,并尝试使用它来随机放置图像,但它似乎并没有像我希望的那样工作。
try:
import tkinter as tk
from tkinter import ttk
from tkinter.ttk import *
except ImportError:
import Tkinter as tk
from Tkinter import ttk
from Tkinter.ttk import *
import random
from random import randint
window = tk.Tk()
style = ttk.Style(window)
style.configure("BW.TLabel")
game_frame = tk.Frame(window)
game_grid = tk.Canvas(game_frame, width=450, height=450, borderwidth=0,
highlightthickness=0)
def grid_create():
global row, col, cell_width, cell_height, rect, rows , columns
rows = 8
columns = 8
cell_width = 50
cell_height = 50
rect = {}
for i in (game_grid, game_frame):
i.pack()
for column in range(8):
for row in range(8):
x1 = column*cell_width
y1 = row * cell_height
x2 = x1 + cell_width
y2 = y1 + cell_height
rect[row,column] = game_grid.create_rectangle(x1,y1,x2,y2,
fill="green",
tags="rect")
grid_draw()
def grid_draw():
global row, col
game_grid.itemconfig("rect", fill="green")
for i in range(8):
row = random.randint(0,8)
col = random.randint(0,8)
create_objects()
def create_objects():
rows = 8
columns = 8
cell_width = 50
cell_height = 50
bandits = 5
bandit_list = {}
bandit_img = tk.PhotoImage(file="Bandit.png")
for column in range(randint(0,8)):
for row in range(randint(0,8)):
x1 = column*cell_width + 22
y1 = row * cell_height - 22
x2 = x1 + cell_width
y2 = y1 + cell_height
bandit_list[row, column] = game_grid.create_image(x1, y2,
image=bandit_img)
for i in range(randint(0,5)):
row = random.randint(0,8)
col = random.randint(0,8)
grid_create()
window.mainloop()
我只想在随机单元格中显示五个 'bandits' - 因此 bandits = 5
- 但我似乎无法弄清楚如何做到这一点。我也不希望任何 'bandits' 重叠。感谢任何帮助。
图片:
Link to image as I do not have the required reputation to embed it here.
PhotoImage
和 "Garbage Collector"
存在问题,它们删除(从内存中)分配给函数中局部变量的图像。
您必须将其分配给全局变量即。
# create global variable
bandit_img = None
def create_objects():
# inform function to use global varia
global bandit_img
bandit_img = tk.PhotoImage(file="Bandit.png")
或小部件实例:请参阅第 http://effbot.org/tkinterbook/photoimage.htm
页上的 "Note"
要仅获取 5 个强盗,您必须获取随机值 (row,col)
并检查它是否不存在于强盗列表中。如果 (row,col)
存在于列表中,那么您将获得随机值并再次检查。当您找到唯一的 (row,col)
时,您可以使用 while True
循环和 break
离开循环
try:
import tkinter as tk
import tkinter.ttk as ttk
except ImportError:
import Tkinter as tk
import ttk
import random
# --- constants --- (UPPER_CASE names)
ROWS = 8
COLUMNS = 8
CELL_WIDTH = 50
CELL_HEIGHT = 50
BANDITS_NUMBER = 5
# --- functions ---
def create_grid():
data = {}
for col in range(COLUMNS):
for row in range(ROWS):
x1 = col * CELL_WIDTH
y1 = row * CELL_HEIGHT
x2 = x1 + CELL_WIDTH
y2 = y1 + CELL_HEIGHT
data[row, col] = game_grid.create_rectangle(x1, y1, x2, y2,
fill="green", tags="rect")
return data
def create_bandits(image):
data = {}
for i in range(BANDITS_NUMBER):
while True:
row = random.randint(0, ROWS-1)
col = random.randint(0, COLUMNS-1)
if (row,col) not in data:
break
x1 = col * CELL_WIDTH + 22
y1 = row * CELL_HEIGHT - 22
x2 = x1 + CELL_WIDTH
y2 = y1 + CELL_HEIGHT
data[row, col] = game_grid.create_image(x1, y2, image=image)
return data
# --- main ---
# - init -
window = tk.Tk()
game_frame = tk.Frame(window)
game_frame.pack()
game_grid = tk.Canvas(game_frame, width=450, height=450, borderwidth=0,
highlightthickness=0)
game_grid.pack()
game_grid.itemconfig("rect", fill="green")
# - data -
rects = create_grid()
# create global variable
bandit_image = tk.PhotoImage(file="Bandit.png")
# send image to function - so you don't need word "global"
bandits = create_bandits(bandit_image)
# - start -
window.mainloop()
我正在尝试创建一个将图像放置在网格的随机单元格中的游戏。 我设法创建了一个网格,并尝试使用它来随机放置图像,但它似乎并没有像我希望的那样工作。
try:
import tkinter as tk
from tkinter import ttk
from tkinter.ttk import *
except ImportError:
import Tkinter as tk
from Tkinter import ttk
from Tkinter.ttk import *
import random
from random import randint
window = tk.Tk()
style = ttk.Style(window)
style.configure("BW.TLabel")
game_frame = tk.Frame(window)
game_grid = tk.Canvas(game_frame, width=450, height=450, borderwidth=0,
highlightthickness=0)
def grid_create():
global row, col, cell_width, cell_height, rect, rows , columns
rows = 8
columns = 8
cell_width = 50
cell_height = 50
rect = {}
for i in (game_grid, game_frame):
i.pack()
for column in range(8):
for row in range(8):
x1 = column*cell_width
y1 = row * cell_height
x2 = x1 + cell_width
y2 = y1 + cell_height
rect[row,column] = game_grid.create_rectangle(x1,y1,x2,y2,
fill="green",
tags="rect")
grid_draw()
def grid_draw():
global row, col
game_grid.itemconfig("rect", fill="green")
for i in range(8):
row = random.randint(0,8)
col = random.randint(0,8)
create_objects()
def create_objects():
rows = 8
columns = 8
cell_width = 50
cell_height = 50
bandits = 5
bandit_list = {}
bandit_img = tk.PhotoImage(file="Bandit.png")
for column in range(randint(0,8)):
for row in range(randint(0,8)):
x1 = column*cell_width + 22
y1 = row * cell_height - 22
x2 = x1 + cell_width
y2 = y1 + cell_height
bandit_list[row, column] = game_grid.create_image(x1, y2,
image=bandit_img)
for i in range(randint(0,5)):
row = random.randint(0,8)
col = random.randint(0,8)
grid_create()
window.mainloop()
我只想在随机单元格中显示五个 'bandits' - 因此 bandits = 5
- 但我似乎无法弄清楚如何做到这一点。我也不希望任何 'bandits' 重叠。感谢任何帮助。
图片: Link to image as I do not have the required reputation to embed it here.
PhotoImage
和 "Garbage Collector"
存在问题,它们删除(从内存中)分配给函数中局部变量的图像。
您必须将其分配给全局变量即。
# create global variable
bandit_img = None
def create_objects():
# inform function to use global varia
global bandit_img
bandit_img = tk.PhotoImage(file="Bandit.png")
或小部件实例:请参阅第 http://effbot.org/tkinterbook/photoimage.htm
页上的 "Note"要仅获取 5 个强盗,您必须获取随机值 (row,col)
并检查它是否不存在于强盗列表中。如果 (row,col)
存在于列表中,那么您将获得随机值并再次检查。当您找到唯一的 (row,col)
while True
循环和 break
离开循环
try:
import tkinter as tk
import tkinter.ttk as ttk
except ImportError:
import Tkinter as tk
import ttk
import random
# --- constants --- (UPPER_CASE names)
ROWS = 8
COLUMNS = 8
CELL_WIDTH = 50
CELL_HEIGHT = 50
BANDITS_NUMBER = 5
# --- functions ---
def create_grid():
data = {}
for col in range(COLUMNS):
for row in range(ROWS):
x1 = col * CELL_WIDTH
y1 = row * CELL_HEIGHT
x2 = x1 + CELL_WIDTH
y2 = y1 + CELL_HEIGHT
data[row, col] = game_grid.create_rectangle(x1, y1, x2, y2,
fill="green", tags="rect")
return data
def create_bandits(image):
data = {}
for i in range(BANDITS_NUMBER):
while True:
row = random.randint(0, ROWS-1)
col = random.randint(0, COLUMNS-1)
if (row,col) not in data:
break
x1 = col * CELL_WIDTH + 22
y1 = row * CELL_HEIGHT - 22
x2 = x1 + CELL_WIDTH
y2 = y1 + CELL_HEIGHT
data[row, col] = game_grid.create_image(x1, y2, image=image)
return data
# --- main ---
# - init -
window = tk.Tk()
game_frame = tk.Frame(window)
game_frame.pack()
game_grid = tk.Canvas(game_frame, width=450, height=450, borderwidth=0,
highlightthickness=0)
game_grid.pack()
game_grid.itemconfig("rect", fill="green")
# - data -
rects = create_grid()
# create global variable
bandit_image = tk.PhotoImage(file="Bandit.png")
# send image to function - so you don't need word "global"
bandits = create_bandits(bandit_image)
# - start -
window.mainloop()