有没有一种方法可以将数据从数据库输出到tkinter列表框?

时间:2019-05-06 07:13:27

标签: python-3.x tkinter

在使用python 3x和tkinter从数据库查询后,将数据打印到列表框时出现问题。

import sqlite3
import test
import MySQLdb
import sys
from tkinter import messagebox
try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk

try:
    import ttk
    py3 = False
except ImportError:
    import tkinter.ttk as ttk
    py3 = True

import ui_support


def vp_start_gui():
    '''Starting point when module is the main routine.'''
    global val, w, root
    root = tk.Tk()
    top = Toplevel1 (root)
    ui_support.init(root, top)
    root.mainloop()

w = None
def create_Toplevel1(root, *args, **kwargs):
    '''Starting point when module is imported by another program.'''
    global w, w_win, rt
    rt = root
    w = tk.Toplevel (root)
    top = Toplevel1 (w)
    ui_support.init(w, top, *args, **kwargs)
    return (w, top)

def destroy_Toplevel1():
    global w
    w.destroy()
    w = None

class Toplevel1:
            try:


                db = MySQLdb.connect(host="localhost",    # your host, usually localhost
                     user="root",         # your username
                     passwd="",  # your password
                     db="test")        # name of the data base
            except Exception:
                 messagebox.showerror("Oops!", "This is embarrassing. We are not able to connect to the DB")

                 cur = db.cursor

            def getUserByID(self):

                user_id = self.Text1.get("1.0",'end-1c' )

                try:
                    messagebox.showinfo("Request Successful!", "Please wait as the data is fetched....")
                    get_user = ("SELECT * FROM user_details WHERE user_id = ?")
                    cur.execute(get_user,[(self.user_id.get())])
                    result = cur.fetchall()
                    self.Listbox1.insert(result)
                    #issue getting data from DB inserting to Listbox

                except Exception:("Sorry", "The program is not responding")

            def getUser(self):
                try:
                    messagebox.showinfo("Please Wait!", "All the data is being fetched....")
                    cur.execute("SELECT * FROM user_details")
                    result = cur.fetchall()
                    self.Listbox1.insert = (result)              
                 #issue getting data from DB inserting to Listbox
                except Exception:("Sorry", "The program is not responding")

            db.close()            

            def __init__(self, top=None):
                    '''This class configures and populates the toplevel window.
                    top is the toplevel containing window.'''
                    _bgcolor = '#d9d9d9'  # X11 color: 'gray85'
                    _fgcolor = '#000000'  # X11 color: 'black'
                    _compcolor = '#d9d9d9' # X11 color: 'gray85'
                    _ana1color = '#d9d9d9' # X11 color: 'gray85'
                    _ana2color = '#ececec' # Closest X11 color: 'gray92'
                    font10 = "-family {Georgia} -size 14"

                    top.geometry("600x450+433+86")
                    top.title("Project App")
                    top.configure(highlightcolor="black")

                    self.Labelframe1 = tk.LabelFrame(top)
                    self.Labelframe1.place(relx=0.25, rely=0.178, relheight=0.3
                            , relwidth=0.567)
                    self.Labelframe1.configure(relief='groove')
                    self.Labelframe1.configure(width=340)

                    self.Label1 = tk.Label(self.Labelframe1)
                    self.Label1.place(relx=0.147, rely=0.222, height=27, width=69
                            , bordermode='ignore')
                    self.Label1.configure(activebackground="#f9f9f9")
                    self.Label1.configure(font="-family {Georgia} -size 14")
                    self.Label1.configure(text='''User ID''')

                    self.Button1 = tk.Button(self.Labelframe1)
                    self.Button1.place(relx=0.618, rely=0.593, height=31, width=74
                            , bordermode='ignore')
                    self.Button1.configure(activebackground="#f9f9f9")
                    self.Button1.configure(command = self.getUserByID ,text='''Submit''')


                    self.Text1 = tk.Text(self.Labelframe1)
                    self.Text1.place(relx=0.412, rely=0.222, relheight=0.178, relwidth=0.429
                            , bordermode='ignore')
                    self.Text1.configure(background="white")
                    self.Text1.configure(font="TkTextFont")
                    self.Text1.configure(selectbackground="#c4c4c4")
                    self.Text1.configure(width=146)
                    self.Text1.configure(wrap='word')

                    self.Button2 = tk.Button(self.Labelframe1)
                    self.Button2.place(relx=0.118, rely=0.593, height=31, width=84
                                       , bordermode='ignore')
                    self.Button2.configure(command = lambda: self.getUser(),text='''Show All''')

                    self.Label2 = tk.Label(top)
                    self.Label2.place(relx=0.2, rely=0.044, height=31, width=359)
                    self.Label2.configure(activebackground="#f9f9f9")
                    self.Label2.configure(text='''Twitter Swifter''')

                    self.Label3 = tk.Label(top)
                    self.Label3.place(relx=0.3, rely=0.933, height=21, width=229)
                    self.Label3.configure(activebackground="#f9f9f9")
                    self.Label3.configure(text='''Copyright Dennis''')

                    self.Listbox1 = tk.Listbox(top)
                    self.Listbox1.place(relx=0.117, rely=0.578, relheight=0.347
                            , relwidth=0.807)
                    self.Listbox1.configure(background="white")
                    self.Listbox1.configure(font="TkFixedFont")
                    self.Listbox1.configure(width=484)



if __name__ == '__main__':
    vp_start_gui()

0 个答案:

没有答案