here2share

# Tk_males_and_females_counter.py

Jan 9th, 2022 (edited)
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.03 KB | None | 0 0
  1. # Tk_males_and_female_counter.py
  2.  
  3. from tkinter import *
  4.  
  5. # global variables
  6. male_cnt = 0
  7. female_cnt = 0
  8. total_cnt = 0
  9.  
  10. # event handlers
  11. def on_click(e):
  12.     global male_cnt, female_cnt
  13.     global total_cnt
  14.     gender = e.widget["text"]
  15.     e.widget["fg"] = "black"
  16.     if gender == "males":
  17.         male_cnt += 1
  18.         tv_male.set(male_cnt)
  19.     else:
  20.         female_cnt += 1
  21.         tv_female.set(female_cnt)
  22.     total_cnt += 1
  23.     tv_total.set(f'total = {total_cnt}')
  24.  
  25.  
  26. def right_click(e):
  27.     global male_cnt, female_cnt
  28.     global total_cnt
  29.     gender = e.widget["text"]
  30.     e.widget["fg"] = "red"
  31.     if gender == "males":
  32.         male_cnt -= 1
  33.         tv_male.set(male_cnt)
  34.     else:
  35.         female_cnt -= 1
  36.         tv_female.set(female_cnt)
  37.     total_cnt -= 1
  38.     tv_total.set(f'total = {total_cnt}')
  39.  
  40.  
  41. def male_click(e):
  42.     global male_cnt
  43.     global total_cnt
  44.     male_cnt += 1
  45.     total_cnt += 1
  46.     tv_male.set(male_cnt)
  47.     tv_total.set(f'total = {total_cnt}')
  48.  
  49.  
  50. def female_click(e):
  51.     global female_cnt
  52.     global total_cnt
  53.     female_cnt += 1
  54.     total_cnt += 1
  55.     tv_female.set(female_cnt)
  56.     tv_total.set(f'total = {total_cnt}')
  57.  
  58. # main
  59. root = Tk()
  60. root.option_add("*Font", "consolas 25")
  61.  
  62. tv_male = IntVar()
  63. tv_female = IntVar()
  64. tv_total = StringVar()
  65.  
  66. btn_male = Button(root, text="males", bg="deep sky blue", width=8)
  67. btn_male.grid(row=0,
  68.               column=0,
  69.               ipady=20)
  70. btn_male.bind("<Button-1>", on_click)
  71. btn_male.bind("<Button-3>", right_click)
  72. btn_female = Button(root, text="females", bg="hot pink", width=8)
  73. btn_female.grid(row=0,
  74.                 column=1,
  75.                 ipady=20)
  76. btn_female.bind("<Button-1>", on_click)
  77. btn_female.bind("<Button-3>", right_click)
  78. Label(root, textvariable=tv_male, bg="deep sky blue").grid(row=1, column=0, sticky="news")
  79. Label(root, textvariable=tv_female, bg="hot pink").grid(row=1, column=1, sticky="news")
  80. Label(root, textvariable=tv_total, bg="gold").grid(row=2, columnspan=2, sticky="news")
  81. root.mainloop()
Add Comment
Please, Sign In to add comment