Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Simple tkinter window using pygame sound mixer

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.

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()
 

Attachments

  • red.png
    red.png
    7.1 KB · Views: 0
  • on1.png
    on1.png
    5.8 KB · Views: 0
  • off1.png
    off1.png
    5.7 KB · Views: 0
  • green.png
    green.png
    7.2 KB · Views: 0

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom