使用Python制作我的按钮更新计数

2024-10-04 09:25:21 发布

您现在位置:Python中文网/ 问答频道 /正文

有人能告诉我如何计算标签上所有点击按钮的次数并更新该次数的逻辑吗?这是我到目前为止的情况。我脑子里想不出如何在维护其他命令的同时告诉它我想要什么。以下是我到目前为止的文字:

import sys
from tkinter import *

tP = 0

def mBall():
    mtext = 'Ball'
    mtext1 = 'Total Pitches: ' + str(int(tP + 1))
    mlabel2 = Label(mGui, text = mtext).grid(sticky = W)
    mlabel3 = Label(mGui, text = mtext1).grid(row = 5, column = 1, sticky = W)

def mStrike():
    mtext = 'Strike'
    mlabel2 = Label(mGui, text = mtext).grid(sticky = W)

def mFoul():
    mtext = 'Foul'
    mlabel2 = Label(mGui, text = mtext).grid(sticky = W)

def mInPlay():
    mtext = '*  ' + ment.get()
    mtext1 = 'Total Pitches: 0'
    mlabel2 = Label(mGui, text = mtext).grid(sticky = W)
    mlabel3 = Label(mGui, text = mtext1).grid(row = 5, column = 1, sticky = W)

    return tP = tP + 1

mGui = Tk()
ment = StringVar()

mGui.geometry('250x450+500+300')
mGui.title('My Copy')

mlabel = Label(mGui, text = 'Pitch Counter').grid(row = 0, column = 0, columnspan = 2, sticky = N)

mlabel = Label(mGui, text = 'r2').grid(row = 2, column = 1, columnspan = 2, sticky = N)
mlabel = Label(mGui, text = 'r4').grid(row = 4, column = 1, columnspan = 2, sticky = N)

mBall = Button(mGui, text = 'Ball', command = mBall, fg = 'white', bg = 'blue').grid(row = 1, column = 0, columnspan = 2, sticky = W)
mStrike = Button(mGui, text = 'Strike', command = mStrike, fg = 'white', bg = 'blue').grid(row = 1, column = 0, columnspan = 2, sticky = N)
mFoul = Button(mGui, text = 'Foul', command = mFoul, fg = 'white', bg = 'blue').grid(row = 1, column = 0, columnspan = 2, sticky = E)

mInPlay = Button(mGui, text = 'In Play', command = mInPlay, fg = 'white', bg = 'blue').grid(row = 3, column = 0, columnspan = 2, sticky = N)
mEntry = Entry(mGui, textvariable = ment).grid()

Tags: textdefcolumnbuttonlabelcommandgridrow