更新python tkinter标签

时间:2015-11-24 10:01:20

标签: python tkinter

from tkinter import *
import time

master = Tk()

def uiPrint():
    print("")
    print(click)


click = 0
mult = 1
clickView = click

def doubClicks():
    global click
    global mult
    if click < 5:
        print("Not enough clicks!")
    elif click >= 5:
        mult = mult*2
        click = click - 5
        print("Double Clicks Purchased!")

def buttonCommand():
    clickView = IntVar()
    Label(master, textvariable =clickView).pack()
    clickView.get()
    global click
    global mult
    click += 1*(mult)
    uiPrint()

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

purchaseDoubleClickButton = Button(master, text="Purchase Double Clicks", command = doubClicks)
purchaseDoubleClickButton.pack()


master.title("Clicker")
master.geometry("%sx%s+%s+%s" % (200,70,512,512))
mainloop()

问题:

tkinter窗口上的标签,告诉您有多少次点击停留在0,进行更新。问题是什么?可能缺少一些非常明显的东西

1 个答案:

答案 0 :(得分:0)

我认为你应该更新你的IntVar的同一个实例,以便更新内容。你应该做的是在主程序中创建你的IntVar并在buttonCommand()函数中使用它作为全局:

def buttonCommand():    
    global click
    global mult
    global clickView
    click += 1*(mult)
    clickView.set(click)
    uiPrint()

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

purchaseDoubleClickButton = Button(master, text="Purchase Double Clicks", command = doubClicks)
purchaseDoubleClickButton.pack()

clickView = IntVar()
clickView.set(click)
Label(master, textvariable = clickView).pack()    

修改 要在购买双击时更新标签,请在“doubleClicks”功能上使用相同的方法:

def doubClicks():
    global click
    global mult
    global clickView
    if click < 5:
        print("Not enough clicks!")
    elif click >= 5:
        mult = mult*2
        click = click - 5
        print("Double Clicks Purchased!")
    clickView.set(click)

另一种方法是使用OOP,因此创建一个“clicker”类而不是使用全局变量:

from Tkinter import *

class Clicker():
    def __init__(self, master):            
        self.click = 0
        self.mult = 1
        self.clickView = StringVar() 
        self.clickView.set(self.click)

        self.mainClickButton = Button(master, text="Click!", command =  self.buttonCommand)
        self.mainClickButton.pack()

        self.purchaseDoubleClickButton = Button(master, text="Purchase Double Clicks", command = self.doubClicks)
        self.purchaseDoubleClickButton.pack()

        Label(master, textvariable = self.clickView).pack()


    def uiPrint(self):
        print("")
        print(self.click)

    def doubClicks(self):
        if self.click < 5:
            print("Not enough clicks!")
        elif self.click >= 5:
            self.mult = self.mult*2
            self.click = self.click - 5
            print("Double Clicks Purchased!")
            self.clickView.set(self.click)

    def buttonCommand(self):              
        self.click += 1*(self.mult)
        self.clickView.set(self.click)
        self.uiPrint()


if __name__ == '__main__':
    master = Tk()        
    master.title("Clicker")
    master.geometry("%sx%s+%s+%s" % (200,70,512,512))  
    clicker = Clicker(master)

    mainloop()