我对tkinter模块还不熟悉,并且想知道在按下按钮后是否还有更新标签。我正在尝试使用按钮增加和减少标签值的益智游戏。我包含了所有代码,所以你们都可以看到发生了什么。当我运行它并单击按钮时没有任何反应。
#creat GUI
import tkinter
import time
#-------------------------------------------------
def isactive():
return True
#-------------------------------------------------
def inv(event):
global OkToPressI
if OkToPressI == False:
pass
else:
updateflint()
updategrass()
updatehay()
updatetree()
updatelog()
updatesapling()
updatetwig()
updateboulder()
updaterock()
updatepickaxe()
updateaxe()
updatefirepit()
updatetent()
updatetorch()
startLabel.config(text='')
OkToPressI =False
#-------------------------------------------------
def updateflint():
global flint
if isactive():
flintlabel.after(100, updateflint)
#-------------------------------------------------
def updategrass():
global grass
if isactive():
grasslabel.after(100, updategrass)
#-------------------------------------------------
def updatehay():
global hay
if isactive():
haylabel.after(100, updatehay)
#-------------------------------------------------
def updatetree():
global tree
if isactive():
treelabel.after(100, updatetree)
#-------------------------------------------------
def updatelog():
global log
if isactive():
loglabel.after(100, updatelog)
#-------------------------------------------------
def updatesapling():
global sapling
if isactive():
saplinglabel.after(100,updatesapling)
#-------------------------------------------------
def updatetwig():
global twig
if isactive():
twiglabel.after(100, updatetwig)
#-------------------------------------------------
def updateboulder():
global boulder
if isactive():
boulderlabel.after(100, updateboulder)
#-------------------------------------------------
def updaterock():
global rock
if isactive():
rocklabel.after(100, updaterock)
#-------------------------------------------------
def updatepickaxe():
global pickaxe
if isactive():
pickaxelabel.after(100, updatepickaxe)
#-------------------------------------------------
def updateaxe():
global axe
if isactive():
axelabel.after(100, updateaxe)
#-------------------------------------------------
def updatefirepit():
global firepit
if isactive():
firepitlabel.after(100, updatefirepit)
#-------------------------------------------------
def updatetent():
global tent
if isactive():
tentlabel.after(100, updatetent)
#-------------------------------------------------
def updatetorch():
global torch
if isactive():
torchlabel.after(100, updatetorch)
#-------------------------------------------------
#crafting constrants
def crafthay():
global hay
global grass
if grass == 0:
return 'You can\'t.'
else:
return 'Crafting...'
time.sleep(5)
hay += 1
grass -= 1
return 'Item crafted.'
#-------------------------------------------------
def crafttwig():
global twig
global sapling
if sapling == 0:
return 'You can\'t.'
else:
return 'Crafting...'
time.sleep(5)
sapling -= 1
twig += 1
return 'Item crafted.'
#-------------------------------------------------
def craftlog():
global axe
global tree
global log
if axe == 0:
if tree ==0:
return 'You can\'t.'
else:
return 'You are missing something.'
else:
return 'Crafting...'
time.sleep(5)
axe -= 1
tree -= 1
log += 1
return 'Item crafted.'
#-------------------------------------------------
def craftaxe():
global twig
global flint
global axe
if flint == 0:
if twig < 3:
return 'You can\'t.'
else:
return 'You are missing something.'
else:
return 'Crafting...'
time.sleep(5)
twig -= 3
flint -=1
axe += 1
return 'Item crafted.'
#-------------------------------------------------
def crafttent():
global twig
global tent
global hay
if twig < 10:
if hay < 15:
return 'You can\'t.'
else:
return 'Crafting...'
time.sleep(5)
twig -= 10
hay -= 15
tent += 1
return 'Item Crafted.'
#-------------------------------------------------
def craftfirepit():
global boulder
global log
global twig
global torch
global firepit
if boulder < 5:
if log < 3:
if twig == 0:
if torch == 0:
return 'You can\'t.'
else:
return 'Crafting...'
time.sleep(5)
boulder -= 5
log -= 3
twig -= 1
torch -= 1
firepit += 1
return 'Item crafted'
#-------------------------------------------------
def crafttorch():
global flint
global grass
global twig
global torch
if flint == 0:
if grass == 0:
if twig == 0:
return 'You can\'t.'
else:
return 'crafting...'
time.sleep(5)
flint -= 1
grass -= 1
twig -= 1
torch += 1
return 'Item crafted.'
#-------------------------------------------------
def craftpickaxe():
global flint
global twig
global pickaxe
if flint < 2:
if twig == 0:
return 'You can\'t'
else:
return 'Crafting...'
time.sleep (5)
flint -= 2
twig -= 1
pickaxe += 1
return 'Item crafted.'
#-------------------------------------------------
#all global variables
OkToPressI = True
flint = 50
grass = 100
hay = 0
tree = 100
log = 0
sapling = 100
twig = 0
boulder = 30
rock = 0
pickaxe = 0
axe = 0
firepit = 0
tent = 0
torch = 0
#-------------------------------------------------------
print("'Crafting Challenge' Game")
print("-----------------------------------------\n")
print("TRY TO SURVIVE BY CRAFTING A TENT AND A FIREPIT!")
print('Press i to see your inventroy and c to craft.')
#----------------------------------------------------------------------
#run the GUI for the active inventory
root = tkinter.Tk()
root.title("Inventory")
root.geometry("750x750")
startLabel= tkinter.Label(root, text="Press 'i' to see inventory!", font=('Helvetica', 12))
flintlabel= tkinter.Label(root, text="Flint: " + str(flint), font = ('Helvetica',12))
grasslabel= tkinter.Label(root, text='Grass: ' + str(grass), font = ('Helvetica',12))
haylabel= tkinter.Label(root, text='Hay: ' + str(hay), font = ('Helvetica',12))
treelabel= tkinter.Label(root, text='Tree: ' + str(tree), font = ('Helvetica',12))
loglabel= tkinter.Label(root, text='Log: ' +str(log), font = ('Helvetica',12))
saplinglabel= tkinter.Label(root, text='Sapling: ' + str(sapling), font = ('Helvetica',12))
twiglabel= tkinter.Label(root, text='Twig: ' +str(twig), font = ('Helvetica',12))
boulderlabel= tkinter.Label(root, text='Boulder: ' + str(boulder), font = ('Helvetica',12))
rocklabel= tkinter.Label(root, text='Rock: ' + str(rock), font=('Helvetica',12))
pickaxelabel= tkinter.Label(root, text='Pickaxe: ' + str(pickaxe), font=('Helvetica',12))
axelabel= tkinter.Label(root, text='Axe: ' +str(axe), font=('Helvetica',12))
firepitlabel= tkinter.Label(root, text='Firepit: ' + str(firepit), font=('Helvetica',12))
tentlabel= tkinter.Label(root, text='Tent: ' + str(tent), font=('Helvetica',12))
torchlabel= tkinter.Label(root, text='Torch: ' + str(torch), font=('Helvetica',12))
craftlabel= tkinter.Label(root, text='----------------------------------------------')
craft2label= tkinter.Label(root, text='Press the button to craft.')
btnhay = tkinter.Button(root, text='Hay', command=crafthay)
btntwig = tkinter.Button(root, text='Twig', command=crafttwig)
btnlog = tkinter.Button(root, text='Log', command=craftlog)
btnaxe = tkinter.Button(root, text='Axe', command=craftaxe)
btntent = tkinter.Button(root, text='Tent', command=crafttent)
btnfirepit = tkinter.Button(root, text='Firepit', command=craftfirepit)
btntorch = tkinter.Button(root, text='Torch', command=crafttorch)
btnpickaxe = tkinter.Button(root, text='Pickaxe', command=craftpickaxe)
startLabel.pack()
flintlabel.pack()
grasslabel.pack()
haylabel.pack()
treelabel.pack()
loglabel.pack()
saplinglabel.pack()
twiglabel.pack()
boulderlabel.pack()
rocklabel.pack()
pickaxelabel.pack()
axelabel.pack()
firepitlabel.pack()
tentlabel.pack()
torchlabel.pack()
craftlabel.pack()
btnhay.pack()
btntwig.pack()
btnlog.pack()
btnaxe.pack()
btntent.pack()
btnfirepit.pack()
btntorch.pack()
btnpickaxe.pack()
root.bind('<i>',inv)
root.mainloop()
#starts crafting
if tent >= 1 and firepit >= 1:
print("\n**YOU HAVE MANAGED TO SURVIVE!\nWELL DONE!")
答案 0 :(得分:1)
基本上有两种方式。
一种方法是使用where.exe node.exe (Windows 7+)
which node.exe (linux bash)
作为标签的文本。
StringVar
您可以使用import tkinter as tk
flint_text = tk.StringVar()
flint_text.set("Flint: " + str(flint))
flintlabel= tkinter.Label(root, textvariable=flint_text)
的{{1}}方法更改其文字。标签将自动更新。
另一种方法是使用标签的set
属性。
StringVar
稍后,在你的回调中:
text
请注意,这相当于:
flintlabel= tkinter.Label(root, "Flint: 0")
答案 1 :(得分:1)
以下是单击按钮时更新标签的示例:
try: # In order to be able to import tkinter for
import tkinter as tk # either in python 2 or in python 3
except ImportError:
import Tkinter as tk
def update_something(widget):
import random
widget['text'] = random.randint(0, 9)
if __name__ == '__main__':
root = tk.Tk()
label = tk.Label(root, text=0)
tk.Button(root, text="Update",
command=lambda w=label: update_something(w)).pack()
label.pack()
tk.mainloop()