0

I'm making a text based adventure 'heist' game in python. I've designed a GUI for it, which is controlled using four buttons which change text and command. I also included a few text boxes to show the player's current inventory, score and money available.

However, when the current money available (money) score is supposed to be inserted into the relevant box (MoneyAvailableTextBox), it is also inserted into the HeistAwardTextBox.

The same problem appears when I try to update the heist award box (HeistAwardTextBox), where the heist award score (heistaward) is inserted into both heist award box (HeistAwardTextBox) and money available box (MoneyAvailableTextBox).

Here is an edited sample of the relevant code.

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()

Is there something I'm missing? Or anyway to solve this?

  • 1
    There is no code on updating the two text boxes. Better provide [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – acw1668 Jan 11 '21 at 13:10
  • Please fix the indentation of your code, and also explain what you've done to debug this. There appears to be some unrelated code in your example, please try to reduce the code down to a [mcve]. – Bryan Oakley Jan 11 '21 at 16:40
  • I've updated the question. I've included all the necessary code for it run plus the labels. I've tried a few things to try and fix the error (I renamed of the text boxes for example) but there nothing which I can see which causes the error. I'm still very new to python, so I may be just doing a stupid mistake. Any help will be appreciated. –  Jan 12 '21 at 10:37

1 Answers1

0

Well, I asked a friend if he can take a look at my code and he managed to fix it after playing around with it for a bit. I thought I'd post an answer to my own question in case anyone else comes across this problem.

The problem was with these lines of code:

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)

Since both share the same "text = '£'", the text is inserted into both of them. By deleting the "text = '£'" or changing it's value to something else (so they are different), the problem is stopped.

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)

Not exactly sure why, I'm still new to python, but it seems to work. My best guess is that "text = '£'" some how links the text boxes together. I'm not sure though and if anyone can add to this or explain why, please do.