1

I am trying to make a simple music player but I keep getting this error:

Traceback (most recent call last):
    File "C:/Users/nickw/PycharmProjects/untitled1/music player", line 28, in <module>
        slider = tk.Scale(window, from_=100, to=0, command=setVolume)
      File "C:\Users\nickw\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 2856, in __init__
        Widget.__init__(self, master, 'scale', cnf, kw)
      File "C:\Users\nickw\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 2132, in __init__
        BaseWidget._setup(self, master, cnf)
      File "C:\Users\nickw\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 2110, in _setup
        self.tk = master.tk
    AttributeError: module 'tkinter' has no attribute 'tk'

This is my code:

import pygame
import Tkinter as tk
window = tk.Tk()


pygame.init()
pygame.mixer.music.load("music")

started = False
playing = False

def buttonClick():
    global playing, started
    if not playing:
        if not started:
            pygame.mixer.music.play(-1)
            started=True
        else:
            pygame.mixer.music.unpause()
        button.config(text ="Pause")
    else:
        pygame.mixer.music.pause()
        button.config(text="play")
        playing = not playing
def setVolume(val):
    volume = float(slider.get())
    pygame.mixer.music.set_volume(volume /100)


slider = tk.Scale(window, text="play", command="buttonClick")
button = tk.Button(tk, text = "play", command = buttonClick)

slider.pack()
slider.set(100)
button.pack()
window.mainloop()
1
  • 1
    Cannot find the error line slider = tk.Scale(window, from_=100, to=0, command=setVolume) in your code (seems like this line slider = tk.Scale(window, text="play", command="buttonClick") in your code. Another problem is that in the error messages, you are using Python 3, but you use Tkinter in your code. It should be tkinter. Post your real code. Commented Nov 8, 2016 at 6:55

3 Answers 3

6

If you are using python 3.x you have to change your import-line from

import Tkinter as tk

to

import tkinter as tk

Another problem is your slider: the constructor expects a function as last argument, you give it a string. You actually know the correct way, as I can see on the following line.

Sign up to request clarification or add additional context in comments.

Comments

5

I actually found the error! There was many so the tkinter problem was solved with

from tkinter import *
tk=Tk()

and the scale problem I corrected with this line.

w = Scale(tk, from_=0, to=100, command=setVolume)

Comments

3

If you have named your python file name as tkinter.py or Tkinter.py, change the file name to something else, it will get rid of the attribute error

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.