简体   繁体   English

无法弄清楚如何阻止列和行在TkInter GUI中移位

[英]Cant figure out how to stop columns and rows from shifting in TkInter GUI

So, I am working on a final project for my Python class and am creating a GUI for a game using TkInter. 因此,我正在为我的Python类开发一个最终项目,并使用TkInter为游戏创建GUI。 I have a little 'sprite' if you will that moves across the GUI using button presses. 如果您要使用按钮在GUI上移动,我会有一个“小精灵”。 I am trying to figure out how to stop the annoying re-sizing of the rows and columns as the 'sprite' moves around. 我试图弄清楚如何在“ sprite”移动时停止烦人的行和列大小调整。 I would also like to know how to detect if a cell is occupied or not. 我还想知道如何检测一个单元是否被占用。 It would really be great if I could somehow see the grid lines of the grid, but I dont think that is possible so the next best thing would be knowing how to permanently set up the grids columns and rows and make them unchangeable. 如果我能以某种方式看到网格的网格线,那将真的很棒,但是我认为这是不可能的,因此,下一个最好的事情就是知道如何永久设置网格的列和行并使它们不变。 I'm sorry if I'm not being clear on what I want I just have dozens of ideas running around in my head. 很抱歉,如果我不清楚我想要什么,我脑子里只有几十个想法。

here is my code so far 到目前为止,这是我的代码

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)

Any help you can give would be greatly appreciated. 您将给予的任何帮助将不胜感激。 Thank you. 谢谢。

A minimum size for each grid cell can be set with the function .minsize. 可以使用函数.minsize设置每个网格单元的最小尺寸。 If you set a minimum size that's always larger than the widgets you put into each cell, the cells will not change size as you move things around. 如果您设置的最小大小始终大于您放入每个单元格中的小部件的大小,则在移动内容时这些单元格将不会更改大小。 See this manual page: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/grid-config.html . 请参见此手册页: http : //infohost.nmt.edu/tcc/help/pubs/tkinter/web/grid-config.html

Well there are a few issues with your code if you are working on Python 3.X. 好吧,如果您使用的是Python 3.X,则您的代码会有一些问题。

  • The first problem is that you are trying to use a .png file whereas the PhotoImage class only accepts .gif , .pgm , or .ppm files. 第一个问题是您尝试使用 .png文件,而 PhotoImage类仅接受 .gif.pgm.ppm文件。 You can read the doc at PhotoImage Doc . 您可以在 PhotoImage Doc上阅读该文档。
  • The next issue, a warning, is that you are setting variables as global at the end of your functions. 下一个问题是警告,是在函数末尾将变量设置为global变量。 The way python works is that if a variable is declared inside a function it is local, so what you normally do is tell it at the start of the function, ok this variable is global, and THEN proceed to modifying it. python的工作方式是,如果在函数内部声明了变量,则该变量是局部的,因此通常要做的是在函数开始时告诉它,好的,此变量是全局变量,然后继续对其进行修改。 If not you can't be sure that the variable you are working on is the global one you want. 如果不是,您将无法确定您正在使用的变量是您想要的全局变量。
  • For the grid resizing, your best option is to put the buttons inside a grid, inside the grid so that their size is independent of the size of cells in the main grid. 对于调整网格大小,最好的选择是将按钮放在网格内部,即网格内部,使它们的大小与主网格中单元格的大小无关。 And it is better to display things on a frame rather than directly on master. 而且最好将内容显示在框架上而不是直接显示在母版上。 I would change your code to be something like (and replace Label s and Frame s from functions to construct on master to construct on gridFrame ): 我将您的代码更改为类似的代码(并从功能上替换LabelFrame ,以在master上构建, gridFramegridFramegridFrame ):

     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) 
  • The bit about knowing where something is can be easily maintainted by simply having a matrix that is initially empty and when you place something at a certain row and column, change those indices of the matrix to True and then simply check the matrix at position [i][j] to see if it is occupied 通过简单地使矩阵最初为空,然后将其放置在特定的行和列上,将矩阵的那些索引更改为True ,然后简单地检查位置[i][j]看它是否被占用

Hope that helped. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM