Java, Python, Database, Flutter, Matlap, Micorcontroller, Tutorials, Swing Framework

Hancie e-Learning Studio

Learn Java, Learn HTML, CSS, PHP, Javascript, Python Tutorials || Download program source codes || Java Project and Source code available here || All types error troubleshooting tips available here

How to populate Combobox data from MySQL database table in Python Tkinter?

 


connection.py
import mysql.connector
import sys

def Connect():
    conn=None
    try:
        conn=mysql.connector.connect(
            host='localhost',
            username='root',
            password='',
            database='level4b'
        )

    except:
        print("Error", sys.exc_info())

    finally:

        return conn

libs.py

class Libs():
    def __init__(self, cid=0,
    name=None, dob=None, email=None, password=None):
        self.cid=cid
        self.name=name
        self.dob=dob
        self.email=email
        self.password=password

    #Getter
    def getCid(self):
        return self.cid

    def getName(self):
        return self.name

    def getDob(self):
        return self.dob

    def getEmail(self):
        return self.email

    def getPassword(self):
        return self.password

    #setter
    def setCid(self, cid):
        self.cid=cid

    def setName(self, name):
        self.name=name

    def setDob(self, dob):
        self.dob=dob

    def setEmail(self, email):
        self.email=email

    def setPassword(self, password):
        self.password=password

    def __str__(self):
        return ('{},{},{},{},{}'.format(self.cid,
    self.name, self.dob, self.email, self.password))
backend.py
from Hancie.connection import Connect
import mysql.connector
import sys


def get_alldata():
    conn=None
    sql="""SELECT name FROM customers"""
    Result=None
    try:
        conn=Connect()
        cursor=conn.cursor()
        cursor.execute(sql)
        Result=cursor.fetchall()
        cursor.close()
        conn.close()


    except:
        print("Error", sys.exc_info())

    finally:
        del sql, conn
        return Result
gui.py
from tkinter import *
from tkinter import ttk

from Hancie.backend import get_alldata


class Hancie():
    def __init__(self, main):
        self.main=main
        self.main.title("Hancie e-Learning Studio")
        self.main.geometry("500x400")

        myfont=('Tahoma',16)

        searchlbl = Label(self.main, text="Name:", font=myfont)
        searchlbl.place(x=20, y=50)

        Result = get_alldata()
        comboxdata = [r for r, in Result]

        nameCombo = ttk.Combobox(self.main,values=comboxdata, font=myfont)
        nameCombo.place(x=120, y=50)


if __name__=='__main__':
    main=Tk()
    Hancie(main)
    main.mainloop()
Output

Welcome all to Hancie e-learning studio
Friends,
I have brought an e-learning platform for you from where you can teach internet, website, programming language, error troubleshooting, etc. Blogger, WordPress templates, themes are available for free on this website, which are not charged for downloading, so that you can earn money by blogging using such templates and this is not a difficult task and this You can do it now. You keep uploading new posts by us and keep taking advantage of this website. The aim of which is to spread the knowledge related to internet, career, web designing and technology to the people and contribute to the development of the country.