2016-03-04 35 views
1

因此,我正在研究我的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) 

任何幫助您能給將不勝感激。謝謝。

+0

我假設你故意使用Tkinter?我的意思是,你在製作一款遊戲,你首先看了Pygame吧? – Fauxpas

+0

您的代碼有點長,我不認爲所有這些代碼都與您的問題有關。請參閱:[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

+0

此外,你檢查['grid_propagate()'](http://effbot.org/tkinterbook/grid.htm #Tkinter.Grid。grid_propagate方法)? – Lafexlos

回答

0

那麼如果你正在使用Python 3.X,那麼你的代碼有幾個問題。

  • 第一個問題是,你試圖用一個 .png文件,而 PhotoImage類只接受 .gif.pgm,或 .ppm文件。您可以閱讀 PhotoImage Doc的文檔。
  • 下一個問題,一個警告,是你的變量設置爲global在你的函數結束。 python的工作方式是,如果一個變量是在函數內部聲明的,那麼它就是本地的,所以你通常做的是在函數的開頭告訴它,確定這個變量是全局變量,然後繼續修改它。如果不是,你不能確定你正在使用的變量是你想要的全局變量。
  • 對於網格大小調整,最好的選擇是將按鈕放在網格內部,使其大小與主網格中單元格的大小無關。最好在框架上顯示,而不是直接在主屏幕上顯示。我會改變你的代碼是這樣的(和函數代替Label S和Frame s到構造上master建設上gridFrame):

    WIDTH = 200 
    HEIGHT = 200 
    
    master= Tk() 
    master.resizable(width=False, height=False) 
    gridFrame = Frame(master, width=WIDTH, height=HEIGHT) 
    gridFrame.grid() 
    
    # Your function code goes here 
    
    buttonGrid = Frame(gridFrame) 
    buttonGrid.grid(row=4, column=3, sticky=N) 
    # created widgets 
    label1 = Label(gridFrame, text="Enter a command:") 
    command = Entry(gridFrame, width=80) 
    leftButton = Button(buttonGrid, text="<", command=left) 
    rightButton = Button(buttonGrid, text=">", command=right) 
    downButton = Button(buttonGrid, text="v", command=down) 
    upButton = Button(buttonGrid, text="^", command=up) 
    submitButton = Button(gridFrame, text="SUBMIT", command=submit) 
    
    # display widgets 
    label1.grid(row=5, column=1, sticky=E) 
    command.grid(row=5, column=2) 
    leftButton.grid(row=1, column=0, sticky=E) 
    rightButton.grid(row=1, column=2, sticky=W) 
    downButton.grid(row=2, column=1, sticky=N) 
    upButton.grid(row=0, column=1, sticky=S) 
    submitButton.grid(row=5, column=3, pady=5, padx=5) 
    
  • 知道一些位在那裏的東西是可以很容易地通過簡單maintainted具有矩陣最初爲空的,當你把東西在一定的行和列,矩陣的那些指標改爲True,然後簡單地檢查矩陣在[i][j]位置,看它是否被佔用

希望有所幫助。

+0

對不起,我忘了補充一點,當你在tkinter的網格中定位時,從0開始的編程索引被保留,所以如果你想讓圖像不能進入'輸入命令:'標籤,你可以設置它索引列爲0而不是1. – Mixone

+1

tk 8.6包含在Windows的3.4和3.5中,顯示.png。我相信,鏈接的文檔大約有十年左右的時間。 –

+0

約束依然存在,我用python 3.5試了一下。 – Mixone

相關問題