2019-08-15 00:41:05

i have managed to create an application skeleton of a text editor that i am working on, i have created the main edit box and the menus, and now i am trying to bind events to the menu items, my first attempt was the open file dialog, and i ran it after implementing the code for the open dialog and i got this, note, if you need the code that i used, i can post it, but here is what i got after running this file
Traceback (most recent call last):
  File "window.py", line 53, in <module>
    frame = GUI()
  File "window.py", line 13, in __init__
    self.Bind(wx.EVT_MENU, self.Open, menu_open)
AttributeError: 'GUI' object has no attribute 'Open'

if anyone knows how i can fix this, please tell me, thanks for the help

2019-08-15 01:19:04

Its effectively saying that your GUI() class has no function called self.Open, without seeing the class itself its difficult to say whether its a simple misspelling or something else. Something like this should work as expected:

import wx

class Frame(wx.Frame):
    def __init__(self, title):
        wx.Frame.__init__(self, None, title=title, pos=(150,150), size=(350,200))

    #create menu bar
        menuBar = wx.MenuBar()
    #file Menu tab
        menu = wx.Menu()
    #open file
        m_load = menu.Append(wx.ID_OPEN,"&Open", "Load an image")
    #bind open function
        self.Bind(wx.EVT_MENU, self.Open, m_load)
    #append this menubar tab
        menuBar.Append(menu, "&File")

     #set the menu bar
        self.SetMenuBar(menuBar)

    def Open(self, event):
        self.SetTitle('success')


app = wx.App()
top = Frame("<<project>>")
top.Show()
app.MainLoop()
-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2019-08-15 16:23:23

that previous problem seems to be resolved, but another problem has taken its place
i ran the project, but now, it doesn't do anything, it just shows a blank command prompt window, no text, no trace backs, nothing shows up
here is the code for the window, i don't know if this will send properly, please keep in mind that this is incomplete, some of the menus have not been added
import os
import wx
class GUI(wx.Frame):   
    def __init__(self):
        super().__init__(parent=None, title='Spirit')
        controls = wx.Panel(self)
        text_field = wx.TextCtrl(controls,style = wx.TE_MULTILINE|wx.TE_PROCESS_TAB|wx.TE_NOHIDESEL)
        menuBar = wx.MenuBar()
        firstMenu = wx.Menu()
        new = firstMenu.Append(wx.ID_NEW, "New")
        open = firstMenu.Append(wx.ID_OPEN, "Open")
        self.Bind(wx.EVT_MENU, self.Open, open)
        save = firstMenu.Append(wx.ID_SAVE, "Save")
        saveAs = firstMenu.Append(wx.ID_SAVEAS, "Save As")
        print = firstMenu.Append(wx.ID_PRINT, "Print")
        ExitApp = firstMenu.Append(wx.ID_EXIT, 'Quit')
        menuBar.Append(firstMenu, "&file")
        secondMenu = wx.Menu()
        undo = secondMenu.Append(wx.ID_UNDO,"Undo")
        cut = secondMenu.Append(wx.ID_CUT, "Cut")
        copy = secondMenu.Append(wx.ID_COPY, "Coppy")
        paste = secondMenu.Append(wx.ID_PASTE, "Paste")
        delete = secondMenu.Append(wx.ID_DELETE, "Delete")
        find = secondMenu.Append(wx.ID_FIND, "Find")
        selectAll = secondMenu.Append(wx.ID_SELECTALL, "Select All")
        menuBar.Append(secondMenu, "edit")
        self.SetMenuBar(menuBar)
    def Open(self, event):
        openFileDialog = wx.FileDialog(GUI, "Open", "", "",
      "Plain text files (*.txt)|*.txt",
       wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
        openFileDialog.ShowModal()
        openFileDialog.Destroy()
        self.Show()
if __name__ == '__main__':
    app = wx.App()
    frame = GUI()
    app.MainLoop()

2019-08-16 06:35:49

When you create a widget, you have to enable its "show" function for it to display the window. So at the bottom of the code add in frame.Show() like so:

if __name__ == '__main__':
    app = wx.App()
    frame = GUI()
    frame.Show()
    app.MainLoop()
-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2019-08-19 01:53:29

i am sorry to bother you once again, but another problem has occurred, now, the save as dialog is acting up as well with a different error, first i shall post the traceback and after that will be the code, and please note, i tried my best to solve this problem before posting up her again
Traceback (most recent call last):
  File "window.py", line 46, in <module>
    frame = GUI()
  File "window.py", line 15, in __init__
    self.Bind(wx.EVT_MENU, self.SaveAs, saveas)
NameError: name 'saveas' is not defined
and here is the code
import os
import wx
class GUI(wx.Frame):   
    def __init__(self):
        super().__init__(parent=None, title='Spirit')
        controls = wx.Panel(self)
        text_field = wx.TextCtrl(controls,style = wx.TE_MULTILINE|wx.TE_PROCESS_TAB|wx.TE_NOHIDESEL)
        menuBar = wx.MenuBar()
        firstMenu = wx.Menu()
        new = firstMenu.Append(wx.ID_NEW, "New")
        open = firstMenu.Append(wx.ID_OPEN, "Open")
        self.Bind(wx.EVT_MENU, self.Open, open)
        save = firstMenu.Append(wx.ID_SAVE, "Save")
        saveAs = firstMenu.Append(wx.ID_SAVEAS, "Save As")
        self.Bind(wx.EVT_MENU, self.SaveAs, saveas)
        print = firstMenu.Append(wx.ID_PRINT, "Print")
        ExitApp = firstMenu.Append(wx.ID_EXIT, 'Quit')
        menuBar.Append(firstMenu, "&file")
        secondMenu = wx.Menu()
        undo = secondMenu.Append(wx.ID_UNDO,"Undo")
        cut = secondMenu.Append(wx.ID_CUT, "Cut")
        copy = secondMenu.Append(wx.ID_COPY, "Coppy")
        paste = secondMenu.Append(wx.ID_PASTE, "Paste")
        delete = secondMenu.Append(wx.ID_DELETE, "Delete")
        find = secondMenu.Append(wx.ID_FIND, "Find")
        selectAll = secondMenu.Append(wx.ID_SELECTALL, "Select All")
        menuBar.Append(secondMenu, "edit")
        self.SetMenuBar(menuBar)
    def Open(self, event):
        openFileDialog = wx.FileDialog(frame, "Open", "", "",
      "text files (*.txt)|*.txt",
       wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
        openFileDialog.ShowModal()
        print(openFileDialog.GetPath())
        openFileDialog.Destroy()
    def SaveAs(self, event):
        saveFileDialog = wx.FileDialog(frame, "saveas", "", "",
       "text files (*.txt)|*.txt",
        wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT)
        saveFileDialog.ShowModal()
        print(saveFileDialog.GetPath())
        saveFileDialog.Destroy()
        self.Show()
if __name__ == '__main__':
    app = wx.App()
    frame = GUI()
    frame.Show()
    app.MainLoop()
hopefully, this is the last error i will have to bother people with
smile

2019-08-19 02:49:02

Its no problem, heh. It looks like this is a simple capitalization mistake as indicated in the NameError traceback, "saveas" is all lower case, but in GUI.__init__() its defined as "saveAs" with a capitol A for as:

class GUI(wx.Frame):   
    def __init__(self):
        saveAs = firstMenu.Append(wx.ID_SAVEAS, "Save As")
        self.Bind(wx.EVT_MENU, self.SaveAs, saveas)

It seems to work fine otherwise, though I also noticed a minor spelling issue in the text of the copy menu option:

        copy = secondMenu.Append(wx.ID_COPY, "Coppy")
-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2019-08-19 03:29:13

thank you so much, it works now, hopefully this is the last bug with this thingy, lol

2019-08-19 03:36:46

sorry for the double post, but how do i set a menu short cut so instead of it saying save as s it could say something like save as A, and when you press the A key in the file menu, it will activate the shortcut that i have created

2019-08-19 04:39:58 (edited by magurp244 2019-08-19 04:42:55)

Use the "&" symbol in the name description, or alternatively you can use "\t" for a different kind of keyboard shortcut, like so:

    #create menu bar
        menuBar = wx.MenuBar()
    #file Menu tab
        menu = wx.Menu()
    #open file
        m_load = menu.Append(wx.ID_OPEN,"&Open", "Load an image")
        self.Bind(wx.EVT_MENU, self.Open, m_load)
    #exit program
        m_exit = menu.Append(wx.ID_EXIT, "E&xit\tAlt-X", "Close window and exit program.")
        self.Bind(wx.EVT_MENU, self.OnClose, m_exit)
    #append this menubar tab
        menuBar.Append(menu, "&File")

In this example, "O" in Open is setup as the shortcut, "X" is setup for the shortcut to exit, and "F" is the shortcut for File, its whichever the character is after the "&" symbol. But Alt+X is also setup as a global keyboard shortcut you can use without opening the file menu first, by using "\t" followed by the key shortcut in the name string, in this case "\tAlt-X".

-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer