Python gui programming cookbook - second edition pdf download






















Lines and define the width and height of the ScrolledText widget we are about to create. These are hardcoded values we are passing into the ScrolledText widget constructor in line These values are magic numbers found by experimentation to work well. By setting the wrap property to tk. WORD we are telling the ScrolledText widget to break lines by words so that we do not wrap around within a word. The default option is tk. CHAR , which wraps any character regardless of whether we are in the middle of a word.

The second screenshot shows that the vertical scrollbar moved down because we are reading a longer text that does not entirely fit into the x, y dimensions of the SrolledText control we created. Setting the columnspan property of the grid widget to 3 for the SrolledText widget makes this widget span all the three columns.

If we do not set this property, our SrolledText widget would only reside in column one, which is not what we want. So far, we have created several widgets of the same type for example, Radiobutton by basically copying and pasting the same code and then modifying the variations for example, the column number. In this recipe, we start refactoring our code to make it less redundant.

We are refactoring some parts of the previous recipe's code, Using scrolled text widgets , so you need that code to apply this recipe to. Running this code will create the same window as before, but our code is much cleaner and easier to maintain. This will help us when we expand our GUI in the coming recipes. In line 89, we set a default value to the tk.

IntVar variable that we named radVar. This is important because, while in the previous recipe we had set the value for Radiobutton widgets starting at 1, in our new loop it is much more convenient to use Python's zero-based indexing. If we did not set the default value to a value outside the range of our Radiobutton widgets, one of the radio buttons would be selected when the GUI appears.

While this in itself might not be so bad, it would not trigger the callback and we would end up with a radio button selected that does not do its job that is, change the color of the main win form. In line 95, we replace the three previously hardcoded creations of the Radiobutton widgets with a loop that does the same.

It is just more concise fewer lines of code and much more maintainable. For example, if we want to create instead of just three Radiobutton widgets, all we have to change is the number inside Python's range operator.

We would not have to type or copy and paste 97 sections of duplicate code, just one number. This recipe concludes the first chapter of this book. All the following recipes in all of the next chapters will build upon the GUI we have constructed so far, greatly enhancing it.

Burkhard Meier has more than 19 years of professional experience working in the software industry as a software tester and developer, specializing in software test automation development, execution, and analysis. He has a very strong background in Python 3 software test automation development, as well as in SQL relational database administration, the development of stored procedures, and debugging code.

Over the past three years, he has also developed several video courses on Python for Packt, the latest course being Mastering Object-Oriented Programming with Python. Find out how to create visually stunning and feature-rich applications by empowering Python's built-in TKinter GUI toolkit. About this book Python is a multi-domain, interpreted programming language. Publication date: May Publisher Packt. Pages ISBN Download code from GitHub. Chapter 1. Creating our first Python GUI Preventing the GUI from being resized Adding a label to the GUI form Creating buttons and changing their text property Text box widgets Setting the focus to a widget and disabling widgets Combo box widgets Creating a check button with different initial states Using radio button widgets Using scrolled text widgets Adding several widgets in a loop.

Creating our first Python GUI. Getting ready. Note All the recipes in this book were developed using Python 3. They have not been tested on any other configuration. As Python is a cross-platform language, the code from each recipe is expected to run everywhere. If you are using a Mac, it does come with built-in Python, yet it might be missing some modules such as tkinter, which we will use throughout this book. We are using Python 3. If you are using a Mac or Python 2, you might have to install Python 3.

If you really wish to run the code in this book on Python 2. For example, tkinter in Python 2. The Python 2. Why hold on to the past, unless you really have to? How to do it…. How it works…. Note A little note about classes and types: In Python, every variable always has a type. We cannot create a variable that does not have a type. Yet, in Python, we do not have to declare the type beforehand, as we have to do in the C programming language. Python is smart enough to infer the type.

C , at the time of writing this book, also has this capability. Using Python, we can create our own classes using the class keyword instead of the def keyword. In order to assign the class to a variable, we first have to create an instance of our class. If this sounds confusing, do not worry. We will cover OOP in the coming chapters. Note An event loop is a mechanism that makes our GUI work. We can think of it as an endless loop where our GUI is waiting for events to be sent to it.

We can write all of our GUI code in advance and nothing will be displayed on the user's screen until we call this endless loop win. The event loop ends when the user clicks the red X button or a widget that we have programmed to end our GUI. When the event loop ends, our GUI also ends. There's more…. Preventing the GUI from being resized.

Adding a label to the GUI form. Note ttk stands for themed tk. Creating buttons and changing their text property. Note GUIs are event-driven. Text box widgets. Note Python is a dynamically typed language and infers the type from the assignment. What this means is that if we assign a string to the name variable, it will be of the string type, and if we assign an integer to name , its type will be integer.

Using tkinter , we have to declare the name variable as the type tk. StringVar before we can use it successfully. The reason is that tkinter is not Python. We can use it from Python, but it is not the same language. Setting the focus to a widget and disabling widgets. Note On a Mac, you might have to set the focus to the GUI window first before being able to set the focus to the Entry widget in this window.

Note Note how the cursor now defaults to residing inside the text Entry box. Combo box widgets. Creating a check button with different initial states. Using radio button widgets. Note While this is the first recipe that changes the color of a widget, quite honestly, it looks a bit ugly. Using scrolled text widgets.

Adding several widgets in a loop. Read more Unlock this book with a 7-day free trial. About the Author Burkhard Meier Burkhard Meier has more than 19 years of professional experience working in the software industry as a software tester and developer, specializing in software test automation development, execution, and analysis.

Browse publications by this author. It gives fairly good guidance and the examples all so far work. That rates 5 stars. It suffers, as do almost every programming book I've encountered , because the author is too close to the forest to see the trees.

It would have been good to have extended the 'How it Works' sections with more clarification. Some of my lack of understanding may be that I am not an experienced Python programmer. May pages. Moore and 1 more. Meier has more than 17 years of professional experience working in the software industry as a software tester and developer, specializing in software test automation development, execution, and analysis.

He has a very strong background in Python 3 software test automation development, as well as in SQL relational database administration, the development of stored procedures, and debugging code.

While experienced in Visual Studio. Being highly appreciative of art, beauty, and programming, the author developed GUIs in C and Python to streamline everyday test automation tasks, enabling these automated tests to run unattended for weeks, collecting very useful data to be analyzed and automatically plotted into graphs and e-mailed to upper management upon completion of nightly automated test runs.

This site comply with DMCA digital copyright. We do not store files not owned by us, or without the permission of the owner. We also do not have links that lead to sites DMCA copyright infringement. If You feel that this book is belong to you and you want to unpublish it, Please Contact us.

About the Book This book will guide you from the very basics of creating a fully functional GUI in Python with only a few lines of code. Instructions and Navigations All of the codes are organized as per the chapters, each folder has the codes related to that chapter or appendix. Tk Add a title win. MIT License. Releases No releases published.

Packages 0 No packages published. Contributors 4. You signed in with another tab or window. Reload to refresh your session.



0コメント

  • 1000 / 1000