首页 > 解决方案 > python - 如何在python tkinter的shell中输出某个按钮被单击的次数?

问题描述

所以我的 gui 功能齐全,除了我希望能够在控制台中保留一个计数器,说明每个不同按钮被点击了多少次(例如“点赞按钮被点击了 x 次”和“um 按钮被点击了 y 次” )。我尝试过不成功,我的问题是控制台中实际上没有打印任何内容。有人可以指出如何正确执行此操作吗?谢谢你。

from tkinter import *
import tkinter as tk


window = tk.Tk()
window.geometry('900x900')
window.config(bg = 'honeydew2')
window.title('The Speech Filler Counter')

like = 0
def likes():
  global like 
  like = like + 1
  likelabel.configure(text=like)

likelabel = tk.Label(window, text = like, bg = 'honeydew2', fg = 'red')
likelabel.place(x = 22, y = 1)


likebutton = tk.Button(window, text="Like", activebackground= 'blue', activeforeground = 'honeydew2', bg = 'deep sky blue', fg = 'red', command=likes).place(x= 2, y = 25)


um = 0
def ums():
  global um
  um = um + 1
  umlabel.configure(text = um)

umlabel = tk.Label(window, text = um, bg = 'honeydew2', fg = 'cyan')
umlabel.place(x = 122, y = 1 )

umbutton = tk.Button(window, text="Um", activebackground= 'brown', activeforeground = 'honeydew2', bg = 'light pink', fg = 'blue', command=ums).place(x= 100, y = 25)

basically = 0
def basicallys():
  global basically
  basically = basically + 1
  baslabel.configure(text = basically)

baslabel = tk.Label(window, text = basically, bg = 'honeydew2', fg = 'deep pink')
baslabel.place(x = 235, y = 1 )

basbutton = tk.Button(window, text="Basically", activebackground= 'red', activeforeground = 'black', bg = 'gold', fg = 'brown', command=basicallys).place(x= 200, y = 25)

uh = 0
def uhs():
  global uh
  uh = uh + 1
  uhlabel.configure(text = uh)

uhlabel = tk.Label(window, text = uh, bg = 'honeydew2', fg = 'lime green')
uhlabel.place(x = 350, y = 1 )

uhbutton = tk.Button(window, text="Uh", activebackground= 'purple2', activeforeground = 'goldenrod1', bg = 'firebrick1', fg = 'blue2', command=uhs).place(x= 335, y = 25)

youknow = 0
def youknows():
  global youknow
  youknow = youknow + 1
  youknowlab.configure(text = youknow)

youknowlab = tk.Label(window, text = youknow, bg = 'honeydew2', fg = 'SlateBlue1')
youknowlab.place(x = 455, y = 1 )

youknowbutton = tk.Button(window, text="You know", activebackground= 'DarkOrange1', activeforeground = 'DeepSkyBlue1', bg = 'hot pink', fg = 'lemon chiffon', command=youknows).place(x= 425, y = 25)

ok = 0
def oks():
  global ok
  ok = ok + 1
  oklabel.configure(text = ok)

oklabel = tk.Label(window, text = ok, bg = 'honeydew2', fg = 'blue2')
oklabel.place(x = 595, y = 1 )

okbutton = tk.Button(window, text="Ok", activebackground= 'midnight blue', activeforeground = 'SeaGreen2', bg = 'SlateBlue2', fg = 'magenta2', command=oks).place(x= 575, y = 25)

print(f"um: {(um)}")
print(f"like: {like}")
print(f"uh: {uh}")
print(f"basicallly: {basically}")
print(f"ok: {ok}")
print(f"youknow: {youknow}")


window.mainloop()

标签: pythonpython-3.xtkinter

解决方案


推荐阅读