python标签_如何在python中更新标签?

I'm new to python, and I was creating a small game when I got a problem. I searched for an answer, found one, yet it didn't work.

The game I'm trying to make is more or less a recreation of Cookie Clicker, and I'm trying to make the label which I have the score on update, instead, it creates a new label.

from tkinter import *

import time

master = Tk()

def uiPrint():

print("")

print(click)

blankLine()

click = 0

mult = 1

dcp1 = 0

def blankLine():

for i in range(20):

print("")

def buttonCommand():

global click

global mult

click += 1*(mult)

uiPrint()

scoreCommand = Button(text=click)

scoreCommand.pack()

mainClickButton = Button(master, text="Click me!", command = buttonCommand)

mainClickButton.pack()

master.title("Cookie Clicker")

photo = PhotoImage(file=r"C:\Users\---\Desktop\Cookie.gif")

label = Label(image=photo)

label.image = photo

label.pack()

mainloop()

解决方案

I found this post :

A bit modified this means :

from tkinter import *

master = Tk()

def uiPrint():

print("")

print(clickcount)

blankLine()

clickcount=0

click=StringVar() #Updates label if changed

click.set("0");

mult = 1

dcp1 = 0

def blankLine():

for i in range(20):

print("")

def buttonCommand():

global clickcount

global click

global mult

clickcount += 1*(mult)

click.set(str(clickcount)); #Update score

uiPrint()

mainClickButton = Button(master, text="Click me!", command = buttonCommand)

mainClickButton.pack()

master.title("Cookie Clicker")

photo = PhotoImage(file=r"C:\Users\---\Desktop\Cookie.gif")

label = Label(image=photo)

label.image = photo

label.pack()

l = Label(master, textvariable = click) #Score

l.pack()

mainloop();

I would additonaly suggest to make the cookie image as a button :

from tkinter import *

master = Tk()

def uiPrint():

print("")

print(clickcount)

blankLine()

clickcount=0

click=StringVar() #Updates label if changed

click.set("0");

mult = 1

dcp1 = 0

def blankLine():

for i in range(20):

print("")

def buttonCommand():

global clickcount

global click

global mult

clickcount += 1*(mult)

click.set(str(clickcount)); #Update score

uiPrint()

photo = PhotoImage(file=r"C:\Users\---\Desktop\Cookie.gif")

mainClickButton = Button(master, image=photo, command = buttonCommand)

mainClickButton.pack()

master.title("Cookie Clicker")

l = Label(master, textvariable = click) #Score

l.pack()

mainloop();

Both tested with python 2.7.11 and python 3.5.2, they work fine

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值