Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

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

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...