提问者:小点点

无法确定如何在TkInter GUI中阻止列和行移动


所以,我正在为我的Python类做一个最终的项目,并正在使用TkINTERs为一个游戏创建一个图形用户界面。如果你愿意,我有一个小精灵,它可以通过按下按钮在图形用户界面上移动。我试图找出如何停止恼人的行和列的大小调整,因为'精灵'移动。我还想知道如何检测牢房是否被占用。如果我能以某种方式看到网格的网格线,那就太好了,但我认为这是不可能的,所以下一个最好的事情就是知道如何永久设置网格的列和行,并使它们不可更改。对不起,如果我不清楚我想要什么,我只是脑子里有几十个想法。

这是到目前为止我的代码

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个答案

匿名用户

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

匿名用户

如果您使用的是Python3,那么您的代码会有一些问题。十、

>

  • 第一个问题是,您试图使用 。png文件,而 PhotoImageclass仅接受 。gif。pgm,或 。ppm文件。你可以在 照片文件。
  • 下一个问题是警告,您正在函数末尾将变量设置为global。python的工作方式是,如果在函数中声明了一个变量,那么它是本地的,所以通常在函数的开头告诉它,这个变量是全局的,然后继续修改它。如果不是,你不能确定你正在处理的变量是你想要的全局变量
  • 对于调整网格大小,最好的选择是将按钮放在网格内,网格内,使其大小独立于主网格中单元格的大小。最好是在一个框架上显示,而不是直接在母版上显示。我会将您的代码更改为(并将标签s和框架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)
    

    知道某个元素的位置很容易维护,只需拥有一个初始为空的矩阵,当您将某个元素放置在某一行和某列时,将该矩阵的索引更改为True,然后只需检查位于[i][j]位置的矩阵,查看它是否被占用

    希望这有帮助。