from Tkinter import *
import tkMessageBox
import pygame.mixer

# create GUI window
app = Tk()
app.title("Head Frist Mix")

sound_file = "50459_M_RED_Nephlimizer.wav"

# start the sounds system
mixer = pygame.mixer
mixer.init()

# create function
def track_toggle():
    if track_playing.get() ==  1:
        track.play(loops = -1)
    else:
        track.stop()

# create function volume
def change_volume(v):
    track.set_volume(volume.get())

track = mixer.Sound(sound_file)

track_playing = IntVar()

# create check button
track_button = Checkbutton(app, variable = track_playing,
                           command = track_toggle,
                           text = sound_file)
track_button.pack(side = LEFT)

# create scale button
volume = DoubleVar()
volume.set(track.get_volume())
volume_scale = Scale(variable = volume,
                     from_ = 0.0,
                     to = 1.0,
                     resolution = 0.1,
                     command = change_volume,
                     label = "Volume",
                     orient = HORIZONTAL)
volume_scale.pack(side = RIGHT)

def shutdown():
    track.stop()
    app.destroy()

app.protocol("WM_DELETE_WINDOW", shutdown)

# GUI loop
app.mainloop()

I the above code I have two questions. At the very top I've imported "from Tkinter import * & import tkMessageBox". Why would I need to import tkMessageBox if I've already imported everything in Tkinter via "from Tkinter import *"?

Secondly, of all GUI controls I've ever added the first parameter was always the name of my Tk object, in this case (app). So why doesn't the Scale control also need "app" as it's fist parameter? Thanks.

1) Because they are different modules:

>>> import Tkinter
>>> Tkinter
<module 'Tkinter' from 'D:\Python27\lib\lib-tk\Tkinter.pyc'>
>>> import tkMessageBox
>>> tkMessageBox
<module 'tkMessageBox' from 'D:\Python27\lib\lib-tk\tkMessageBox.pyc'>

Second question I don't know to answer.

Tkinter is remarkably tolerant, you can use or leave off app in the Scale or Checkbutton widgets. Since there is only one parent, Tkinter figures it out. It is a good habit to put the parent in, just in case there is more than one.

Edited 6 Years Ago by vegaseat: n/a

This article has been dead for over six months. Start a new discussion instead.