无法弄清楚如何在TkInter GUI

时间:2016-03-04 07:41:27

标签: python python-3.x tkinter grid-layout

所以,我正在为我的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)

非常感谢您提供的任何帮助。谢谢。

2 个答案:

答案 0 :(得分:0)

可以使用函数.minsize设置每个网格单元的最小大小。如果您设置的最小大小始终大于放入每个单元格的小部件,则单元格在移动时不会更改大小。请参阅本手册页:http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/grid-config.html

答案 1 :(得分:0)

如果您正在使用Python 3.X,那么您的代码会出现一些问题。

  • 第一个问题是您尝试使用.png文件,而PhotoImage班只接受.gif.pgm或{{1} }文件。您可以在PhotoImage Doc
  • 阅读文档
  • 下一个问题是警告,即您在函数结束时将变量设置为.ppm。 python的工作方式是,如果一个变量在函数内声明它是本地的,那么你通常做的是在函数的开头告诉它,确定这个变量是全局的,然后继续修改它。如果不是,您无法确定您正在处理的变量是否是您想要的全局变量。
  • 对于网格大小调整,最好的选择是将按钮放在网格内,网格内,使其大小与主网格中单元格的大小无关。最好在框架上显示内容而不是直接显示在主框架上。我会将您的代码更改为(并将globalLabel替换为Frame上构造的函数以构建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) 然后只需检查位置True的矩阵,看它是否被占用

希望有所帮助。