How to Dynamically Label my Buttons Based on Database Query using Tkinter

I am trying to label my button dynamically based on the database results. So in this case the button would be labeled '23'


| id | number |

| 1 | 23

from tkinter import *
import pymysql as mdb

from tkinter import ttk

def functionHolder():
    print("do nothing function holder")

root = Tk()
dbi = mdb.connect("localhost",port=3306, user="access", passwd="***", db="index_db" )
cursor = dbi.cursor()

cursor.execute("""SELECT number FROM caution_elements WHERE id = 1 """)
data = cursor.fetchone()[0]

result =str("%s " % data)

 varButt = Button(root,textvariable=data, command=functionHolder)


Button should be labeled [23] I currently get a blank button and no errors

1 answer

  • answered 2017-12-06 01:31 Nae

    A textvariable option of tkinter widgets require special variable classes such as StringVar, BooleanVar or IntVar. Their values can be accessed using get method and their values can be changed using set as in:

    import tkinter as tk
    root = tk.Tk()
    data = tk.StringVar()
    def change():
        if data.get() == "This":
    tk.Label(root, textvariable=data).pack()
    tk.Button(root, text="Change", command=change).pack()

    If you are not sure about using class variables another example without them would be:

    import tkinter as tk
    root = tk.Tk()
    data = "This"
    def change():
        global data
        if data == "This":
            data = "That"
            a_label['text'] = data
            data = "This"
    a_label = tk.Label(root, text=data)
    tk.Button(root, text="Change", command=change).pack()

    Both of the code pieces do the same thing except that when you change data's value, in the first example you're also changing text displayed by the label, and in the second example you need to explicitly update a_label's text option.

    Also note that you can use a_widget.config(option=value), a_widget.configure(option=value), a_widget['option'] = value interchangeably given that you're modifying only one option.

  • how to automatically manage the serial in a database?

    I have created an application in python which accepts values from the user and inserts them into the database . It accepts the name, address, phone numbers, etc from the application itself. But the problem is, the user can't enter the serial number into the application. Is there a way to manage the serial number automatically? Because no one remembers the serial number of the last record. So, one needs to check the serial number of the last record in the database every time before assigning the serial number for the new record.

    I also want that if any record is deleted from the middle of the database, the serial number should be adjusted automatically. So, what I want is, the serial numbers should be assigned automatically like 1 for the first record, 2 for the next, 3,4,5,6... and so on. But if the record corresponding to the serial number 5 is deleted, then the 6 should become 5.

    Thanks in advance