Python Forum
moving from tkinter to wxpython - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: moving from tkinter to wxpython (/thread-8405.html)

Pages: 1 2 3 4 5 6 7 8 9 10


RE: moving from tkinter to wxpython - Barrowman - Mar-01-2018

Thanks once again. I will continue to work on this further and,no doubt, get back with more that is puzzling me.


RE: moving from tkinter to wxpython - Barrowman - Mar-03-2018

I have had a few things to do but am now near to finishing this project as far as it goes running on Linux. Problems were on 2 fronts. The mp3 files which were the Bible chapters did not stich to the same naming convention throughout. I tried to accommodate the different naming but in the end I wrote a script to go through them and make most of them consistent but a few I edited manually. The other problem was that Psalms has 150 chapters and with the size of the buttons it wasn't practical to display them all on screen and with the code I have I couldn't see how to make that page scroll. In the end I have put half in order and tacked the rest at the end.
I still have some audio clips to make but i think it's mostly working otherwise.
After that I will have to see if I can convert it for Windows. Whether this is by altering the code or using something like py2exe I don't know yet. I realise the directories will need altering but I wonder if anyone has any suggestions to make the code better?
Most of this work would not have been accomplished without the help of the many suggestions and code from Larz60+
I will let my friend try it out on my laptop and hope she likes it.

#!/usr/bin/env python3
import sys
import wx
import os
books = ['67', 'Genesis050', 'Exodus040', 'Leviticus027', 'Numbers036', 'Deuteronomy034', 'Joshua024', 'Judges021',
         'Ruth004', '1Samuel031', '2Samuel024', '1Kings022', '2Kings025', '1Chronicles029', '2Chronicles036',
         'Ezra010', 'Nehemiah013', 'Esther010', 'Job042', 'Psalms075', 'Proverbs031', 'Ecclesiastes012',
         'Song of Solomon008', 'Isaiah066', 'Jeremiah052', 'Lamentations005', 'Ezekiel048', 'Daniel012', 'Hosea014',
         'Joel003', 'Amos009', 'Obadiah001', 'Jonah004', 'Micah007', 'Nahum003', 'Habakkuk003', 'Zephaniah003',
         'Haggai002', 'Zechariah014', 'Malachi004', 'Matthew028', 'Mark016', 'Luke024', 'John021', 'Acts028',
         'Romans016', '1Corinthians016', '2Corinthians013', 'Galatians006', 'Ephesians006', 'Philippians004',
         'Colossians004', '1Thessalonians005', '2Thessalonians003', '1Timothy006', '2Timothy004', 'Titus003',
         'Philemon001', 'Hebrews013', 'James005', '1Peter005', '2Peter003', '1John005', '2John001', '3John001',
         'Jude001', 'Revelation022', 'Psalms150'] 

chapters = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150]
 
book_names = [0,'/home/norman/Blind/recordings/genesis.mp3', '/home/norman/Blind/recordings/exodus.mp3', '/home/norman/Blind/recordings/leviticus.mp3','/home/norman/Blind/recordings/numbers.mp3','/home/norman/Blind/recordings/deuteronomy.mp3','/home/norman/Blind/recordings/joshua.mp3','/home/norman/Blind/recordings/judges.mp3','/home/norman/Blind/recordings/ruth.mp3','/home/norman/Blind/recordings/1samual.mp3','/home/norman/Blind/recordings/2samual.mp3','/home/norman/Blind/recordings/1kings.mp3','/home/norman/Blind/recordings/2kings.mp3','/home/norman/Blind/recordings/1chronicles.mp3','/home/norman/Blind/recordings/2chronicles.mp3', '/home/norman/Blind/recordings/ezra.mp3', '/home/norman/Blind/recordings/nehemiah.mp3','/home/norman/Blind/recordings/esther.mp3','/home/norman/Blind/recordings/job.mp3','/home/norman/Blind/recordings/psalms.mp3','/home/norman/Blind/recordings/proverbs.mp3','/home/norman/Blind/recordings/ecclesiastes.mp3', 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150]
 
 
class Log:
    def WriteText(self, text):
        if text[-1:] == '\n':
            text = text[:-1]
        wx.LogMessage(text)
 
    write = WriteText
 
class BibleNB(wx.Frame):
    def __init__(self, parent, id=wx.ID_ANY, title="Bible For The Blind", xpos=20, ypos=20, width=1000,
                 height=600, style=wx.DEFAULT_FRAME_STYLE, name='BibleApp'):
                # ideal width x height ratio: 16:9
        app = wx.App()
        wx.Frame.__init__(self, parent, id=id, title=title, pos=(xpos, ypos), size=(width, height), style=style)
        self.Bind(wx.EVT_CLOSE,self.OnClose)
        app.SetTopWindow(self)
        self.create_notebook()
        app.MainLoop()
 
    def create_notebook(self):
        wx.Log.SetActiveTarget(wx.LogStderr())
        log = Log()
        # for size use 16:9 ratio for best appearence
#        yfactor = 16/9
        width = 1200
        height = 600 #int(width / yfactor)
        self.frame =  wx.Frame(self,
                               id=wx.ID_ANY,
                               title='Bible For The Blind',
                               pos=(20, 20),
                               size=(width, height),
                               style=wx.DEFAULT_FRAME_STYLE,
                               name=wx.FrameNameStr)
 
        self.nb = wx.Notebook(self.frame, id=wx.ID_ANY, style=wx.BK_DEFAULT)
        self.log = log
        self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChanged)
        self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGING, self.OnPageChanging)
        column = 0
        row = 0
        pge = "Index" #books[pagenum]
        thispage = pge[:-3]
        win = wx.Panel(self.nb, -1, style=wx.CLIP_CHILDREN)
            # win = self.makeColorPanel(wx.WHITE)
        win.SetSize(self.GetBestSize())
            # st.SetForegroundColour(wx.BLACK)
            # self.nb.AddPage(win, thispage)
        self.nb.AddPage(win, thispage)
)
        column = 0
        row = 0
        for nums in range(1, 67):
            nme = books[nums]
            name = nme[:-3]
            btn = 'butt' + str(nums)
            btn = wx.Button(win, -1, name, pos=(column, row), size=(110, 36))
            btn.Bind(wx.EVT_ENTER_WINDOW, self.OnEnterWindow, btn)
            btn.Bind(wx.EVT_LEAVE_WINDOW, self.OnExitWindow, btn)
            btn.Bind(wx.EVT_BUTTON, self.OnClick, btn)
            column += 150
            if column == 1200:
                column = 0
                row += 55
        nums = 67
        btn = "butt" + str(nums)
        btn = wx.Button(win, -1, "Psalms ove 75", pos=(column, row), size=(110, 36)) 
        btn.Bind(wx.EVT_BUTTON, self.OnHighClick,btn)
        btn.Bind(wx.EVT_ENTER_WINDOW, self.OnEnterWindow, btn)
        btn.Bind(wx.EVT_LEAVE_WINDOW, self.OnExitWindow, btn)
        column += 150
        if column == 1200:
            column = 0
            row += 55
        nums = 68    
        btn = 'butt' + str(nums)
        btn = wx.Button(win, -1, "Exit", pos=(column, row), size=(110, 36))
        btn.Bind(wx.EVT_BUTTON, self.OnClose, btn)


        for pagenum in range(1, 67):
            column = 0
            row = 0
            pge = books[pagenum]
            thispage = pge[:-3]
            win = wx.Panel(self.nb, -1, style=wx.CLIP_CHILDREN)
            # win = self.makeColorPanel(wx.WHITE)
            win.SetSize(self.GetBestSize())
            # st.SetForegroundColour(wx.BLACK)
            # self.nb.AddPage(win, thispage)
            self.nb.AddPage(win, thispage)
            numchps = books[pagenum]
            chaps = int(numchps[-3:])
            column = 0
            row = 0
            st = 1

            for nums in range(st, chaps + 1):
                btn = 'butt' + str(nums)
                btn = wx.Button(win, nums, str(nums), pos=(column, row), size=(110, 36))
                btn.Bind(wx.EVT_ENTER_WINDOW, self.OnEnterWindow, btn)
                btn.Bind(wx.EVT_LEAVE_WINDOW, self.OnExitWindow, btn)
                btn.Bind(wx.EVT_BUTTON, self.OnChapterClick, btn,id = nums)
                column += 150
                if column == 1200:
                    column = 0
                    row += 55
            btn = 'butt' + str(nums)
            btn = wx.Button(win, -1, "Back to Index", pos=(column, row), size=(110, 36))
            btn.Bind(wx.EVT_BUTTON, self.OnIndex, btn)
            column += 150
            if column == 1200:
                column = 0
                row += 55
            btn = 'butt' + str(nums)
            btn = wx.Button(win, -1, "Exit", pos=(column, row), size=(110, 36))
            btn.Bind(wx.EVT_BUTTON, self.OnClose, btn)
            column += 150
            if column == 1200:
                column = 0
                row += 55
        column = 0
        row = 0
        thispage = "Psalms over 75"
        win = wx.Panel(self.nb, -1, style=wx.CLIP_CHILDREN)
            # win = self.makeColorPanel(wx.WHITE)
        win.SetSize(self.GetBestSize())
            # st.SetForegroundColour(wx.BLACK)
            # self.nb.AddPage(win, thispage)
        self.nb.AddPage(win, thispage)
        for nums in range(1, 76):
            name = str(nums+75)
            btn = 'butt' + str(nums)
            btn = wx.Button(win, -1, name, pos=(column, row), size=(110, 36))
            btn.Bind(wx.EVT_ENTER_WINDOW, self.OnEnterWindow, btn)
            btn.Bind(wx.EVT_LEAVE_WINDOW, self.OnExitWindow, btn)
            btn.Bind(wx.EVT_BUTTON, self.OnChapterClick, btn)
            column += 150
            if column == 1200:
                column = 0
                row += 55
            
        btn = 'butt' + str(nums)
        btn = wx.Button(win, -1, "Back to Index", pos=(column, row), size=(110, 36))
        btn.Bind(wx.EVT_BUTTON, self.OnIndex, btn)
        column += 150
        if column == 1200:
            column = 0
            row += 55
        btn = 'butt' + str(nums)
        btn = wx.Button(win, -1, "Exit", pos=(column, row), size=(110, 36))
        btn.Bind(wx.EVT_BUTTON, self.OnClose, btn)
        column += 150
        if column == 1200:
            column = 0
            row += 55

        self.frame.Show()
 
    def OnPageChanged(self, event):
        if self:
            old = event.GetOldSelection()
            new = event.GetSelection()
            sel = self.nb.GetSelection()
            self.log.write('OnPageChanged,  old:%d, new:%d, sel:%d\n' % (old, new, sel))
        event.Skip()
 
    def OnPageChanging(self, event):
        if self:
            old = event.GetOldSelection()
            new = event.GetSelection()
            sel = self.nb.GetSelection()
            self.log.write('OnPageChanging, old:%d, new:%d, sel:%d\n' % (old, new, sel))
        event.Skip()

    def OnHighClick(self,event):
        sel = self.nb.SetSelection(67)
        self.log.write('OnPageChanging, old:%d, new:%d, sel:%d\n' % (old, new, sel))
        event.Skip()
 
    def OnClick(self, event):
        obj = event.GetEventObject()
        mybutton= event.GetEventObject().GetLabel()
        for finder in range(1,67):
            thisone = books[finder]
            if thisone[:-3] == mybutton:
                print(books[finder])
                mypage = finder
                finder = 67
        sel = self.nb.SetSelection(mypage)
        print("This is the page ", mypage)
        event.Skip()


    def OnChapterClick(self, event):
        obj = event.GetEventObject()
        mybutton= event.GetEventObject().GetLabel()

        print("This is My Button ",mybutton)
        sel = self.nb.GetSelection()
        print ("This is sel in chapter",sel)
        chap =str(sel)
        if chap == "67":
            chap = "19"
        if sel<10:
            chap = "0" + chap
        print(sel)
        
        ch = str(mybutton)
        print ("This is ch ", ch)
        if len(ch) < 2:
            ch = "0" + ch
        if len(ch) <3:
            ch = "0" + ch 
        print("This is ch ",ch)
        cmd = "mpv --no-audio-display /home/norman/Blind/newsue/bi12_" + chap + "__E_" + ch + ".mp3 &"
        print("This would be the command",cmd)
        os.system(cmd)
        event.Skip()


    def OnIndex(self, event):
        obj = event.GetEventObject()
        print("You clicked %s" % obj.GetLabel())
        sel = self.nb.SetSelection(0)
        self.log.write('OnPageChanging, sel:%d\n' % (sel))
        event.Skip()

    def OnClose(self, event):
        self.Destroy()

    def OnEnterWindow(self, event):
        obj = event.GetEventObject()
        sel = self.nb.GetSelection()
        print(sel)
        mybutton= event.GetEventObject().GetId()
        print("This is My Button in Index ",mybutton)
        toplay_book = mybutton*-1 - 2003
        cmd = "mpv --no-audio-display " + book_names[toplay_book] + " &"
        print(cmd)
        os.system(cmd)
        print ("This is toplay ",toplay_book)
        print("You hovered over %s" % obj.GetLabel())
        event.Skip()

    def OnExitWindow(self, event):
        obj = event.GetEventObject()
        print("You hovered over %s" % obj.GetLabel())
        cmd = "killall -9 mpv"
        os.system(cmd)
 
if __name__ == '__main__':
    BibleNB(None)



RE: moving from tkinter to wxpython - Larz60+ - Mar-04-2018

The next step would be to use Natural Language Processing to change
the mp3. Here are some packages that you may be interested in investigating:
General on speech Recognition packages: https://pypi.python.org/pypi/SpeechRecognition/
note that two of these (only) work offline, which is what you want,
snowboy: https://snowboy.kitt.ai/
and CMU Sphinx, but that's on SourceForge which apparently has some major issues after a fire, and
may not be available for some time.


RE: moving from tkinter to wxpython - Larz60+ - Mar-04-2018

I took a look at your code. There are some ugly parts in there.
Are you interested in some suggestions?


RE: moving from tkinter to wxpython - Larz60+ - Mar-04-2018

OK, here's some fun stuff to play with:
get the pocketsphinx wheel here: https://www.lfd.uci.edu/~gohlke/pythonlibs/#pocketsphinx
if you haven't used this site before, here's how it works:
you will see a listing like:
Output:
pocketsphinx‑0.1.3‑cp27‑cp27m‑win32.whl pocketsphinx‑0.1.3‑cp27‑cp27m‑win_amd64.whl pocketsphinx‑0.1.3‑cp34‑cp34m‑win32.whl pocketsphinx‑0.1.3‑cp34‑cp34m‑win_amd64.whl pocketsphinx‑0.1.3‑cp35‑cp35m‑win32.whl pocketsphinx‑0.1.3‑cp35‑cp35m‑win_amd64.whl pocketsphinx‑0.1.3‑cp36‑cp36m‑win32.whl pocketsphinx‑0.1.3‑cp36‑cp36m‑win_amd64.whl pocketsphinx‑0.1.3‑cp37‑cp37m‑win32.whl pocketsphinx‑0.1.3‑cp37‑cp37m‑win_amd64.whl
  • the cp36 refers to cpython version 3.6,
  • the win32 and win64_amd64 distinguish between 32 bit and 64 bit.
  • choose the wheel that matches your system.
  • download it to a directory of your choice
  • start a command window in that directory (somewheels (but not this one) require starting the cmd window in admin mode, this can be done by right clicking the program name in start menu and selecting 'run as admin')
  • run 'pip install wheelname'

once you have that installed, you want to install PyAudio
'pip install PyAudio'
next, follow instructions in the following 'tutorial': http://www.codesofinterest.com/2017/03/python-speech-recognition-pocketsphinx.html
and have fun!


RE: moving from tkinter to wxpython - Barrowman - Mar-04-2018

(Mar-04-2018, 01:07 AM)Larz60+ Wrote: I took a look at your code. There are some ugly parts in there.
Are you interested in some suggestions?

Yes I am interested. I expected that most of it would be ugly.


RE: moving from tkinter to wxpython - Larz60+ - Mar-04-2018

Did your code get posted correctly?
what is the close parenthesis on line 69?
also, please explain book_names, what is path: /home/norman/Blind/recordings/ ?


RE: moving from tkinter to wxpython - Barrowman - Mar-04-2018

(Mar-04-2018, 10:47 AM)Larz60+ Wrote: Did your code get posted correctly?
what is the close parenthesis on line 69?
also, please explain book_names, what is path: /home/norman/Blind/recordings/ ?

Close parenthesis should be removed. The code had quite a number of routines I had previously tried and failed with. They were commented out and I missed deleting it when I tidied up before posting.
The path is where I have the recordings of the various book names. So genesis.mp3 just says the word genesis when that button is entered and so on. I have still not recorded all the names nor the chapters. When they have I will update book_names list.


RE: moving from tkinter to wxpython - Barrowman - Mar-04-2018

(Mar-04-2018, 01:34 AM)Larz60+ Wrote:
  • the cp36 refers to cpython version 3.6,
  • the win32 and win64_amd64 distinguish between 32 bit and 64 bit.
  • choose the wheel that matches your system.
  • download it to a directory of your choice
  • start a command window in that directory (somewheels (but not this one) require starting the cmd window in admin mode, this can be done by right clicking the program name in start menu and selecting 'run as admin')
  • run 'pip install wheelname'

I can see that this is for Windows so, as I only have Linux on this laptop I am uncertain it would be usable. I have a copy of WIn7 Pro and also Win 10 which I intended to install within virtualbox to test code out.


RE: moving from tkinter to wxpython - Larz60+ - Mar-04-2018

you're right, and I knew that you were using Linux, my slip.