Configuring the Python Database Project
For this project, we'll have a form with two tabs. The main tab can be used to scroll back and forward through all the records in the database. The second tab can be used to add new records.
Start a new project in Python, then. Call it anything you like. We'll call ours DatabaseProject. Add two Python files to your project. Call them db_main and db_config. Sensitive information like usernames and passwords can go in a separate config file rather than hard-coding them into the main program.
This project will require a lot of import statements, so it's best we get that out of the way first. We're going to use Tkinter for our user interface, as we've done previously. Add these three lines to the top of you db_main file:
import tkinter as tk
from tkinter import messagebox
from tkinter import filedialog
To get tabs, called Notebooks in Tkinter, you need to use a part of the Tkinter library called ttk. This adds a few more widgets to the library, things like Tabs, ComboBoxes, Progress Bars and Treeviews. Add this line then:
from tkinter import ttk
We also need two external libraries: Pillow and PyMySql. Pillow is a third-party library that is meant to improve the inbuilt PIL one, which hasn't been update for some time. (PIL is short for Python Image Library. You used this library in a previous section.) We need an image library to deal with the photos from our database. PyMySql will be used to deal with the database operations.
To add these two libraries, click on File > Settings in PyCharm. From the left-hand side of the dialog box that appears, select Project: DatabaseProject > Project Interpreter:
Click the green plus symbol on the right of the Settings dialog box, circled in red above. You'll see the Available Packages screen appear. Type in Pillow, and select the top item, the one that has Alex Clark as the Fork Author:
Click the button for Install Package.
Still on the Available Packages screen, delete Pillow from the search box and type PyMySql. Highlight the top search result, the one with Inada Naoki as the Author:
Install this package was well. Close down the Available Packages screen once they have both installed successfully. You will be returned to the Settings screen, which should look like this:
Click OK to close the Settings dialog box and return to PyCharm.
Add the following two import statements to your code:
from PIL import ImageTk, Image
We'll need two libraries to deal with file manipulation later in the project. Add these two:
The os library deals with Operating System stuff, while the shutil one is a shell utility. We'll need it to move an image file to the correct folder.
Finally, we can import our db_config file:
Your code should look like this (we haven't used any of these libraries yet, so they'll all be greyed out):
We can create a form like we did before:
form = tk.Tk()
To specify the size of the form, you can use the geometry method. In between the round brackets of geometry, you specify a width and height:
Notice that the size goes in quote marks and that there's an 'x' between the two. Add the line to your code. Add the mainloop at the end and your code should be this:
form = tk.Tk()
form.title("Tkinter Database Form")
Run the program to see a blank Tkinter form with a title at the top.
In the next lesson, we'll add some tabs to this form.
TKinter Tabs >
< Python Course Menu