Can someone guide me how to make menu in python please?

http://www.daniweb.com/forums/post693793.html#post693793

This is a previous post of what sort of menu i need.

Can someone just give me a start please.

Cheers

Dani AI

Generated

You are already on the right track using wxPython, . Building on and : create menu items, then bind each to a handler. That is the key step many beginners miss. See the official docs for menus and event binding if you want the deeper details. (docs.wxpython.org)

Inside your frame (after you have a menubar and a menu), add and wire your four actions like this:

# in your frame's __init__, after creating a wx.Menu instance named lin_menu
coeff_it = lin_menu.Append(-1, "&Enter coefficients\tCtrl+E")
check_it = lin_menu.Append(-1, "Check iterative solvability")
gs_it    = lin_menu.Append(-1, "Solve with Gauss-Seidel")
jj_it    = lin_menu.Append(-1, "Solve with Gauss-Jacobi")

self.Bind(wx.EVT_MENU, self.on_enter_coeff, coeff_it)
self.Bind(wx.EVT_MENU, self.on_check_iterative, check_it)
self.Bind(wx.EVT_MENU, self.on_gauss_seidel, gs_it)
self.Bind(wx.EVT_MENU, self.on_gauss_jacobi, jj_it)

Then implement the handlers. For item (2), a quick practical test is strict diagonal dominance; if true, Jacobi and Gauss-Seidel will converge. You can check it like this:

def is_diagonally_dominant(self, A):
    n = len(A)
    return all(abs(A[i][i]) > sum(abs(A[i][j]) for j in range(n) if j != i)
               for i in range(n))

def on_check_iterative(self, evt):
    msg = "Likely to converge." if self.is_diagonally_dominant(self.A) \
          else "No guarantee; try reordering rows or scaling."
    wx.MessageBox(msg, "Iterative solvability")

Strict diagonal dominance is a simple, commonly used sufficient condition for convergence of Jacobi and Gauss-Seidel. (en.wikipedia.org)

Notes for follow-ups:

  • : "No module named wx" means wxPython is not installed for your interpreter. Install it with: py -m pip install -U wxPython (Windows) or python3 -m pip install -U wxPython. (wxpython.org)
  • If you prefer a console menu, Python 3.10+ has match/case (switch-like) you can use for dispatch. (docs.python.org)

This keeps your GUI tidy and lets you drop your Gauss-Seidel/Jacobi code straight into on_gauss_seidel and on_gauss_jacobi. is right that no one should do the whole assignment, but the wiring above should get you unstuck.

Recommended Answers

All 10 Replies

Here's a start: Google It

It really depends. Are you using wxPython Tkinter or some other kind of GUI becuase they all differ.
I can give you an example in wxPython

import wx

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None)
        self.menubar = wx.MenuBar()
        self.file = wx.Menu()
        self.file.Append(wx.ID_ANY,'&Test')
        self.menubar.Append(self.file,'File')
        self.SetMenuBar(self.menubar)
        self.Show()

app = wx.App(False)
frame = MainFrame()
app.MainLoop()

Hope that helps!

Start with this!


import wx
class TextEditor(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(350, 600))
        # self/Text editor is now a Window with all attribute of a frame
        # create status bar
        self.CreateStatusBar()
        # set default status bar string
        self.SetStatusText("For help press F1")
        # define sizers
        vbox = wx.BoxSizer(wx.VERTICAL)
        hbox = wx.BoxSizer(wx.HORIZONTAL)
                
        #Add menu
        #define menubar and menu
        menubar = wx.MenuBar()
        # attach menubar to the frame
        self.SetMenuBar(menubar)
        #define menus
        filemenu = wx.Menu()
        # Append items        
        filemenu.Append(-1, "&New", "New Blank file")        
        #-----------------------------------------------
        # Append a separator
        filemenu.AppendSeparator()       
        #Print menu
        filemenu.Append(-1, "&Print", "Print file")
        #-------------------------------------------------------
        #Append print menu to menubar 
        menubar.Append(filemenu, "&File")
        
        
        #------------------------------------
        
        #center the window
        self.Center()
        #show the window(without this the windows will not show up)
        self.Show(True)
       

#our main application
app = wx.App(False)
TextEditor(None, -1, "Text Editor")
app.MainLoop()

thnx and to jlm699 im sory fot the pm but i've google it already and im not getting any help because im using Dr python which has wxPython integrated.

Can i know how to modify the above codes so that i can add something to it plz.

Thnx again

what do you need to add?

I need to add
"Your program should display a menu which will allow the user to choose whether he wants to (1)
coefficients (2) check whether the linear system can be solved using iteration (3) calculate and display the
required results using Gauss-Seidel (4) calculate and display the required results using Gauss-Jacobi"

I need from the above question 4 menu that the user can click from.

Also i would be grateful if someone could tell me how i modify the codes for any particular menu(i worked in Visual Basic 6.0 where interfaces are much easier than python)
Thnx again.

I have done some modification to show you how to add it:
For more info you can consider www.zetcode.com. BTW any Python IDE can do it if only wxpython is installed


import wx
class TextEditor(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(350, 600))
        # self/Text editor is now a Window with all attribute of a frame
        # create status bar
        self.CreateStatusBar()
        # set default status bar string
        self.SetStatusText("For help press F1")
        # define sizers
        vbox = wx.BoxSizer(wx.VERTICAL)
        hbox = wx.BoxSizer(wx.HORIZONTAL)
                
        #Add menu
        #define menubar and menu 
        menubar = wx.MenuBar()
        # attach menubar to the frame
        self.SetMenuBar(menubar)
        #define menus
        filemenu = wx.Menu()
        #Add any other menu using same trick eg. help = wx.Menu() or print = wx.Menu()
        # Append items  to menu predefined (For now file menu)    
        filemenu.Append(-1, "&New", "New Blank file")  
        # same for help menu i.e help.Append(menu id, menu caption, status bar text )      
        #-----------------------------------------------
        # Append a separator -- line to separate menus
        filemenu.AppendSeparator()       
        #Print menu
        filemenu.Append(-1, "&Print", "Print file")
        #-------------------------------------------------------
        #Append print menu to menubar 
        menubar.Append(filemenu, "&File")
        # also menubar.Append(menu name, caption)
         # Note: as in VB, & is for ALT+F
        
        
        #------------------------------------
        
        #center the window
        self.Center()
        #show the window(without this the windows will not show up)
        self.Show(True)
       

#our main application
app = wx.App(False)
TextEditor(None, -1, "Text Editor")
app.MainLoop()

Have that helped you??

Can u give me the codes for the above menu i mean with the questions i gave?

Also i want to know who to modify menu that i add?

Your asking for a full code solution to your assignment? I think you may be going wrong there

Chris

Hi, i'm using PythonWin. When i used ur code which import wx, shows error: No module named wx.
I hope U will help me, i'm pretty new in programming in python.
Btw. sorry for my english.

EDIT: is there any method to create "switch...case" like in C ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.