基本上我要做的就是制作一个简单的点计数器,就像在地下城和龙中,或者在一开始就在辐射4中。
from tkinter import *
points = 21
global Strength
global dexterity
global intelligence
def Plus():
print("hi")
label = Label(root, text = points)
label.grid()
strengthlb = Label(root, text = "Strength")
strengthlb.grid()
strplusbtn = Button(root, text = "+", command = points -1)
strplusbtn.grid()
label.config(text = points)
strengthcoutnerlb = Label(root, text = 0)
我的目标是当我点击名为strplusbtn
的按钮时,我向strengthcounterlb
添加1,并从名为label
的标签和名为points
的变量中减去1。 / p>
答案 0 :(得分:0)
command
选项,以便调用
按下时引用的方法。text
选项来配置标签的文本。在下面的代码plus
按钮被配置为在按下时调用increase_str
方法。该方法修改全局变量(points
,strength
,tot_atr
,str_atr
)并标记文本选项。
try:
import tkinter as tk
except:
import Tkinter as tk
def increase_str():
global points, strength
strength += 1
points -= 1
tot_atr['text'] = "Total Points: {}".format(points)
str_atr['text'] = "Strength: {}".format(strength)
if __name__ == '__main__':
root = tk.Tk()
points = 10
strength = 5
tot_atr = tk.Label(root, text="Total Points: {}".format(points))
str_atr = tk.Label(root, text="Strength: {}".format(strength))
plus = tk.Button(root, text="+", command=increase_str)
tot_atr.pack()
str_atr.pack(side='left')
plus.pack(side='left')
root.mainloop()