Making our window look better

We've fetched the information from the database, but it comes as one long line and enclosed with (' '). It also stretches the window way to wide. Today we're going to fix all that. We'll have to rewrite most of what we've done to work with columns, so we can scroll. I t turns out you've got to put the text into a Column in Python to make it scrollable. The window we are making is not changed. so from # Create the window and down is as it was in the last session.

As soon as I've got the cursor, I execute a SQL statement. We know that were only going to have the first entity in the array (there is only one). Then we'd like to set the wrap with for the text.

mTextsFormatted = ""
for (Texts) in cur:
   
    mTexts = textwrap.wrap(Texts[0], 40)
    
    for  r in range(len(mTexts)):
        mTextsFormatted = mTextsFormatted + (mTexts[(r)])  + "\n"

For this part you will have to use the plugin textwrap, At the top of the program you'll find import textwrap that gives us that.
The first thing I do, before I make a for-loop, is to set mTextFormatted to "" (nothing). That makes the variable defined, so I can use it to add to itself. If I hadn't declared the variable, it wouldn't' exist, so I could not add to it.

First statement after the for-loop begins, is to wrap the text from Texts[0] at 40 columns, and as such create an array of it. We are relly not looking for an array, but a nicely formatted text string, with newline in them and all, so that's what we must make.
In order to do that, we read every line in the array, enter newline ath the end of it ( + "\n" ). That is also why the variable mTextFormatted is declared. Now we have an empty variable to throw line one into. After the first round of the loop, that problem is sloved for good, and we throw in line two, three....

len(mTexts) counts the number of entries in the mTexts array, so I can loop through them all, ending up with the variable mTextFormatted containing all the lines in the wrapped text and a newline ( \n ) at the appropriate place where the line should break.
The range() in the same loop counts from 0 till (in this case) the last entry in the array, so it loops through them all. You can also choose start point if it's not 0. Read more here.
 

column1 = [
    [sg.Text(f'{mTextsFormatted}')]
]

As we now have formatted the text as we want it, we define a Column, containing the formatted text.
 

sg.theme('Default1')   # Add a touch of color
    # All the stuff inside your window.
    
layout = [ [sg.Column(column1, scrollable=True,  vertical_scroll_only=True)],
              [sg.Button('OK')]]

Then we set the theme and rewrite the layout from last lesson to support column. We add the column1 from earlier, let it be scrollable, but only upwards and downwards - not left to right or right to left. The OK-button, we leave as is.

The rest of the program is as it was in last session.

We are going to learn a lot about how to format windows, texts and numbers, but for now, this'll do, I think. Little by little does it, and suddenly you're home. laugh

If you've got something like the image above, you've either done something right or done the same errors I did (but didn't notice), for that is what I got.

Have an unbelievable day - of the good kind!

 
« First -- Previous -- Next -- Last »