Dropdown in Python Tkinter with data from SQL Server [closed]

1mrurvl1  于 2023-04-04  发布在  Python
关注(0)|答案(1)|浏览(113)

Closed. This question needs to be more focused . It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post .

Closed yesterday.
Improve this question

How to make a dropdown in Python Tkinter, where the dropdown items are taken from SQL Server

I was looking for to create a dropdown in Python Tkinter and it's data to be taken from SQL. I tried searching for it on the internet but didn't find what I needed. It would be really nice if someone would help me with this.

yacmzcpb

yacmzcpb1#

If you have a sqlite3 database with the options in a column "select" and a table e.g. entries, you can do it like here:

import sqlite3
import tkinter as tk

# Create a sqlite3 database with a table 'entries' and column select who have three text values
# [('First selection',), ('Second slection',), ('Third selection',)]

try:
    conn = sqlite3.connect('DropBox_DB.db')
    print("SQLITE3 Version", sqlite3.sqlite_version)

except Error as e:
    print(e)
       
cur = conn.cursor()
cur.execute("SELECT * FROM entries;")
entries = cur.fetchall()

master = tk.Tk()
master.geometry("300x100")  
master.title("Dropdown Example")  

variable = tk.StringVar(master)
variable.set(select[0]) 

w = tk.OptionMenu(master, variable, *[x[0] for x in entries])
w.grid()

master.mainloop()

Output:

相关问题