In this Python TKinter article we want to learn How to Connect Python TKinter with MySQL, you will learn that how you can use MySQLdb or MysqlClient, a third party library for database programming in Python.
What is MySQLdb ?
MySQLdb is an interface to the popular MySQL database server that provides the Python database API. originally MySQLdb was for Python 2.7, but there another version of MySQLdb that is called MysqlClient, and it supports Python 3.0, at the writing of this article MysqlClient supports Python 3.0 up to 3.8, you can check their Documentation for more updates.
Learn More on Python
- TKinter Tutorial For Beginners
- PyQt5 Tutorial For Beginners
- Python MySQL Database
- Python SQLite Database
Installation
You can simply use pip for the installation.
1 |
pip install mysqlclient |
Also you need to install Wamp Server, after installation of the Wamp Server, you need to open Wamp Server and create a database, iam going to call tkdb.
OK now this is the complete code for How to Connect Python TKinter with MySQL.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import tkinter as tk from tkinter import ttk import MySQLdb as mdb class Window(tk.Tk): def __init__(self): super(Window, self).__init__() self.title("Geekscoders - DB Connection") self.minsize(300,250) self.iconphoto(False, tk.PhotoImage(file='geekscoders.png')) self.label_frame = ttk.LabelFrame(self, text = "Database Connection") self.label_frame.grid(column = 0, row =0, padx = 20, pady = 20) self.create_widgets() def create_widgets(self): button = ttk.Button(self.label_frame, text = "Connection Status", command = self.db_connect) button.grid(column = 0, row = 0) self.label = ttk.Label(self.label_frame, text = "") self.label.grid(column = 0, row = 1) def db_connect(self): try: db = mdb.connect('localhost', 'root', '', 'tkdb') self.label.configure(text = "Connected Successfully") except mdb.Error as e: self.label.configure(text = "Not Successfully Connected") window = Window() window.mainloop() |
At the top we have create our main class that extends from tkinter.Tk, and if you want to add title than you can use this code.
1 |
self.title("Geekscoders - DB Connection") |
This is used for setting the min size of the window.
1 |
self.minsize(300,250) |
In here we want to add icon for our window. make sure that you have already added an icon in your working directory.
1 |
self.iconphoto(False, tk.PhotoImage(file='geekscoders.png')) |
This is our LabelFrame, so a labelframe is a simple container widget. Its primary purpose is to act as a spacer or container for complex window layouts.
1 2 |
self.label_frame = ttk.LabelFrame(self, text = "Database Connection") self.label_frame.grid(column = 0, row =0, padx = 20, pady = 20) |
OK in here we have created a button, and you can see that we have connected the command button with db_connect() method, so this is the method that we want to our Mysql Connection with TKinter.
1 2 |
button = ttk.Button(self.label_frame, text = "Connection Status", command = self.db_connect) button.grid(column = 0, row = 0) |
You can use MySQLdb.connect() for connection to mysql database, you need to pass the hostname,username,password and database name. in our case for the hostname we use localhost, for username we use root, for password we leave it blank because we don’t have any password and for database name we can give our db name.
1 |
db = mdb.connect('localhost', 'root', '', 'tkdb') |
Run the complete code and this is the result.