无法从数据库mySQL python的下拉tkinter中加载数据 [英] Can't load data in dropdown tkinter from database mySQL python
本文介绍了无法从数据库mySQL python的下拉tkinter中加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个python代码
I have this python code
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()
何时我运行此代码并打印数据。它显示数据库中的所有行数据。但是,当我希望数据显示在下拉菜单中时,就不会。
请帮助我在下拉菜单中显示数据库的所有行(在一列中)
When i run this code and print the data. It is show all row data from database. But when i want the data is show in dropdown menu, it can't. Please help me for showing all row (in one column) from database in dropdown menu
推荐答案
此是有效的:
import MySQLdb
import Tkinter as tk
from Tkinter import *
db = MySQLdb.connect("localhost", "root", "", "wisata")
cursor = db.cursor()
sql = "SELECT nama_wisata FROM lokasiwisata"
movieList = []
try:
cursor.execute(sql)
results = cursor.fetchall()
for a in results:
data = (a[0])
movieList.append(data)
print (data)
except:
print("Error: unable to fecth data")
root = Tk()
root.title("Rekomendasi Rute Wisata")
root.geometry("450x450")
root.resizable(0, 0)
selected = StringVar(root)
selected.set(movieList[0])
dropdown = apply (OptionMenu,(root, selected) + tuple(movieList))
dropdown.place(x=45, y=10)
root.mainloop()
db.close()
这篇关于无法从数据库mySQL python的下拉tkinter中加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文