Outils pour utilisateurs

Outils du site


issue179:python

Ceci est une ancienne révision du document !


As most of you know, I work fairly closely with Don Rozenberg on his free GUI designer for Python programs called PAGE. Just the other day, I received the following message from SourceForge: “Congratulations! PAGE has just been recognized with the following awards by SourceForge: Community Leader Community Choice Open Source Excellence SourceForge Favorite These honors are awarded only to select projects that have reached significant milestones in terms of downloads and user engagement from the SourceForge community. This is a big achievement, as your project has qualified for these awards out of over 500,000 open source projects on SourceForge. SourceForge sees nearly 30 million users per month looking for, and developing, open source software. These award badges will now appear on your project page, and the award assets can be found in your project admin section.”

To celebrate this accomplishment, I decided to share some of the tricks and tips I’ve learned about using Tkinter to make easy GUI front ends for Python programs using PAGE. The neat thing about these tips is that they don’t require any special coding. It’s all done by setting the appropriate attributes. The first tip concerns enhancing the Tk Checkbutton. We all know what a “regular” checkbutton looks like. While this is an easy widget to use to represent a many-of-many set of options, this is pretty much an old style visual widget. But what about this? This is the image of a customized Checkbutton in the off state, and here is the same Checkbutton in the on state. It’s really easy to create a Checkbutton that looks and works like this using Tkinter and especially in PAGE. First, you need to have the two graphic images you want to use to represent the selected and unselected states. I went to my standard goto website for graphics, openclipart.org. I did a simple search for “switch” and this was one of the options. https://openclipart.org/detail/203164/ui-toggle-button I chose the small .png version, downloaded it and then used Gimp to cut each one out and saved them as separate .png files. I copied them into an images folder inside my project.

Next, I simply placed a standard Tk Checkbutton on the Toplevel form, next to a few other Checkbuttons and I set the attributes using the PAGE Attribute Editor (see table, previous page, bottom right). When the program is run, the Custom checkbutton will act as a toggle switch. There is one other trick that can be used with the Checkbutton, and no images are needed. It turns the Checkbutton into a “sticky” button. When it is unselected, it looks very much like a standard button. When it is selected (clicked), the button stays depressed until it is clicked again. Here is the “unselected” state. And this is the selected state. You can pick to have a color in the unselected state or not. I chose to use coral for the unselected and limegreen for the selected color. Just like the Checkbutton, you will need to set the indicator to No (see table below). The next trick is to modify the standard Tk Radiobutton to provide a custom indicator dot. As with the Checkbutton trick, you will need two graphics. One for the indicator when it is not selected and one for when it is selected.

As I’ve stated many times in the past, I am not an artist in any way, shape or form. I threw together the following images to help show the process. First the unselected image. It’s simply a circle. And the selected image, which is the circle above with a black dot in the middle. Just like we did with the Checkbuttons, we set the attributes as follows (top right). The only thing that is a downside for this trick is that (as you can see in the image below) the Radiobutton goes to a sunken relief. As far as I can tell, there’s nothing that we can do about that (but you know I won’t give up trying). So here is what the full demo project looks like (next page, top right).

As to the code, it’s really very simple. def startup(): _w1.Checkbutton1.invoke() _w1.Radiobutton1.invoke() _w1.Radiobutton3.invoke() The startup function calls the .invoke method of the first Checkbutton and the first and third Radiobuttons. The invoke method makes the program think that there was a mouse-click on the widget, that then calls the associated command callback function. I wanted to show the first widget of each group to be selected on start up. def on_chkbtnClick(*args): status_list = ['Off', 'ON'] cb1 = _w1.che47.get() cb2 = _w1.che48.get() cb3 = _w1.che49.get() cb4 = _w1.che50.get() _w1.CheckDisplay.set( f'{status_list[cb1]} - {status_list[cb2]} - {status_list[cb3]} - {status_list[cb4]}' )

The callback for the Checkbuttons is really simple. The first thing that gets done is to create a very simple list with the text values ‘Off’ and ‘On’ which will be shown as part of a string that will be sent to the display label. Next each of the variables for the Checkbuttons show 0 for unselected and 1 for selected. Those values work nicely as a “pointer” into the list for the display string. Finally we create the string and send it to the label to be displayed. The next two callbacks are for when the Radibutton groups are clicked. The first two (top two) Radiobuttons on the form are handled by the top function and the bottom two custom Radiobuttons are handled by the second function. def on_rbtnClick1(*args): which1 = _w1.selectedButton.get() _w1.RadioDisplay.set(f'Normal RB {which1} Selected') def on_rbtnClick2(*args): which2 = _w1.selectedButton2.get() _w1.RadioDisplay.set(f'Special Rb {which2} Selected')

That’s all I have time for this month. Remember these tips are for “standard” Tk widgets. Yes, there are ttk widgets that can have styles and themes applied to them. The problem with styles and themes are: • Styles and themes are poorly documented • Styles and themes require special programming in Tcl/Tk • Much of the documentation on styles and themes is incomplete • AND (from what I’ve been told) many things are downright wrong! The PAGE source (version 7.3) and the Python code are all available on my repository. You can find it at https://github.com/gregwa1953/FCM-179 . Until next time, as always; stay safe, healthy, positive and creative!

issue179/python.1648457670.txt.gz · Dernière modification : 2022/03/28 10:54 de d52fr