短信被插入到两个不同的盒子里,而它只应该被插入到一个盒子里

Texting being inserted into two different boxes when it should only be inserted into one

我正在 python 制作基于文本的冒险 'heist' 游戏。我为它设计了一个 GUI,它使用四个更改文本和命令的按钮进行控制。我还包含了一些文本框来显示玩家当前的物品栏、分数和可用金钱。

然而,当当前可用货币(money)分数应该被插入相关框(MoneyAvailableTextBox)时,它是插入了HeistAwardTextBox.

当我尝试更新抢劫奖励箱 (HeistAwardTextBox) 时出现同样的问题,抢劫奖励分数 (heistaward) 被插入两个抢劫奖励箱 (HeistAwardTextBox) 和可用钱框 (MoneyAvailableTextBox).

这是相关代码的编辑示例。

import tkinter as tk
from tkinter import*
import pygame

pygame.init()
temp = Tk()
temp.title('EdRevGames')
temp.geometry('1352x752+0+0')
temp.configure(background = 'white')

#This is how much money the player currently has
money = 10
heistaward = 0

#This updates how much money the user has
def updateStats():
    MoneyAvailableTextBox.delete('0', END)
    MoneyAvailableTextBox.insert(tk.INSERT, money)
    HeistAwardTextBox.delete('0', END)
    HeistAwardTextBox.insert(tk.INSERT, heistaward)

# These are the text boxes and labels
MoneyLabel = Label(temp, font=('arial', 14, 'bold'), text = 'Money Available', bg='black', fg = 'white', bd = 5, justify = LEFT)
MoneyLabel.grid(row = 0, column = 0)
MoneySignLabel = Label(temp, font=('arial', 14, 'bold'), text = '£', bg='black', fg = 'white', bd = 5, justify = LEFT)
MoneySignLabel.grid(row = 1, column = 0, sticky = W)
MoneyAvailableTextBox = Entry(temp, font=('arial', 14, 'bold'), bg='White', fg = 'Black', bd = 5, width = 10, borderwidth = 4,  justify = CENTER, text = '£')
MoneyAvailableTextBox.grid(row = 1, column = 0, pady = 10)
HeistAwardLabel = Label(temp, font=('arial', 14, 'bold'), text = 'Heist Award', bg='black', fg = 'white', bd = 5, justify = LEFT)
HeistAwardLabel.grid(row = 2, column = 0)
HeistAwardSignLabel = Label(temp, font=('arial', 14, 'bold'), text = '£', bg='black', fg = 'white', bd = 5, justify = LEFT)
HeistAwardSignLabel.grid(row = 3, column = 0, sticky = W)
HeistAwardTextBox = Entry(temp, font=('arial', 14, 'bold'), bg='White', fg = 'Black', bd = 5, width = 10, borderwidth = 4,  justify = CENTER, text = '£')
HeistAwardTextBox.grid(row = 3, column = 0, pady = 10)

#Button which when pressed updates money avaible
Button1 = Button(temp, font=('arial', 14, 'bold'), bg='blue', fg = 'white', bd = 1, width = 17, height = 2, justify = CENTER, command = updateStats, text = 'Update')
Button1.grid(row = 4)

temp.mainloop()

有什么我想念的吗?或者无论如何要解决这个问题?

好吧,我问了一个朋友他是否可以看一下我的代码,他玩了一会儿后设法修复了它。我想我会 post 回答我自己的问题,以防其他人遇到这个问题。

问题出在这些代码行上:

MoneyAvailableTextBox = Entry(temp, font=('arial', 14, 'bold'), bg='White', fg = 'Black', bd = 5, width = 10, borderwidth = 4,  justify = CENTER, text = '£')
MoneyAvailableTextBox.grid(row = 1, column = 0, pady = 10)

HeistAwardTextBox = Entry(temp, font=('arial', 14, 'bold'), bg='White', fg = 'Black', bd = 5, width = 10, borderwidth = 4,  justify = CENTER, text = '£')
HeistAwardTextBox.grid(row = 3, column = 0, pady = 10)

由于两者共享相同的“text = '£'”,因此文本被插入到它们中。通过删除“text = '£'”或将其值更改为其他值(因此它们不同),问题就停止了。

MoneyAvailableTextBox = Entry(temp, font=('arial', 14, 'bold'), bg='White', fg = 'Black', bd = 5, width = 10, borderwidth = 4,  justify = CENTER)
MoneyAvailableTextBox.grid(row = 1, column = 0, pady = 10)

HeistAwardTextBox = Entry(temp, font=('arial', 14, 'bold'), bg='White', fg = 'Black', bd = 5, width = 10, borderwidth = 4,  justify = CENTER)
HeistAwardTextBox.grid(row = 3, column = 0, pady = 10)

不太清楚为什么,我对 python 还是个新手,但它似乎有效。我最好的猜测是“text = '£'”是如何将文本框链接在一起的。不过我不确定,如果有人可以添加或解释原因,请添加。