Ceci est une ancienne révision du document !
Welcome back my friends. I hope that May finds you all healthy, wealthy and wise. Or at least healthy.
This month, I’m going to do another PAGE article, but what I present will be directly relatable to straight Python/Tkinter programming, so I think that it will be a good article for you all.
Anyway, let’s get started.
I’ll start off with a screenshot (below) of the program as it looks like in the PAGE designer. It’s really nothing but the Toplevel form, a TButton, a TLabelframe holding 12 TRadiobuttons and a TNotebook widget. I’ve gone ahead and edited the TNotebook to have 4 tabs and set a fairly long label on each. And each of the tabs has at least one label (either Tk or ttk).
There is really nothing special about the program at this point. However, in the support module I play all kinds of games with everything there.
Bottom right is shown an idea of what you can expect from the finished code.
I hope that this is already giving you some ideas of how this might be useful to you in your own programs. It’s actually very easy to do and, with the theme that I’ve created, you can customize the theme package to suit your moods. We’ll go into the theme package more next month. For now, let’s look at how to change the position of the Tabs on the TNotebook.
The TNotebook has a little known (well, probably little known to most Tkinter programmers anyway) option that allows the programmer to set the position of the Tab portion of the TNotebook widget. It’s part of the ttk Style options. Unfortunately, the Tcl man page for the TNotebook widget mentions that only the ability to set the position is available. It says nothing about what the various “legal” settings are. So, we’ll start with the actual function that does all the work (see next page, top right).
So the first thing that needs to happen is to create an object that inherits from the ttk.Style base object. After many hours of struggling to come up with a name for my version of the object, I finally settled on “style”. Pretty cool, huh? Anyway, that joke probably fell flat, so we’ll move on.
The next part of the function is probably not useful to this particular program, since all the decisions as to the position are provided by the TRadiobuttons. However, if you are going to add this function to one of your programs, you might want to keep it in, since mistakes are easy to make.
The positions list is provided to verify that the passed parameter is actually one of the “legal” positions. We check to see if it is a member of the positions list, and if so, we use the Style.configure command. The syntax is:
style.configure(TWidget name, StyleOption = Value, …)
Then, the elif catches a nonvalid setting, prints a message to the terminal and “resets” the TNotebook position setting back to the default (which is the North West corner of the widget) just in case there had been some valid changes previous to this call.
That’s the entire magic that has rarely been documented.
The next function that we will take a look at is the callback function that is called whenever a TRadiobutton is clicked. Just so you know, all the TRadiobuttons have the same callback and the values of the TRadiobuttons are set 1 to 12. This makes it easy to use an if tree. It would be just as easy to use the switch case tree, but since that is available only from 3.10 forward, I decided to stick with the if tree (see code right).
I deleted many of the elif statements since they are pretty much all the same thing with the only thing changing is the position.
You might also notice that each of the if/elif statements has two set tab statements. The first is to set the position to either “e” or “n” before the actual final position is set. This is because when I was testing the program, if I went from a position on the top or bottom of the TNotebook directly to one on the right or left, the full tab set would not show up. The same if I went from the right or left to one on the right or left. I’m fairly certain that’s a bug in the ttk code. I can understand why it might have been overlooked by the original programmers of the TNotebook widget. How often would ANYONE create a program to walk the tabs around the edges of the widget. Besides me, right?
Another thing you might have noticed is that the tabs are set to an interesting color set. I just put up a new post on my website (thedesignatedgeek.xyz) that explains the process, but I’ll go over it quickly here. I use another function to take care of this and it uses more ttk Styling “magic”. I called the function setup_base_style() (code shown top right).
Again, we create an instance of the ttk.style object and, this time, we apply a map which is used mostly when we want to deal with colors.
Basically, we assign a list of tuples to both the background and foreground colors. There are three states that we deal with here. They are selected, active, and !active (or not active). As you might suspect, the selected state is the tab that is currently selected and its page (actually a TFrame) is the one that is currently shown. The active state is triggered when the mouse cursor is hovering over that tab, and the !active state is when any of the other tabs are not in one of the other two states. So the actual selected tab is the one in the darker gray, the active tab is the one in the light gray and the !active tabs are the ones in sandybrown color.
The setup_base_style is called from the startup menu. For those who don’t remember what the startup function is or where it is being called from, this is a function that allows any variables or functions to be called before the program is actually shown to the user and the program goes into the Tkinter mainloop function.
The first few lines simply set up some general styling things (which we’ll discuss next month) and calls an imported Python module that I created and named mystyles_dark. This was an experiment I played around with back in December 2022. The idea was to create a Python based Theme for Tkinter programs. If you remember, a ttk Theme is simply a collection of styles that apply to various ttk widgets. I never got around to finishing that project, since once I got the basics of getting the styles for various widgets going, I then put the Python file on the back burner and dived head first into writing a true Tcl/Tk theme in Tcl. (That was an experience, let me tell you! Well, actually that story is going in a blog on my website.) Anyway, I digress. The Python style module has only one function, which is create_styles() which handles all of the stylings. It requires the style object (in this case sty (actually a better name than style, huh? Less to type!)). By calling it, all the widgets it has code for (most of the ttk library) is now styled to a fairly dark color. I then grab the background color from the Python styling library and assign that to a global variable (background1). Then I call the TNotebook Tab styling routine. The frames that make up the “pages” of the TNotebook, are coloured by calling the function color_notebook_pages(). I then set the background through style.configure to be the same background as the rest of the project. All the labels, since I mix Tk Labels and ttk.TLabels on the pages of the TNotebook need to be set to proper background and foreground colors, which is what the function fix_labels() does. Finally, I make sure that the Tabs positions are set to the default position and set the title for the project and center it into the screen (code shown top right).
We’ll talk about the Python styling module next month (actually I’m going to start it later this afternoon, but you won’t get to see it until June). One note on the styling module, it requires an empty file named “shared.py” to share information between the modules and to provide the temporary global images for some graphic stuff. Again, more on that next month.
LATE NOTE:
After I wrote the article, I went back and looked at the mystyles.dark module. I said that pretty much as soon as I had gotten it to its current state, I pushed it onto the back burner in order to work on the tcl theme file. When I looked with a bit of a critical eye, I noticed that the module was missing many ttk widgets and what was there was so very minimal that it was pretty much useless outside of setting background/foreground for SOME widgets. So over the past few days, I’ve modified it pretty heavily. The version that I used in the sample program for the article was 2.04. The modifications are now up to 2.05.7𝛽 . I’ve replaced the original in the repository with the latest. There are a couple of additional functions in the module beyond the create_styles() function. In order to properly style the TCombo box (at least in my mind), the dropdown portion needs to have the same background and foreground as the entry field. To do that, however, there needs to be a couple of calls that require the options to know the root or Toplevel widget.
So, there is a new function that is called add_options(toplevel) and requires the toplevel name. For use with PAGE, simply send in _top1 or whatever your toplevel name is. The other is a simple function called get_version(), which just returns the version of the module.
As I usually do, I’ve created a repository on my GitHub page at https://github.com/gregwa1953/FCM193. It will contain the source code to create this month’s project including the PAGE .tcl file, the PAGE python modules and the Python styling module (such as it is).
Until next time, as always; stay safe, healthy, positive and creative!