Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   YEY!! My First GUI To share with the world!! (http://www.programmingforums.org/showthread.php?t=4811)

SaturN Jul 8th, 2005 6:24 PM

YEY!! My First GUI To share with the world!!
 
Ok, it's my first, so it ain't great!! But it ain't bad either! It does not look perfect, but it's only created on the basics, It's an updated version of my lyric finder..

:


#Lyric Finder.pyw
 
import urllib2, os, sys, random
from Tkinter import *
if "Lyrics" not in os.listdir("C:\\"):
        os.mkdir("C:\Lyrics")
                                                         
def Search():
        global artist, track
        path = "C:\Lyrics"
        artists = artist.get()
        tracks = track.get()
        artists = artists.replace(' ','%20')
        tracks = tracks.replace(' ','%20')
        url = """http://lyrc.com.ar/en/tema1en.php?songname=%s&artist=%s""" % (tracks,artists)
        opener = urllib2.build_opener()
        opener.addheaders = [('User-agent', 'Mozilla/5.0')]
        lyrics = opener.open(url).read()
        artists = artists.replace("%20","_")
        tracks = tracks.replace("%20","_")
        file = open("%s\%s.html" % (path, "%s by %s"%(tracks, artists)),'w')
        file.write(lyrics)
        os.startfile("%s\%s.html"% (path, "%s by %s"% (tracks, artists)))
        file.close()
        report.configure(text="Displaying Track")
def MakeWindow():
        global lb, artist, track, report
        win = Tk(className=" Lyric Finder powered by Lyrc.com")
        frame1 = Frame(win)
        frame1.pack(side=LEFT)
        frame2 = Frame(win)
        frame2.pack(side=RIGHT)
        artist = StringVar()
        track = StringVar()
        filler = Label(frame1).grid(row=0, column=0)
        artist1 = Entry(frame1, textvariable=artist)
        arr = Label(frame1, text="Enter Artist:")
        arr.grid(row=1, column=1)
        artist1.grid(row=1, column=2)
        fill = Label(frame1).grid(row=2, column=0)
        track1 = Entry(frame1, textvariable=track)
        trr = Label(frame1, text="Enter Track:")
        trr.grid(row=3, column=1)
        track1.grid(row=3, column=2)

        filler1 = Label(frame1, text=' ')
        filler1.grid(row=4, column=2)
        fileo = Button(frame1, text="Open Choosen File", command=OpenFile)
        fileo.grid(row=5, column=2)
        filler2 = Label(frame1, text=' ')
        filler2.grid(row=6, column=2)
        search = Button(frame1, text="Search", command=Search)
        search.grid(row=7, column=2)
        filler3 = Label(frame1)
        filler3.grid(row=8, column=2)               
        report = Label(frame1)
        report.grid(row=9, column=2)
        scrollv = Scrollbar(frame2, orient=VERTICAL)
        scrollh = Scrollbar(frame2, orient=HORIZONTAL)
        lb = Listbox(frame2, xscrollcommand=scrollh.set, yscrollcommand=scrollv.set)
        scrollv.config(command=lb.yview)
        scrollh.config(command=lb.xview)
        scrollh.pack(side=BOTTOM, fill=X)
        scrollv.pack(side=RIGHT, fill=Y)
        title = Label(frame2, text="Previously Downloaded Lyrics").pack(side=TOP, pady=10)
        lb.pack(side=LEFT, fill=BOTH, expand=2, padx=10, pady=10)
        Files()
        win.mainloop()
def Files():
        global tracklisting
        tracklisting = []
        for file in os.listdir("C:\Lyrics"):
                tracklisting.append(os.path.join("C:\Lyrics", file))
                lb.insert(END, file)
def OpenFile():
        x = "".join(lb.curselection())
        x = int(x)
        os.startfile(tracklisting[x])
 
       
MakeWindow()


The reason for all the Filler1 etc is to fill the grid to generate gaps i could find no documented way of doing it, so that worked fine!!

Need Tkinter to run.

Any feedback or new ideas, let me know :D

Cerulean Jul 8th, 2005 6:45 PM

First obvious crit - don't use Tkinter. It's inherently flawed and very slow (apparently its bound to Python via tcl, so two layers of interpreted language). Use a real GUI toolkit like wxPython or PyQt. Tkinter's many flaws can be found by Googling for it. Tkinter apps look horrifically ugly under Linux too.

Secondly - Don't hardcode paths like "C:\Lyrics" - there is no 'C:\' for me here, so I had to manually change everything to have a go.

Thirdly - More feedback. I entered my artist and song-name ("Smashing Pumpkins", "Cherry"), and then pressed Search. Nothing happened. I pressed Open Chosen File.. still nothing, except looking at Konsole showed me that you've got a bug for each. Here's the trackback:
Pressing "Search":
:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.4/lib-tk/Tkinter.py", line 1345, in __call__
    return self.func(*args)
  File "tkintergui.py", line 23, in Search
    os.startfile("%s\%s.html"% (path, "%s by %s"% (tracks, artists)))
AttributeError: 'module' object has no attribute 'startfile'

Pressing "Open Chosen File":
:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.4/lib-tk/Tkinter.py", line 1345, in __call__
    return self.func(*args)
  File "tkintergui.py", line 77, in OpenFile
    x = int(x)
ValueError: invalid literal for int():

Python version 2.4.1

SaturN Jul 9th, 2005 12:29 PM

It works fine for me!! :S I have the same python version, i've just run the exact same search as you now, and it's fine!!
Also, i have wxPython, but i can't find a decent on-line tutorial! lol
So if you've got a link or anything :D that would be good. And thanks for the note about paths! Never really thought about that. Thanks :D

Cerulean Jul 10th, 2005 7:52 AM

There are plenty of good wxPython tutorials. Try working through this

SaturN Jul 10th, 2005 5:09 PM

Thanks, I'll see what wonders this brings :D
Thanks for your input :)

dannyp Jul 13th, 2005 7:55 AM

I agree with Cerulean, Tkinter is terrible. wxPython IMO is the best I have used. The homepage has semi-good tutorials, then I would suggest just looking at other people's code to get better ideas of how to do things. I was able to code a nice looking GUI in about 10 hours total.

SaturN Jul 16th, 2005 2:55 PM

Tkinter seems ok for small little apps like that!! i may be wrong!! But, like i say seems ok, I'll probably look in to using wxPython for larger scale projects, but atm it seems to suit ok,
and it's easy to use :D :D hehe

Cerulean Jul 16th, 2005 3:42 PM

I find wxPython easier to use. Use wxPython for a little and you'll see that it is too. It seems like a hassle, learning something new after you just learnt a bit of Tkinter, but let me just re-phrase why Tkinter is bad:

1. Slow. Tkinter goes through an extra layer of interperation (it wraps tcl/tk), while wxPython is directly bound to wxWidgets (which is written in C++).
2. Applications look ugly and don't integrate with the rest of the Linux desktop (it doesn't use GTK or Qt). I can send you a screenshot if that would help you realise the true vulgarness of the Tkinter GUI.
3. Lack of high-level abstraction. Tkinter is a pretty low level, so you're touching the guts far too often. That really does make it harder to use.

SaturN Jul 16th, 2005 4:55 PM

ok, :D I'll switch to wxPython straight away! hehe, Thanks for the tips..
You live and learn,
trail and error and all that palava, thanks :)


All times are GMT -5. The time now is 5:02 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC