I'm trying to make a music player with PyQt4 and Phonon. The player works well for most of my music, but some MP3 files won't work.
I tried with a lot of MP3s, approximately 1 out of 10 seems to have the problem. Phonon doesn't send any error, it simply doesn't play the song. Just to be sure, I compared two songs, one working and the other not, and they use the same codec (MPEG-1 Layer 3), both are in 44100Hz and 320 kb/s, tags are in ID3v2.3, neither use any special characters in their tags or filepath, and both work correctly when I play them in WMP.
I initialize the player with :
self.player = Phonon.createPlayer(Phonon.MusicCategory)
then to launch a song, I use :
self.player.setCurrentSource(Phonon.MediaSource(path_to_file))
self.player.play()
Obviously, there must be some difference between the files, maybe in the way they were encoded (the whole album of a non-working song won't work either), but I have no clue why some files are ok and other not... Do you have an idea of what could cause the problem ?
EDIT :
I'm on Windows 10, running PyQt 4.11.4 and Python 2.7.15. Not sure about the Phonon version though... :-/
I think I found something though... I managed to get an error for incorrect MP3s :
"Contacts could not be connected because they do not support the same transport. (0x80040266)."
According to this post, it's caused by the file metadata, and when I did some tests, it looks like it comes from the album cover's file size. When the size is higher than ~108 KB, the MP3 doesn't work, but if I resize the image so that it's below this limit, the MP3 works...
Here's a simplified code to test with a problematic mp3 :
import sys
from functools import partial
from PyQt4.QtGui import *
from PyQt4.phonon import *
class MusicPlayer(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.player = Phonon.createPlayer(Phonon.MusicCategory)
BTN_play = QPushButton("Play")
BTN_play.pressed.connect(self.play)
self.setCentralWidget(BTN_play)
self.show()
def play(self):
path = "path/to/file.mp3"
self.player.setCurrentSource(Phonon.MediaSource(path))
self.player.play()
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MusicPlayer()
sys.exit(app.exec_())
Still, I'd prefer not to edit each file metadata to correct it. Could there be a way to make Phonon ignore the cover image when parsing a file so that it doesn't make an error ?
Related
In my application, the user needs to browse for files. However, the askdirectory from tkFileDialog isn't really comfortable for using and browsing files since it's somewhat outdated.
It looks like this:
What I want to achieve should look like the "default" windows browse dialog. Like this:
(Source https://www.pythontutorial.net/tkinter/tkinter-open-file-dialog/)
I am not sure (since I couldn't find proof) but I remember someone telling me that it looks like this because I am using Python 2.7 and not 3+.
Is that true? Does an alternative exist?
It seems it has something to do with your version, as I have done a bit of research and I am using python 3 with the included tkinter and it shows a normal windows explorer popup. So, if you are using one of the up-to-date versions, it should be the OS' default. (I am unable to test that as python 2 will not work properly on my machine although I can confirm since you are using an older one)
I recreated your case with this code:
from tkinter import *
root = Tk()
filedialog.askdirectory()
root.mainloop()
You can try using askopenfilename(). It displays the standard Open File dialog box.
For example:
from tkinter import *
from tkinter import filedialog as fd
root = Tk()
root.title("Button to open files")
root.geometry("500x500")
def openfd(*args):
askk = askopenfilename()
btn = Button(root, text="Click to open file", command=openfd)
btn.place(x=200, y=200)
root.mainloop()
You can read more about it at https://www.pythontutorial.net/tkinter/tkinter-open-file-dialog/.
Code Here is my code
I've attached a link of well formatted code image and file
import os.
from Tkinter import *.
root=Tk().
m=Label(root,text="Processing...",fg="bue",font="calibri 35 bold").
os.chdir("f:\\").
im=PhotoImage("MM.jpg")
ok=Toplevel(root)
s=Label(root,text="Img",image=im)
s.photo=im
s.image=im
s.grid(row=0,column=0) m.grid(row=1,column=0)
but=None.
def dat():.
global s,root,but.
s.destroy().
m.destroy().
but=Label(root,text="Done").
but.pack().
root.overrideredirect(False).
root.mainloop().
I've attached a image of properly indented code in the link
or my file's drive link
Drive link
Please reformat your code so that you can get better help and quickly.
Meanwhile I have a code below showing you how to properly open a .jpg file and have it appear in a tk.Label. I hope this basics can guide you to solve your code problem.
from Tkinter import *
from PIL import Image, ImageTk #added
root=Tk()
filename = "minion.jpg" # Put your filename (can also use full path)here
im = Image.open(filename) #added
im=ImageTk.PhotoImage(im) #revised
s=Label(root,text="Img",image=im)
s.grid(row=0,column=0)
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
root.mainloop()
Update:
I have added the image commands into your script. Do note, your code contained several wrong indents, which pycharm highlighted to me. There were typos, wrong placement of certain commands and missing import statement. I took the liberty to correct them sufficient enough for tkinter to generate a tk window with your label and image, so to be able to answer your question. A screen shot of pycharm with python2.7, a revised code and tk window with label and image is attached below.
Note: For .jpg image you need the PIL or Pillow modules installed to open such file type. Do make sure you have that. If you don't have, follow the instructions in this webpage to install it. That is the limitation of the PIL.ImageTk.PhotoImage method. You can read more from this website.
This might be a general question. I'm modifying a Python code wrote by former colleague. The main purpose of the code is
Read some file from local
Pop out a GUI to do some modification
Save the file to local
The GUI is wrote with Python and Tkinter. I'm not very familiar with Tkinter actually. Right now, I want to implement an auto-save function, which runs alongside Tkinter's mainloop(), and save modified files automatically for every 5 minutes. I think I will need a second thread to do this. But I'm not sure how. Any ideas or examples will be much appreciated!! Thanks
Just like the comment says, use 'after' recursion.
import Tkinter
root = Tkinter.Tk()
def autosave():
# do something you want
root.after(60000 * 5, autosave) # time in milliseconds
autosave()
root.mainloop()
Threaded solution is possible too:
import threading
import time
import Tkinter
root = Tkinter.Tk()
def autosave():
while True:
# do something you want
time.sleep(60 * 5)
saver = threading.Thread(target=autosave)
saver.start()
root.mainloop()
before leaving I use sys.exit() to kill all running threads and gui. Not sure is it proper way to do it or not.
I have some issues in maxplus, the python distribution in 3ds max with pyside.
I try to parent my qtmainwindow to the max window. To minimize my qtmainwindow when max is minimized.
We want to leave the maxplus python installation untouched so I cannot install sip or shiboken to use wrapinstance to get a qobject. Now I would like to know how the parenting would be possible without those packages.
edit:
I recognized that shiboken is available but when I try to parent my QMainWindow to the applicatin it gives a
** system exception **
here is a bit of code i hope that helps to understand:
from PySide import QtCore, QtGui,shiboken
import MaxPlus
class ControlMainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(ControlMainWindow, self).__init__(parent)
maxWinHwd = MaxPlus.Core.GetWindowHandle()
parent=shiboken.wrapInstance(long(maxWinHwd), QtGui.QWidget)
self.setParent(parent)
def main():
global app
app = QtGui.QApplication.instance()
if not app:
app = QtGui.QApplication(sys.argv)
global window
window=ControlMainWindow()
window.show()
if __name__ == '__main__':
main()
Thanks in advance
I'm tackling the exact same problem now, so this is the best solution I've found so far that works OOTB without having to resort to relying on the Blur 3ds max Python extensions:
http://tech-artists.org/forum/showthread.php?5162-MaxPlus-parent-PySide-Window-to-Max-Window
I've tried to make a commented version of the code here as well, though I'm new to ctypes, so it may not be 100% correct:
http://pastebin.com/Sed2g89N
I hope that helps!
So I can do a little C++ console stuff with classes (nothing too fancy I think) and just started with Python (awesome language, it's like C++ without worries) and Tkinter.
The thing is that I don't really have a clue how in general a program with a GUI is structured. I know you have to separate the interface from the internal workings, but that's about it. As an example I am working on a small app that converts Excel tables to LaTeX tables and so far I have this:
from Tkinter import *
class ExcelToLateX:
def __init__(self,master):
self.convert = Button(master,text="Convert",command=self.Conversion)
self.convert.pack(side=BOTTOM,fill=X)
self.input=Input(master,40)
self.output=Output(master,40)
def Conversion(self):
self.output.Write(self.input.Read())
class Input:
def __init__(self,master,x):
self.u=Text(master,width=x)
self.u.pack(side=LEFT)
self.u.insert(1.0,"Paste Excel data here...")
def Read(self):
return self.u.get(1.0,END)
class Output:
def __init__(self,master,x):
self.v=Text(master,width=x)
self.v.pack(side=RIGHT)
self.v.insert(1.0,"LaTeX code")
def Write (self,input):
self.input=input
if self.v.get(1.0,END)=="":
self.v.insert(1.0,self.input)
else:
self.v.delete(1.0,END)
self.v.insert(1.0,self.input)
#Test script
root=Tk()
Window=ExcelToLateX(root)
root.mainloop()
So I have two Text widgets that can read and write stuff and a (for now) empty conversion class that will take Excel tables and spew out LaTeX code. I have no idea if this is the way to go (so any comments/tips are appreciated).
So in short I have two questions:
Are there any widely acknowledged sources that provide information on how a program with a GUI is structured? (preferably Python and Tkinter because that's what I'm doing right know, although it may be a bit more general (cross-language))
Is my current application any good when it comes to structure, and if not, what are some rules of thumb and things I can improve?
I'm just going to throw a couple short comments into the hat. I don't have experience with Tkinter, so my knowledge derives from PyQt4 experience.
Right now you are using composition for your classes, by making the single widget a member attribute. This can obviously work but a useful pattern is to subclass a GUI widget, and then compose the layout by adding more child widgets and parenting to that class. See the examples on this random Tkinter tutorial link I found: http://zetcode.com/tutorials/tkintertutorial/
class Example(Frame):
def __init__(self, parent):
super(Example, self).__init__(parent)
...
And just as a general python convention, you should try and stick with capitalization for your class names, and camelCase or under_score for class instance methods and variables. As you have it, you are using capital for instances (Window =) and methods (Write)
Also, if you aren't going to be subclassing Tkinter widgets, make sure to at least use the new-style classes by subclassing from object: http://realmike.org/blog/2010/07/18/introduction-to-new-style-classes-in-python/
You might also want to nest that last part of your code where you run the event loop inside of the classic python idiom:
if __name__ == "__main__":
root=Tk()
window = ExcelToLateX(root)
root.mainloop()
It prevents your app from immediately being executed if you were to import this module into another application as a library.