Using the tkinter.messagebox library

Previous entries in this series:

  1. Welcome to tkinter
  2. Questions and answers for a basic tkinter GUI
  3. Feet to Meters using tkinter and ttk

While I am mostly looking at the ttk module at present, there is a module in the tkinter library that is so useful that I want to take a break and cover it. The module is called messagebox. It is located in the tkinter library.
Continue reading Using the tkinter.messagebox library

Feet to Meters using tkinter and ttk

Previous entries in this series:

  1. Welcome to tkinter
  2. Questions and answers for a basic tkinter GUI

Previously we have made an application that doesn’t really do anything. Its purpose was to show the necessary code to create the application in a GUI on the screen. Now it’s time to create an application that actually does something.

The application that we are going to make is based on the wonderful tutorial provided by TkDocs. specifically, we are going to be recreating a first (real) example, but instead of using non-object-oriented code, we will be using python classes.
Continue reading Feet to Meters using tkinter and ttk

Welcome to tkinter

I have fallen in love with the python programming language. It has everything that I commonly use when I am programming in C/C++, but it also has the advantage of being cross-platform. As a C/C++ programmer, learning the language was relatively easy, and the joy of my programs being able to run on virtually any system without heroic efforts was truly exhilarating. The only thing that seemed to be missing was the ability to have a GUI that was available across all platforms while still avoiding those aforementioned heroic efforts.

Python already had a self-contained answer to my desire for a cross-platform GUI. When I installed python, it brought along its own GUI for the ride. While it is true that the GUI might not be as all-inclusive and robust as, say, the Windows GUI or GTK+, it seems to be just perfect for the same reasons that the python language, itself, is so wonderful.
Continue reading Welcome to tkinter

C++ programming tools: Orwell Dev-c++

If we want to program in c++, the very first thing we need to do is set up a c++ compiler and linker. It will also make it easier on us if we use an Integrated Development Environment, also known as an IDE. Fortunately for us, we can get these things in one, simple package: the Dev-c++ IDE.

Note: While I, personally, like MinGW and Dev-c++, there are many other good IDEs and programming packages (henceforth referred to as “compilers”) available. But since these are the tools that I use, I will limit my discussion to these.

Since Dev-c++ is open source, it comes in a couple of different versions. I am currently using the “Orwell” Dev-c++ version available on SourceForge. I chose it because the development and updates are readily available.

The Dev-c++ IDE comes in an executable ready to be installed on your system. It brings with it the MinGW compiler and toolset. Since I have never had any problems with its installation, I only have one piece of advice that will be useful in the future: install it to your root directory instead of “program files” or elsewhere (see following edit). If you ever want to compile extra libraries (which you probably will) or work in the command line (which is very useful sometimes) it will be much easier if Dev-c++ is installed in the root directory instead of buried deep in the drive.

Edit:
I want to clarify my recommend installation location. Dev-c++, by default, wants to install to a directory called “Dev-Cpp” in a regular Windows location like “C:\program files\Dev-Cpp”. If we change the installation location to “C:\Dev-Cpp” instead, it will be easier to add the MinGW compiler to our path parameter at a later time if needed (and we probably will want to add it to our path parameter at some point).

Now that we have the compiler and IDE installed, it’s time to see some of the different settings.

Common Dev-c++ settings

Most, if not all, the settings we need to create a program are already configured when the Dev-c++ IDE is installed. But depending on the program we are going to make, we might need to either add some settings or change some of the defaults.

MinGW is a collection of command line tools. One of the beautiful things about Dev-c++ is that it will provide the c++ compiler inside MinGW with all the information it needs to create our program. But sometimes programs need more than just the original code. Simetimes our program will need to be linked to other libraries.

If we need to link our program to a different library, here is how we do it:

We have already created our part of the program. We have the various files in a project that we want to compile and link.

dev-c++1
But during the linking process we want to link it, for instance, to the wsock32.dll.

MinGW uses a library format that is common on Unix systems (including Linux, and other *nix systems). This library system is less common on Windows, but works just fine regardless. In the Windows world we usually identify libraries by their extension. The extension is commonly either dll for a dynamically linked library or lib for a static library. In the *nix world, it is common to preface a library with the “lib” prefix and an “a” extension. So in *nix the “wsock32.dll” library would look something like “libwsock32.a”. You will note that right after the “lib” prefix the file name is the same until the extension.

Since virtually all *nix libraries use lib(name).a format, this is also the format that MinGW knows. And since it knows this format, it will assume that there is a “lib” prefix and an “a” suffix. In fact, it will only look for the (name) portion of the library using the default command line variables.

The command line option to link is the “-l” switch. You don’t even need to put quotes around it. MinGW will look at the -l and assume that the rest of the letters form the name of the library it is looking for.

Going back to our example of “wsock32.dll” this would make the MinGW command line switch look like this: -lwsock32

Now that we know the format, it is easy to send the command line argument to the linker.

First we click on the “Project” menu and select “Project Options …”:

dev-c++2

That will open a Project Options dialog box:

dev-c++3

From the list of tabs across the top of the dialog window, select the “Perimeters” tab. The Perimeters tab is where you can add the compiler and linker options that aren’t available elsewhere. For instance, this is where you can type in the -lwsock32 line to cause MinGW to give you access to Windows sockets.

Before we leave this area completely, lets look at another very useful tab. Right next to the “Perimeters” tab is one called “Compiler”. This tab has six (6) sub-tabs that store many different options for the command line. If it is one of these switches (or options) that you need to turn off or on, it can be done there instead of adding it manually in the boxes under the “Perimeters” tab.

dev-c++4

Supplemental:

Since this essay was mostly demonstrating how to link to a “Windows” library like wsock32.dll, it seems to be a good place to explain that you MinGW isn’t actually linking to wsock32.dll with the -lwsock32 command line switch. If you try to link directly to the wsock32.dll file, you will get an error. Instead, what MinGW is doing is actually linking to a file called “libwsock32.a” like we discussed above.

But that begs the question, what the heck is libwsock32.a?

If you look around in the directories (folders) that are a part of MinGW, you will find several collections of libraries. One of those collections carries libraries that stand in for their Windows counterparts.

The folder they are stored in on my computer is:

C:\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib32

There are many different libraries stored in the directory other than just the Windows tie-ins. But now that we know how *nix and MinGW name their libraries, we can begin to make sense of all the different libraries stored there as well as which ones are sometimes needed on a Windows machine.

The (long) listing of the directory on my computer is below the fold:

Continue reading C++ programming tools: Orwell Dev-c++

Simple multimap functions in C++

Multimaps are something that I have found to be extremely valuable. Each map object consists of two different elements: a key, and a value. The multimap uses the key to put he object into order. Once the object is placed, the value can be found by searching for the corresponding key.

A multimap stores a “pair”. Pairs can easily be created using other members of the C++ standard library. This functionality can be added to your program by the following include:

#include <utility>
using std::make_pair;

As usual, you could always use different syntax. You could use either preface every call to make_pair with “std::”, or you could specify that you are using the entire std namespace like this:

using namespace std;

For clarity, I will use the first example.

All it takes to use a multimap is define the multimap, create a pair to put in it, put the pair in the multimap, and read the map out as needed.

Here is a very simple example of using a multimap. Hopefully the code will be self-documenting:

#include <iostream>
using std::cout; using std::endl;

#include <map>
using std::multimap;

#include <string>
using std::string;

#include <utility>
using std::pair; using std::make_pair;


int main(int argc, char** argv)
{
	multimap<int, string> myMultimap;		// sort by integers
	multimap<string, string> myStringMap;	// sort by strings
	
	pair<int, string> myPair;				// for clarity
	
	// The integer determines what the order will be
	
	// Start by using a "pair" for clarity.
	myPair = make_pair(7, "multimap");
	myMultimap.insert(myPair);
	myPair = make_pair(4, "sample");
	myMultimap.insert(myPair);
	myPair = make_pair(1, "This");
	myMultimap.insert(myPair);
	
	// then just enter them easily.
	
	myMultimap.insert(make_pair(6, "a"));
	myMultimap.insert(make_pair(3, "a"));
	myMultimap.insert(make_pair(2, "is"));
	myMultimap.insert(make_pair(5, "of"));
	
	// create an iterator and read out the multimap
	
	multimap<int, string>::iterator myIterator;
	for(myIterator = myMultimap.begin();
				myIterator != myMultimap.end();
				++myIterator)
	{
		cout << myIterator->second << " ";	
	}
	
	cout << endl;
	
	// The first string of the pair will be used for order.
	// They will be alphabatized.
	
	myStringMap.insert(make_pair("dare to be different", "example"));
	myStringMap.insert(make_pair("about", "This"));
	myStringMap.insert(make_pair("clean", "an"));
	myStringMap.insert(make_pair("grown", "strings."));
	myStringMap.insert(make_pair("grape", "sorting"));
	myStringMap.insert(make_pair("bad", "is"));
	myStringMap.insert(make_pair("everybody loves programming", "of"));
	myStringMap.insert(make_pair("green", "by"));
	
	// create a second iterator and read out the multimap
	
	multimap<string, string>::iterator stringIterator;
	for(stringIterator = myStringMap.begin();
				stringIterator != myStringMap.end();
				++stringIterator)
	{
		cout << stringIterator->second << " ";	
	}
	
	cout << endl;
	
	return 0;
}