简体   繁体   中英

Tkinter update label when variable changes

I am working on a code that is a clicker game. I have done 90% myself as I have never used Tkinter module before so was a learning project really, anyway to the problem:

I have just sorted the auto clicking function and a label in the window attached to a variable (the amount of cash), at the moment I have the label at a .pack(), and set so it creates a new label after every second. Obviously this will create confusion and isn't what you would expect from a game, so I wondered if there was a way to either:

1) Make the label update every time the variable (Amount of cash) updates (This would be Ideal if you can help me with this)

2)Delete the label with a line of code and then put the new label in with the current variables number (Not as good as the first, but would still be great to have this one if the first isn't possible)

Here is the code so far:

import time
from tkinter import *
root=Tk()

CPS=0
cash=0
mult=1

def blankLine():
    for i in range(16):
        print ("")

def infoprint():
    print("Cash =",cash)
    print("")
    print("Each click is worth",mult,"cash")
    print("")
    print("CPS =",CPS)
    blankLine()

def CashASecond():
    global root
    global cash
    global CPS
    cash += CPS
    root.after(1000, CashASecond)
CashASecond()

def CashLabel():
    label1=Label(root,text=cash)
    label1.pack()
    root.after(1000, CashLabel)
CashLabel()

def ManualClicker():
    global cash
    global mult
    cash += 1*(mult)
    infoprint()
ManualClickerButton=Button(root,text="Click",command=ManualClicker)
ManualClickerButton.pack()

class upgrades():
    def DoubleManualClicker():
        global cash
        global mult
        if cash < 1000:
            print("Not enough cash, sorry")
        elif cash>= 1000:
            print ("Double clicks purchased!")
            mult *= 2
            cash = cash - 1,000
    DoubleManualClickerButton=Button(root,text="Purchase double clicks",command=DoubleManualClicker)
    DoubleManualClickerButton.pack()
    DoubleManualClickerLabel=Label(root,text="Each click is worth twice as much, costs 1,000 \n")
    DoubleManualClickerLabel.pack()

    def clicker():
        global cash
        global mult
        global CPS
        if cash < 25:
            print("Not enough cash, sorry")
        elif cash >=25:
            CPS += 1
            print("CPS is now",CPS)
            cash = cash - 25
    ClickerButton=Button(root,text="Purchase auto clicker", command=clicker)
    ClickerButton.pack()
    ClickerLabel=Label(root,text="+1 CPS, costs 25 \n")
    ClickerLabel.pack()

    def SingleMachine():
        global cash
        global mult
        global CPS
        if cash < 50:
            print("Not enough cash, sorry")
        elif cash >= 50:
            CPS += 3
            print("CPS is now",CPS)
            cash = cash - 50
    SingleMachineButton=Button(root,text="Purcahse Single Cash Printer",command=SingleMachine)
    SingleMachineButton.pack()
    SingleMachineLabel=Label(root,text="+3 CPS, costs 50 \n")
    SingleMachineLabel.pack()

    def LoftOfCashPrinter():
        global cash
        global mult
        global CPS
        if cash < 100:
            print("Not enough cash, sorry")
        elif cash >= 100:
            CPS += 5
            print("CPS is now",CPS)
            cash=cash-100
    LoftOfCashPrinterButton=Button(root,text="Purchase a loft of cash printers",command=LoftOfCashPrinter)
    LoftOfCashPrinterButton.pack()
    loftlabel=Label(root,text="+7 CPS, costs 100 \n")
    loftlabel.pack()


root.title("Cash Clicker! ALPHA! By George Hill")
root.geometry("500x900+900+0")

root.mainloop()

Thanks in advance :)

I had this problem before and the way I managed to fix this was to use events and bindings. Note that I am using classes and Application. eg

def Click(self, event):
    self.Label['text'] = ....

....
    self.Buy = Button(....)
    self.Buy.grid(....)
    self.Buy.bind('<Button>', self.Click)

    self.Label = Label(....)
    self.Label.grid(....)

I understand this code is very basic (it won't work) and the use of .... is to show where the use of variables and the like will be used. This is written purely to show you how to bind widgets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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