因此,我正在研究我的Python類的最終項目,並使用TkInter爲遊戲創建GUI。我有一個'精靈',如果你願意通過按下按鈕在GUI上移動。我試圖找出如何在「精靈」四處移動時停止行和列的煩人重新調整大小。我也想知道如何檢測一個單元格是否被佔用。如果我能以某種方式看到網格的網格線,那真的很棒,但我不認爲這是可能的,因此下一個最好的事情就是知道如何永久設置網格的列和行,並使它們不可更改。我很抱歉,如果我不清楚自己想要什麼,我只有幾十個想法在我腦海中流傳。不知道如何停止在TkInter 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吧? – Fauxpas
您的代碼有點長,我不認爲所有這些代碼都與您的問題有關。請參閱:[MCVE]。此外,要繪製網格,您可能需要檢查['canvas'](http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/create_line.html)。['create_line()'] (http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.create_line-method) – Lafexlos
此外,你檢查['grid_propagate()'](http://effbot.org/tkinterbook/grid.htm #Tkinter.Grid。grid_propagate方法)? – Lafexlos