【发布时间】:2016-03-04 07:41:26
【问题描述】:
所以,我正在为我的 Python 类做一个最终项目,并正在使用 TkInter 为游戏创建一个 GUI。如果您愿意使用按钮在 GUI 上移动,我有一个小“精灵”。我试图弄清楚如何在“精灵”四处移动时停止烦人的行和列的重新调整大小。我也想知道如何检测一个单元格是否被占用。如果我能以某种方式看到网格的网格线,那就太好了,但我认为这是不可能的,所以下一个最好的事情是知道如何永久设置网格的列和行并使其不可更改。如果我不清楚我想要什么,我很抱歉,我脑子里只有几十个想法。
这是我目前的代码
from tkinter import *
from tkinter.constants import *
master= Tk()
master.resizable(width=False, height=False)
#for erasing displayed text when not needed anymore
wordsShowing = 0
#for testing button function with map movement
#url for image is http://files.softicons.com/download/game-icons/minecraft-avatars-icons-by-stefan-kroeber/png/50x50/slime.png if you want to see exactly what im seeing
pic = PhotoImage(file="C:\\Users\\Bill\\Desktop\\Python\\Final\\slime.png")
image = Label(master, image=pic)
image.grid(row=0, column=3, columnspan=1, rowspan=1, padx=0, pady=0)
#button functions (im not sure which buttons we will actually be using but im trying to cover all our bases)
def left():
while wordsShowing == 1:
varLabel.grid_remove()
wordsShowing = 0
info = image.grid_info()
move = info["column"]
stay = info["row"]
if move > 0:
image.grid_remove()
image.grid(row=stay, column=move-1, columnspan=1, rowspan=1, padx=0, pady=0)
else:
varLabel = Label(master, text='Sorry, you can not go that direction.')
varLabel.grid(row=1, column=2, rowspan=4)
wordsShowing = 1
global wordsShowing
global varLabel
def right():
while wordsShowing == 1:
varLabel.grid_remove()
wordsShowing = 0
info = image.grid_info()
move = info["column"]
stay = info["row"]
if move < 4:
image.grid_remove()
image.grid(row=stay, column=move+1, columnspan=1, rowspan=1, padx=0, pady=0)
else:
varLabel = Label(master, text='Sorry, you can not go that direction.')
varLabel.grid(row=1, column=2, rowspan=4)
wordsShowing = 1
global wordsShowing
global varLabel
def down():
while wordsShowing == 1:
varLabel.grid_remove()
wordsShowing = 0
info = image.grid_info()
move = info["row"]
stay = info["column"]
if move < 5:
image.grid_remove()
image.grid(row=move+1, column=stay, columnspan=1, rowspan=1, padx=0, pady=0)
else:
varLabel = Label(master, text='Sorry, you can not go that direction.')
varLabel.grid(row=1, column=2, rowspan=4)
wordsShowing = 1
global wordsShowing
global varLabel
def up():
while wordsShowing == 1:
varLabel.grid_remove()
wordsShowing = 0
info = image.grid_info()
move = info["row"]
stay = info["column"]
if move > 0:
image.grid_remove()
image.grid(row=move-1, column=stay, columnspan=1, rowspan=1, padx=0, pady=0)
else:
varLabel = Label(master, text='Sorry, you can not go that direction.')
varLabel.grid(row=1, column=2, rowspan=4)
wordsShowing = 1
global wordsShowing
global varLabel
def submit():
var = command.get()
varLabel = Label(master, text=var)
varLabel.grid(row=1, column=2, rowspan=4)
wordsShowing = 1
global wordsShowing
global varLabel
#created widgets
label1 = Label(master, text="Enter a command:")
command = Entry(master, width=80)
leftButton = Button(master, text="<", command=left)
rightButton = Button(master, text=">", command=right)
downButton = Button(master, text="v", command=down)
upButton = Button(master, text="^", command=up)
submitButton = Button(master, text="SUBMIT", command=submit)
#display widgets
label1.grid(row=5, column=1, sticky=E)
command.grid(row=5, column=2)
leftButton.grid(row=2, column=2, sticky=E, padx=3)
rightButton.grid(row=2, column=3)
downButton.grid(row=3, column=3, sticky=W)
upButton.grid(row=1, column=3, sticky=W)
submitButton.grid(row=5, column=3, pady=5, padx=5)
您能提供的任何帮助将不胜感激。谢谢。
【问题讨论】:
-
我假设您是故意使用 Tkinter?我的意思是,你正在制作游戏,你首先看的是 Pygame 对吧?
-
您的代码有点长,我认为所有这些代码都与您的问题无关。请参阅:minimal reproducible example。此外,要绘制网格,您可能需要检查
canvas.create_line() -
另外,你检查过
grid_propagate()吗? -
除非按钮和精灵混合是游戏固有的一部分,否则我不会将它们混合在同一帧中。如果我确实想要它们在一起,我会使用画布并将按钮放在画布上 infohost.nmt.edu/tcc/help/pubs/tkinter/web/create_window.html 并在画布周围移动精灵。
-
我想我应该做更多的研究。我不知道 Pygame。 facepalm 抱歉,代码太长了......跨度>
标签: python python-3.x tkinter grid-layout