menator01
Gold Coder
Simple tkinter window using images and pygame sound mixer.
You will need to download or create a wav file and name it click.wav or edit accordingly.
You will need to download or create a wav file and name it click.wav or edit accordingly.
Python:
# Do the imports
import tkinter as tk
import pygame
# Inialize pygame mixer
pygame.mixer.init()
root = tk.Tk()
root.geometry('400x200+350+350')
# Set the variable
safety = False
# Define a function for playing sound
def playsound():
click = pygame.mixer.Sound('click.wav')
click.play()
# Define a function for checking state and setting correct files
def state(event):
global safety
if safety:
file = 'green.png'
file2 = 'on1.png'
safety = False
else:
file = 'red.png'
file2 = 'off1.png'
safety = True
img = tk.PhotoImage(file=file)
img.bak = img
label['image'] = img
img2 = tk.PhotoImage(file=file2)
img2.bak = img2
label2['image'] = img2
playsound()
# Starting images
img = tk.PhotoImage(file='green.png')
img.bak = img
img2 = tk.PhotoImage(file='on1.png')
img2.bak = img2
# Labels for holding images
label = tk.Label(root)
label['image'] = img
label.pack(pady=(20,8))
label2 = tk.Label(root)
label2['image'] = img2
label2['cursor'] = 'hand2'
label2.pack(pady=8)
label2.bind('<Button-1>', state)
root.mainloop()