Outils pour utilisateurs

Outils du site


issue162:python

Ceci est une ancienne révision du document !


Last month, a PAGE user came to both Don and myself for some help with a fairly new Python library that implements a spreadsheet like table widget written completely in Python. He was fairly new to PAGE and was having trouble getting the table to initialize properly. I thought the library has a tremendous amount of possibilities, so I thought I’d share the information with you. Just so you know, you don’t have to use PAGE to implement the sheet widget, but you do have to use Tkinter at least.

To get started, you need to install the library and, as usual, we can use Pip to do it.

$ pip install tksheet

Once you’ve done that, you might want to consider downloading the entire source code from the github repository at https://github.com/ragardner/tksheet. I suggest this since there are so many options that this library has, you might not catch some without digging into the source code. The documentation for the library is currently being written, so it’s easy to suss out how to do some of the options by having the code available.

We’ll use a modified version of the demo program to start with, which does not require PAGE. You can find the full version on the homepage of the repository.

from tksheet import Sheet

import tkinter as tk

As almost always, we want to import the important libraries, in this case, the tksheet and tkinter libraries.

Now (top right) we’ll create a class called demo. This will hold all of the code for the demo program that deals with the sheet widget. As you go through the code, you will see that many of the functions later on are simply there to allow you to customize the builtin functions that the library has.

As you already know, the init function sets up various parameters and defaults. In the next lines, the sheet object is simply an instance of Sheet (above).

Here (below) is where you load the dummy data into the sheet.

Next (next page. top right), we enable the various bindings that are already coded in the library.

The next two lines insert the frame and sheet objects into a Tkinter grid.

self.frame.grid(row=0, column=0, sticky=“nswe”) self.sheet.grid(row=0, column=0, sticky=“nswe”)

As you can tell from the commented code (bottom right), the next set of lines show ways to control various things like the theme, highlighting columns, rows and cells, etc.

The rest of the code (next page, right side) overrides the default events and bindings, and provides examples of how to handle them in your own code.

Finally, the required instantiation of the demo class object and the call to the Tkinter mainloop.

app = demo()

app.mainloop()

When you fire up the program, you should see something that looks like the image on the next page (top right).

While that wasn’t too hard at all, I believe that using PAGE makes the process much easier. I’ve created a VERY simple PAGE GUI to show how quickly and easy it is.

Create a folder to hold your project and start up PAGE within that folder. Expand the default Top level form just a bit and place two buttons near the very top of the form, one on the left and one on the right. The left one should have the text “Load CSV”, and the one on the right “Exit”. In the command attribute for the left button enter “on_btnLoad” and for the right button “on_btnExit”. (We’ve covered PAGE programming so often, this should be very obvious, but if it isn’t, look at one of my previous articles about using PAGE. My article in FCM#155 should be a good easy reference).

Next, place a frame in the form that takes up pretty much of the rest of the form. Then place a custom widget into that frame and expand it to fill the frame. (Mouse-3 (Right-click for most people)) | Widget | Fill Container). Save your project as tksheetGUI.tcl and generate your python code.

Bottom right is what your project hopefully looks like. If not, that’s ok. You get the general idea.

Now open tksheet_support.py in your IDE or editor, and let’s get to work.

PAGE only creates a single import line, since that’s all that you need to get started. Add the following lines to the import section of the code to support our needs. The line that PAGE gives us is not in bold.

import sys import platform import os

# Third party libraries from tksheet import Sheet import pandas as pd

We also need to modify the import section that PAGE provides for us. Normally, you would not need to do this, but we are going to add support for the Tkinter messagebox and the filedialog sub-systems.

import Tkinter as tk

import tkFileDialog as filedialog

import tkMessageBox as messagebox

except ImportError:

import tkinter as tk

from tkinter import messagebox

from tkinter import filedialog

Note that we actually don’t do anything with the messagebox but it’s there for your possible future development.

Now (top right) we need to add a couple of lines to the init function, that again, PAGE provides for us.

As I’m sure you are aware, this (middle right) will call the startup() and init_custom() functions before the form is shown to the user. Let’s look at the startup function first.

Yes, there is some code that isn't strictly needed, but I like to add it. Basically, we get and provide the python version, program path and program version, and print all of that to the terminal window. Then we set up some global default values for use with the tksheet library.

In the init_custom() function, we will be initializing various settings of the tksheet for our use. It took me a fair amount of time to narrow down the actual settings needed for proper demo use. There are many other settings as you saw in the earlier demo program, but, for us, these are needed. One thing to note is the theme setting (see below). There are four default themes defined by default. The first demo used the light green theme. For the PAGE version, we will use the dark blue theme.

Now (top right) we need to enable the bindings that we want so that the functionality is what you would normally expect for a spreadsheet demo. These are things like selecting a single cell, a row or column and using the arrow keys as well as allowing for “right-click” (mouse 3) context menu support.

The next few lines (bottom right) allow you to use the popup context menu. Some of them are pretty obvious, but it’s not as clear why the last 6 lines would be needed.

The last part of the init_custom function (bottom right) deals with binding the Mouse-3 button to the “rc” routine and sets extra bindings for us to be able to override the default functions within the library.

Now (next page, top right) we’ll create the four callback functions that we just defined. I must admit that I created the first three for a different demo that printed the information to a status label, but I felt that, for this very simple demo, printing to the terminal was ok.

I created a simple class to handle the actual tksheet as a custom control. See next page, bottom right.

Almost at the end now. We still have to assign the library as the custom control. PAGE supplies a line that says “Custom = tk.Frame”, but we need to replace tk.Frame with our external tkinter control library. You can add the following line and comment out the ‘Custom = tk.Frame’ line.

Custom = sheet

Finally, we need to provide the callback and support functions for the buttons that we defined when we designed the GUI (next page, top right). PAGE generated the first two functions for us as skeletons. The final function helps the import of the CSV files. You already have seen the proper way to end a PAGE application, but I’ve included it here for your convenience.

Now (next page, middle) we have the callback for the load csv file button. It’s a little bloated, but I thought it would be easier for you to follow the code this way rather than write it more elegantly.

Last but not least, here (bottom right) is the load_csv_file function. Here we are using pandas to read the CSV file and convert it into a list. That way, the tksheet library can utilize it.

Be sure to save your file.

Last but not least (again!), you need to know where a good CSV file is. For this demo, I have used the titanic3.csv file which is provided with the source code for the Pandastable Library. I’m certain it’s been released into the public domain. I copied it and put it into the development folder for quick access.

Once you run the program, it should look something like this:

I hope that you enjoyed this month’s project as much as I did creating it. This library has a lot of potential and will be great for quick display of things in table or sheet format. However, there is no code to write back to a file (that’s simple enough for you to work out) and there are no calculation functions at this point. But this is a fantastic library and something that has been needed for a very long time!

As I did once before, I’ve created a repository on github to hold the code and images from this article. You can find it at https://github.com/gregwa1953/FCM162 .

As always, until next time; stay safe, healthy, positive and creative!

issue162/python.1604233666.txt.gz · Dernière modification : 2020/11/01 13:27 de auntiee