无法从数据库mySQL python的下拉菜单tkinter中加载数据

问题描述 投票:1回答:1

我有这个python代码

import MySQLdb
from tkinter import *

# Open database connection
db = MySQLdb.connect("localhost", "root", "", "wisata")

# prepare a cursor object using cursor() method
cursor = db.cursor()

sql = "SELECT nama_wisata FROM lokasiwisata"

try:
    # Execute the SQL command
    cursor.execute(sql)
    # Fetch all the rows in a list of lists.
    results = cursor.fetchall()
    for row in results:
        print(row[0])
except:
    print("Error: unable to fecth data")

# Options list for the dropdown
list_opt = row[0] # Create the main window
root = Tk()
# Rename the title of the window
root.title("Rekomendasi Rute Wisata")
# Set the size of the window
root.geometry("450x450")
# Set resizable FALSE
root.resizable(0, 0)
# Create a variable for the default dropdown option
selected = StringVar(root)
# Set the default drop down option
selected.set(row[0])
# Create the dropdown menu
dropdown = OptionMenu(root, selected, row[0])
# Place the dropdown menu
dropdown.place(x=45, y=10)

# Create an entry
entry = Entry(root)
entry.place(x=47, y=60)

root.mainloop()  # disconnect from server
db.close()

当我运行此代码并打印数据时。显示数据库中的所有行数据。但是,当我希望数据显示在下拉菜单中时,就不会。请帮助我在下拉菜单中显示数据库中的所有行(在一列中)

我有这个python代码从tkinter import * *#打开数据库连接db = MySQLdb.connect(“ localhost”,“ root”,“”,“ wisata”)#使用cursor()方法准备一个游标对象。 。

python mysql tkinter dropdown
1个回答
0
投票

导入MySQLdb将Tkinter导入为tk从Tkinter导入*

© www.soinside.com 2019 - 2024. All rights reserved.