How to Build a Python GUI Application With wxPython

by | Dec 27, 2019 | Uncategorized | 0 comments

All Premium Themes And WEBSITE Utilities Tools You Ever Need! Greatest 100% Free Bonuses With Any Purchase.

Greatest CYBER MONDAY SALES with Bonuses are offered to following date: Get Started For Free!
Purchase Any Product Today! Premium Bonuses More Than $10,997 Will Be Emailed To You To Keep Even Just For Trying It Out.
Click Here To See Greatest Bonuses

and Try Out Any Today!

Here’s the deal.. if you buy any product(s) Linked from this sitewww.Knowledge-Easy.com including Clickbank products, as long as not Google’s product ads, I am gonna Send ALL to you absolutely FREE!. That’s right, you WILL OWN ALL THE PRODUCTS, for Now, just follow these instructions:

1. Order the product(s) you want by click here and select the Top Product, Top Skill you like on this site ..

2. Automatically send you bonuses or simply send me your receipt to consultingadvantages@yahoo.com Or just Enter name and your email in the form at the Bonus Details.

3. I will validate your purchases. AND Send Themes, ALL 50 Greatests Plus The Ultimate Marketing Weapon & “WEBMASTER’S SURVIVAL KIT” to you include ALL Others are YOURS to keep even you return your purchase. No Questions Asked! High Classic Guaranteed for you! Download All Items At One Place.

That’s it !

*Also Unconditionally, NO RISK WHAT SO EVER with Any Product you buy this website,

60 Days Money Back Guarantee,

IF NOT HAPPY FOR ANY REASON, FUL REFUND, No Questions Asked!

Download Instantly in Hands Top Rated today!

Remember, you really have nothing to lose if the item you purchased is not right for you! Keep All The Bonuses.

Super Premium Bonuses Are Limited Time Only!

Day(s)

:

Hour(s)

:

Minute(s)

:

Second(s)

Get Paid To Use Facebook, Twitter and YouTube
Online Social Media Jobs Pay $25 - $50/Hour.
No Experience Required. Work At Home, $316/day!
View 1000s of companies hiring writers now!

Order Now!

MOST POPULAR

*****
Customer Support Chat Job: $25/hr
Chat On Twitter Job - $25/hr
Get Paid to chat with customers on
a business’s Twitter account.

Try Free Now!

Get Paid To Review Apps On Phone
Want to get paid $810 per week online?
Get Paid To Review Perfect Apps Weekly.

Order Now
!
Look For REAL Online Job?
Get Paid To Write Articles $200/day
View 1000s of companies hiring writers now!

Try-Out Free Now!

How To Develop Your Skill For Great Success And Happiness Including Become CPA? | Additional special tips From Admin

Talent Development is actually the number 1 imperative and important matter of getting authentic achievements in all of the procedures as you actually found in this culture as well as in All over the world. Thus fortunate to look at with everyone in the subsequent concerning precisely what powerful Skill level Improvement is; exactly how or what strategies we get the job done to reach aspirations and sooner or later one should deliver the results with what anyone prefers to undertake any time of day with regard to a total your life. Is it so good if you are able to acquire proficiently and get achieving success in whatever you believed, planned for, follower of rules and been effective hard each individual daytime and most certainly you become a CPA, Attorney, an entrepreneur of a great manufacturer or quite possibly a healthcare professional who could highly contribute terrific benefit and valuations to others, who many, any society and neighborhood clearly admired and respected. I can's imagine I can help others to be top notch expert level exactly who will make contributions serious solutions and relief valuations to society and communities at this time. How thrilled are you if you come to be one such as so with your own personal name on the label? I have landed at SUCCESS and overcome most of the difficult portions which is passing the CPA examinations to be CPA. Furthermore, we will also protect what are the risks, or different complications that will be on ones own approach and the simplest way I have personally experienced all of them and will certainly present you the best way to conquer them. | From Admin and Read More at Cont'.

How to Build a Python GUI Application With wxPython

A Graphical User Interface or GUI is a user interface that includes graphical elements that enable a person to communicate with electronic devices like computers, hand-held devices, and other appliances. It displays information using icons, menus, and graphics. They are handled with the help of a pointing device such as a mouse, trackball or a stylus.

A GUI is basically an application that has windows, buttons and lots of other widgets that allows a user to interact with your application. A web browser is a common example of a GUI that has buttons, tabs, and the main window and where the content is displayed.

It was developed by the Xerox Palo Alto research laboratory in the late 1970s. Today, every OS has its own GUI. Software applications make use of these and develop their own GUIs.

Python contains many GUI toolkits, out which Tkinter, wxPython, and PyQt are the important ones. All these toolkits have the ability to work with Windows, macOS, and Linux with the additional quality of working on mobile phones.

wxPython was first released in the year 1998. It is an open-source cross-platform Python GUI toolkit used as a wrapper class around a C++ library called wxWidgets. The main feature of wxPython which distinguishes itself from other toolkits like PyQt and Tkinter is that it uses actual widgets on the native platform where required. This allows the wxPython applications to look native to the operating system in which it is running.

The wxPython toolkit contains a lot of core widgets along with many custom widgets which you can download from the Extra Files section of the wxPython official page.

Here, there is a download of the wxPython demo package which is an application demonstrating the robustness of the widgets included with wxPython. The main advantage of this demo is that you can view it one tab and run it in another and it also allows to edit and re-run the code to observe the changes.

You will be using the latest wxPython release, wxPython 4, which is also called the wxPython’s Project Phoenix. It is a new implementation of wxPython that aims at improving the speed, maintainability, and extensibility of wxPython. The wxPython 3 and wxPython 2 were built only for Python 2. The maintainer of wxPython rejected a lot of aliases and cleaned up a lot of code to make wxPython more easy and Pythonic.

If you are migrating from an older version of wxPython to wxPython 4, take a look at the following references:

The Phoenix version is compatible with both Python 2.7 and Python 3. You can use pip to install wxPython 4:

You will get a prerequisites section on the Github page of wxPython which will provide information to install wxPython on Linux systems.

You can also look into the Extras Linux section to learn about the Python wheels for both GTK2 and GTK3 versions. To install one of the wheels, use the command below:

Remember to modify the command to match with the version of Linux.

As mentioned earlier, GUI is nothing but an interface that allows user interaction.

Common components of the user interfaces:

These items are generally known as widgets. wxPython supports many other common widgets and many custom widgets that are arranged in a logical manner by a developer to allow user interaction.

A GUI works by waiting for the user to perform an action. This is known as an event. An event occurs when something is typed by the user or when the user uses their mouse to press a button or some widget while the application is in focus.

The GUI toolkit runs an infinite loop called an event loop underneath the covers. The task of the event loop is to act on occurred events on the basis of what the developer has coded the application to do. The application ignores the event when it is not able to catch it.

When you are programming a graphical user interface, make sure to attach the widgets to event handlers in order to make your application do something.

You can also block an event loop to make the GUI unresponsive which will appear to freeze to the user. This is a special consideration for you to keep in mind while working with event loops. Launch a special thread or process whenever a GUI takes longer than a quarter of a second to launch a process.

The frameworks of wxPython contain special thread-safe methods that you can use to communicate back to your application. This informs the thread is finished or given an update.

An application skeleton is basically used for prototyping. It is a user interface comprising of widgets that do not contain event handlers. You just need to create the GUI and show it to the stakeholders for signing off and avoid spending time on the backend logic.

An example of creating a Hello World application with Python:

In the example above, there are two parts of the program – wx.App and wx.Frame. The former one is wxPython’s application object which is basically required for running the GUI. It initiates the .MainLoop() which is the event loop you have learned earlier.

The latter part creates a window for user interaction. It informs wxPython that the frame has no parent and its title is Hello World. If you run the code above, this is how it will look like:

How to create a Skeleton Application in Python

The application will look different if you execute it in Mac or Linux.

Note: Mac users may get the following message: This program needs access to the screen. Please run with a Framework build of Python, and only when you are logged in on the main display of your Mac. If you see this message and you are not running in a virtualenv, then you need to run your application with pythonw instead of python. If you are running wxPython from within a virtualenv, then see the wxPython wiki for the solution.

The minimize, maximize and exit will be included in the wx.Frame by default. However, most wxPython code will require you to make the wx.Frame as a subclass and other widgets in order to grab the full power of the toolkit.

Let us rewrite the code using class:

This code can be used as a template for your application.

The wxPython toolkit allows you to create rich applications from more than one hundred widgets. But it can be very daunting to choose the perfect widget from such a large number, so wxPython has included a wxPython Demo which contains a search filter which will help you to find the right widget from the list.

Now, let us add a button and allow the user to enter some text by adding a text field:

When you run the code, the application will look like this:

The first widget that is recommended on Windows is wx.Panel. It makes the background color of the frame as the right shade of gray. Tab traversal is disabled without a Panel on Windows.

If the panel is the sole child of the frame, it will be expanded automatically to fill the frame with itself. The next thing you need to do is to add a wx.TextCtrl to the panel. The first argument is always that which parent the widget should go to for almost all widgets. So if you are willing to keep the text control and the button on the top of the panel, it is the parent you need to specify.

You also need to inform wxPython about the position of the widget. You can do it using the pos parameter. The default location is (0,0) which is actually at the upper left corner of the parent. So to change the text control, you can change the position of the frame, you can shift its left corner 5 pixels from the left(x) and 5 pixels from the top(y). Finally, you can add your button to the panel and label it. You can also set the y-coordinate to 55 to prevent the overlapping of widgets.

Absolute positioning is the technique found in most GUI toolkits by which you can provide the exact coordinates for your widget’s position.

There might be situations when you need to keep track of all your widgets and relocate the widgets in case of a complex application. This can be a really difficult thing to do. However, most modern-day toolkits provide a solution for this, which we’ll study next.

Sizers are methods to define the control layout in dialogs in wxPython. They have the ability to create dialogs that are not dependent on the platform. They manage the positioning of the widgets and adjust them when the user resizes the application window.

Some of the primary types of sizers that are commonly used are:

An example code to add wx.BoxSizer to the previous code:

In the example above, an instance of wx.BoxSixer is created and passed to wx.VERTICAL which is actually the orientation that widgets are included in the sizer. The widgets will be added in a vertical manner from top to bottom. You can also set the BoxSizer’s orientation to wx.HORIZONTAL. In this case, the widgets are added from left to right.  

You can use .Add() to a widget to a sizer which takes maximum five arguments as follows: 

However, in this example, the wx.EXPAND flag is replaced with wx.CENTER to display the button in the center on-screen. 

When you run the code, your application will look something like this:

Run code of python

Though your application looks cool, but it really does nothing. The button you have created does nothing on pressing it. Let us give the button a job:

You can attach event bindings to the widgets in wxPython. This allows them to respond to certain types of events.

If you want the button to do something, you can do it using the button’s .Bind() method. It takes the events you want to bind to, the handler to call an event, an optional source, and a number of optional ids. In the example above, the button object is binded to wx.EVT_BUTTON and told to call button_press when the event gets fired.

.button_press also accepts a second argument by convention that is called event. The event parameter suggests that the second argument should be an event object.

You can get the text control’s contents with the help of GetValue() method within .button_press.

Consider a situation where you are asked to create an MP3 tag editor. The foremost thing you need to do is to look out for the required packages.

Consider a situation where you are asked to create an MP3 tag editor. The foremost thing you need to do is to look out for the required packages.

If you make a Google search for Python mp3 tag editor, you will find several options as below:

Out of these, eyeD3 is a better choice than the other two since it has a pretty nice API that can be used without getting bogged down with MP3’s ID3 specification.

You can install eyeD3 using pip from your terminal:

If you want to install eyeD3 in macOS, you have to install libmagic using brew. Linux and Windows users can easily install using the command mentioned above.

The very first thing you must do before designing an interface is to sketch out how you think the interface should look.

The user interface should perform the following tasks:

If you want to open a file or a folder, you need to have a menu or a button in your user interface. You can do that with a File menu. You will also need a widget to see the tags for multiple MP3 files. A tabular structure consisting of columns and rows would be perfect for this case since you can have labeled columns for the MP3 tags. wxPython toolkit consists of afew widgets to perform this task:

wx.ListCtrl would be a better option of these two since the Grid widget is overkill and complex in nature. Finally, you can use a button to perform the editing tasks.

Below is an illustration of what the application should look like:

MP3 Tog Editor

You can refer to a lot of approaches when you are creating a user interface. You can follow the Model-View-Controller design pattern that is used for developing user interfaces which divides the program logic into three interconnected elements. You should know how to split up classes and how many classes should be included in a single file and so on.

However, in this case, you need only two classes which are as follows:

 Let’s start with imports and the panel class:

In this example above, the eyed3 package, glob package, and the wx package are imported. Then, the user interface is created by making wx.Panel a subclass. A dictionary row_obj_dict is created for storing data about the MP3s. 

The next thing you do is create a wx.ListCtrl and set it to report mode, i.e. wx.LC_REPORT. This report flag is the most popular among all but you can also choose your own depending upon the style flag that you pass in. Now you need to call .InsertColumn() to make the ListCtrl have the correct headers and then provide the index of the column, its label and the width of the column pixels. 

Finally, you need to add your Edit button, an event handler, and a method. The code for the frame is as follows:

This class function is a better and simpler approach than the previous one because you just need to set the title of the frame and instantiate the panel class, MP3Panel. The user interface will look like this after all the implementations:

The next thing we will do is add a File menu to add MP3s to the application and also edit their tags.

The very first thing you need to do to make your application work is to update the wx.Frame class to include the File menu which will allow you to add MP3 files.

Code to add a menu bar to our application:

In the example code above, .create_menu() is called within the class’s constructor and then two instances – wx.MenuBar and wx.Menu are created.

Now, if you’re willing to add an item to the menu, you need to call the menu instance’s .Append() and pass the following things:

After that call the menubar’s .Append() to add the menu to the menubar. It will take the menu instance and the label for menu. The label is called as &File so that a keyboard shortcut is created to open the File menu using just the keyboard.

Now self.Bind() is called to bind the frame to wx.EVT_MENU. This informs wxPython about which handler should be used and which source to bind the handler to. Lastly, call the frame’s .SetMenuBar and pass it the menubar instance. Your menu is now added to the frame.

Now let’s come back to the menu item’s event handler:

You can use wxPython’s wx.DirDialog to choose the directories of the correct MP3 folder. To display the dialog, use .ShowModal(). This will display the dialog modally but will disallow the user to interact with the main application.  You can get to the user’s choice of path using .GetPath() whenever the user presses the OK button. This path has to be added to the panel class and this can be done by the panel’s .update_mp3_listing().

Finally, you will have to close the dialog and the best method is using .Destroy().  There are methods to close the dialog like .Close() which will just dialog but will not destroy it, so .Destroy() is the most effective option to prevent such situation.

Now let’s update the MP3Panel class starting with .update_mp3_listing():

In the example above, the current directory is set to the specified folder and the list control is cleared. The list controls stay fresh and shows the MP3s you’re currently working with. Next, the folder is taken and Python’s globmoduleis used to search for the MP3 files. Then, the MP3s are looped over and converted into eyed3 objects. This is done by calling the .load() of eyed3. After that, you can add the artist, album, and the title of the Mp3 to the control list given that the MP3s have the appropriate tags.

.InsertItem() is used to add a new row to a list control for the first time and SetItem()  is used to add rows to the subsequent columns. The last step is to save your MP3 object to your Python dictionary row_obj_dict.

Now to edit an MP3’s tags, you need to update the .on_edit() event handler:

The user’s selection is taken by calling the list control’s .GetFocusedItem(). It will return -1 if the user will not select anything in the list control. However, if you want to extract the MP3 obj3ct from the dictionary, the user have to select something. You can then open the MP3 tag editor dialog which will be a custom dialog. 

As before, the dialog is shown modally, then the last two lines in .on_edit() will execute what will eventually display the current MP3 tag information. 

Summary

Let us sum up what we have learned in this article so far – 

The main feature of the wxPython Graphical User Interface is its robustness and a large collection of widgets that you can use to build cross-platform applications. Since you have now learned how to create a working application, that is an MP3 tag editor, you can try your hand to enhance this application to a more beautiful one with lots of new features or you can perhaps create your own wonderful application. To gain more knowledge about Python tips and tricks, check our Python tutorial and get a good hold over coding in Python by joining the Python certification course.

  • Classic version vs project Phoenix
  • Phoenix Migration Guide
  • Main Window.
  • Menu.
  • Toolbar.
  • Buttons.
  • Text Entry.
  • Labels.
  • wx.BoxSizer
  • wx.GridSizer
  • wx.FlexGridSizer
  • window ( the widget )– This is the widget that is added to the sizer. 
  • proportion – It sets how much space corresponding to other widgets in the sizer will the widget should take. By default, the proportion is zero which leaves the wxPython to its original proportion. 
  • flag – It allows you to pass in multiple flags by separating them with a pipe character: |. The text control is added using wx.ALL and wx.EXPAND flags. The wx.ALL flag adds a border on all sides of the widget. On the other hand, wx.EXPAND expands the widgets as much as the sizer can be expanded. 
  • border – This parameter informs wxPython about the number of pixels of border needed around the widget.  
  • userData – It is a rare argument that is used for resizing in case of complex applications. 
  • mp3 -tagger
  • eyeD3
  • mutagen
  • Open up one or more MP3 files.
  • Display the current MP3 tags.
  • Edit an MP3 tag.
  • wx.grid.Grid
  • wx.ListCtrl
  • wx.Panel class
  • wx.Frame class
  • A unique identifier
  • Label
  • A help string
  • Installing wxPython and Working with wxPython’s widgets 
  • Working of events in wxPython 
  • Comparing absolute positioning with sizers 
  • Creating a skeleton application and a working application 
  • Research & References of How to Build a Python GUI Application With wxPython|A&C Accounting And Tax Services
    Source

    From Admin and Read More here. A note for you if you pursue CPA licence, KEEP PRACTICE with the MANY WONDER HELPS I showed you. Make sure to check your works after solving simulations. If a Cashflow statement or your consolidation statement is balanced, you know you pass right after sitting for the exams. I hope my information are great and helpful. Implement them. They worked for me. Hey.... turn gray hair to black also guys. Do not forget HEALTH? Competence Advancement is usually the number 1 vital and significant point of attaining a fact achievement in every vocations as most people saw in our modern culture along with in World-wide. So fortuitous to explore with you in the subsequent pertaining to what precisely powerful Talent Development is;. how or what procedures we function to enjoy objectives and in the end one might deliver the results with what the person delights in to can just about every single time of day intended for a entire everyday life. Is it so fantastic if you are able to improve successfully and see accomplishment in what exactly you believed, steered for, self-displined and labored really hard every working day and definitely you turned out to be a CPA, Attorney, an person of a good sized manufacturer or even a general practitioner who will be able to very play a role excellent support and valuations to many people, who many, any population and neighborhood definitely popular and respected. I can's believe that I can benefit others to be top notch competent level who will add vital systems and aid values to society and communities currently. How cheerful are you if you come to be one similar to so with your unique name on the label? I have landed at SUCCESS and rise above all of the the complicated components which is passing the CPA qualifications to be CPA. On top of that, we will also go over what are the risks, or many other matters that could possibly be on ones own means and exactly how I have professionally experienced all of them and definitely will demonstrate you how to defeat them.

    Send your purchase information or ask a question here!

    11 + 15 =

    0 Comments

    Submit a Comment

    World Top Business Management Tips For You!

    Business Best Sellers

     

    Get Paid To Use Facebook, Twitter and YouTube
    Online Social Media Jobs Pay $25 - $50/Hour.
    No Experience Required. Work At Home, $316/day!
    View 1000s of companies hiring writers now!
    Order Now!

     

    MOST POPULAR

    *****

    Customer Support Chat Job: $25/hr
    Chat On Twitter Job - $25/hr
    Get Paid to chat with customers on
    a business’s Twitter account.
    Try Free Now!

     

    Get Paid To Review Apps On Phone
    Want to get paid $810 per week online?
    Get Paid To Review Perfect Apps Weekly.
    Order Now!

    Look For REAL Online Job?
    Get Paid To Write Articles $200/day
    View 1000s of companies hiring writers now!
    Try-Out Free Now!

     

     

    How to Build a Python GUI Application With wxPython

    error: Content is protected !!