Object oriented python pdf
We have previously seen how to write text-only programs which have a command-line interface, or CLI. Now we object oriented python pdf briefly look at creating a program with a graphical user interface, or GUI. Python standard library which serves as an interface to Tk, a simple toolkit. There are many other toolkits available, but they often vary across platforms.
We will see how to make a simple GUI which handles user input and output. GUIs often use a form of OO programming which we call event-driven: the program responds to events, which are actions that a user takes. Python, and must be installed separately. Anything that happens in a user interface is an event. Our application needs to monitor, or listen for, all the events that we find interesting, and respond to them in some way if they occur. To do this, we usually associate certain functions with particular events. For example, a button or a text field needs to be inside some kind of containing window.
The widget classes provide us with a lot of default functionality. Once we have constructed the backbone of our GUI, we will need to customise it by integrating it with our internal application class. Try executing this code for yourself. The window manager is the part of your operating system which handles windows. All the widgets inside a window, like buttons and other controls, may look different in every GUI toolkit, but the way that the window frames and title bars look and behave is determined by your window manager and should always stay the same. Our application should only have one root, but it is possible for us to create other windows which are separate from the main window.
Buttons, however, are designed to cause something to happen when they are clicked. We didn’t have to write any code to make the buttons fire click events or to bind the methods to them explicitly. We also didn’t have to write our own function for closing the window, because there is already one defined as a method on the window object. We did, however, write our own method for printing a message to the console. There are many ways in which we could organise our application class. We could also use inheritance to extend one of the widgets in the tree with our custom functions. The GUI in the previous example has a relatively simple layout: we arranged the three widgets in a single column inside the window.
We have to use one of the available geometry managers to specify a position for each of our widgets, otherwise the widget will not appear in our window. We can mix different alignments in the same container, but this may not work very well for complex layouts. It allows us to position widgets in a more flexible way, using a grid layout. There are some specialised cases, however, in which it can come in useful. Now we can use all this information to create a simple calculator.
We will allow the user to enter a number in a text field, and either add it to or subtract it from a running total, which we will display. We have defined two methods on our class: the first is used to validate the contents of the entry field, and the second is used to update our total. Whenever the value of the field changes, we store the integer value of the contents in self. How do we connect this validation function up to our entry field? When the function is called, these codes will be replaced by different pieces of information about the change which is being made to the entry value.
In our example, we only care about one piece of information: what the new value is going to be. We have written a single handler for updating the total, because what we have to do in all three cases is very similar. We can solve the problem by wrapping the handler in three different functions, each of which calls the handler with a different parameter when it is called. We have used lambda functions to create these wrappers because they are so simple. We know how to update the total because of the parameter which is passed into the handler. Explain why we needed to use lambdas to wrap the function calls in the last example. Rewrite the button definitions to replace the lambdas with functions which have been written out in full.