蟒蛇。 mysql.connector。缺少从数据库中显示列表框中的数据

时间:2015-10-23 17:44:56

标签: python mysql tkinter listbox connection

我在tkinter中没有在listbox中显示数据时遇到问题。控制台不会显示任何错误。在我看来,与数据库的第二次连接是一个问题。第一个连接在ConfigurationPage中,第二个连接在DeletePage中。在HomePage中我可以向数据库添加新行,文本框显示添加结果,但在deletePage中列表框有问题。我希望你知道什么是不正确的。为了方便我放置应用程序工作的一些截图

ConfigurationPage

enter image description here

主页 HomePage

删除页面 enter image description here

代码:

__author__ = 'Goldsmitd'
import mysql.connector
import tkinter as tk
from tkinter import ttk


class AdressBookApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.container = tk.Frame(self)
        self.container.pack(side='top')
#creating menu
        menubar = tk.Menu(self.container, bg='grey')
        filemenu = tk.Menu(menubar, tearoff=0)
        filemenu.add_command(label="Save settings")
        filemenu.add_separator()
        filemenu.add_command(label="Exit", command=quit)
        menubar.add_cascade(label="File", menu=filemenu)
        optmenu = tk.Menu(menubar, tearoff=0)
        optmenu.add_command(label='Configuration',
                            command=lambda: self.show_frame(ConfigurationPage))
        menubar.add_cascade(label='Options', menu=optmenu)

        tk.Tk.config(self, menu=menubar)



#creating windows
        self.frames ={}
        for F in (HomePage, ConfigurationPage, DeletePage):
            frame = F(self.container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky='nsew')
#displaying start page
        self.show_frame(ConfigurationPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()


class ConfigurationPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        self.userL = ttk.Label(self, text='user name:')
        self.passL = ttk.Label(self, text='password:')
        self.hostL = ttk.Label(self, text='host:')
        self.dataL = ttk.Label(self, text='databse:')
        self.userL.grid(row=0, column=0, pady=5, padx=5)
        self.passL.grid(row=1, column=0, pady=5, padx=5)
        self.hostL.grid(row=2, column=0, pady=5, padx=5)
        self.dataL.grid(row=3, column=0, pady=5, padx=5)

        self.userE = ttk.Entry(self)
        self.passE = ttk.Entry(self)
        self.hostE = ttk.Entry(self)
        self.dataE = ttk.Entry(self)
        self.userE.grid(row=0, column=1, pady=5, padx=5)
        self.passE.grid(row=1, column=1, pady=5, padx=5)
        self.hostE.grid(row=2, column=1, pady=5, padx=5)
        self.dataE.grid(row=3, column=1, pady=5, padx=5)

        self.userE.insert(0, 'root')
        self.passE.insert(0, '')
        self.hostE.insert(0, 'localhost')
        self.dataE.insert(0, 'people')

#default connection to database
        self.connect_base('root','','localhost','people')
#CREATING BUTTONS
        self.connB = ttk.Button(self, text='connect',
                                command=lambda: self.connect_base(self.userE.get(),
                                                                  self.passE.get(),
                                                                  self.hostE.get(),
                                                                  self.dataE.get()))
        self.cleaB = ttk.Button(self, text='clear',
                                command=self.clear_click)
        self.closB = ttk.Button(self, text='close',
                                command=lambda: controller.show_frame(HomePage))
        self.connB.grid(row=4, column=0, pady=5, padx=5)
        self.cleaB.grid(row=4, column=1, pady=5, padx=5)
        self.closB.grid(row=4, column=2, pady=5, padx=5)

    def connect_base(self,inUser,inPass,inHost,inData):
        self.cnx = mysql.connector.connect(user=inUser, password=inPass, host=inHost, database=inData)

        self.cursor = self.cnx.cursor()


    def clear_click(self):
        self.userE.delete(0,'end')
        self.passE.delete(0,'end')
        self.hostE.delete(0,'end')
        self.dataE.delete(0,'end')



class HomePage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

#Eempty label
        self.emptyL = ttk.Label(self)
        self.emptyL.grid(column=0, row=0)

# creating widgets

        self.gnameL = ttk.Label(self, text='given name:')
        self.gnameE = ttk.Entry(self, width=30)
        self.gnameL.grid(column=0, row=1, pady=5, padx=5)
        self.gnameE.grid(column=1, row=1, pady=5, padx=5, columnspan=2)

        self.snameL = ttk.Label(self, text='surname:')
        self.snameE = ttk.Entry(self, width=30)
        self.snameL.grid(column=0, row=2, pady=5, padx=5)
        self.snameE.grid(column=1, row=2, pady=5, padx=5, columnspan=2)

        self.adressL = ttk.Label(self, text='adress:')
        self.adressE = ttk.Entry(self, width=30)
        self.adressL.grid(column=0, row=3, pady=5, padx=5)
        self.adressE.grid(column=1, row=3, pady=5, padx=5, columnspan=2)

        self.mobileL = ttk.Label(self, text='mobile:')
        self.mobileE = ttk.Entry(self, width=30)
        self.mobileL.grid(column=0, row=4, pady=5, padx=5)
        self.mobileE.grid(column=1, row=4, pady=5, padx=5, columnspan=2)

        self.emailL = ttk.Label(self, text='email:')
        self.emailE = ttk.Entry(self, width=30)
        self.emailL.grid(column=0, row=5, pady=5, padx=5)
        self.emailE.grid(column=1, row=5, pady=5, padx=5, columnspan=2)

#creating BUTTONS

        self.addB = ttk.Button(self, text='add',
                               command=lambda: self.add_click(controller.frames[ConfigurationPage],
                                                              self.gnameE.get(),
                                                              self.snameE.get(),
                                                              self.adressE.get(),
                                                              self.mobileE.get(),
                                                              self.emailE.get()))
        self.deleteB = ttk.Button(self, text='delete',
                                  command=lambda: app.show_frame(DeletePage))
        self.clearB = ttk.Button(self, text='clear',
                                 command=self.clear_click)
        self.addB.grid(column=0, row=6, padx=5, pady=5)
        self.deleteB.grid(column=1, row=6, padx=5, pady=5)
        self.clearB.grid(column=2,row=6, padx=5, pady=5, sticky='w')

#creating OUTPUT

        self.o1T = tk.Text(self, width=50, height=10,padx=5,pady=5, relief='ridge')
        self.o1T.grid(column=4, row=1,columnspan=3, rowspan = 5)
#creating commands

    def add_click(self, vessel, name1, name2, name3, name4, name5):
        vessel.cursor.execute('ALTER TABLE book MODIFY `id` INT(11) NOT NULL AUTO_INCREMENT')
        vessel.cursor.execute("INSERT INTO book (gname, surname, adress, mobile, email) VALUES ('%s', '%s', '%s', '%s', '%s')"\
                                     % (name1, name2, name3, name4, name5))
        vessel.cnx.commit()

        vessel.cursor.execute('SELECT id, gname, surname, adress, mobile, email FROM book')

        for (id, gname, surname, adress, mobile, email) in vessel.cursor:

            self.o1T.insert(tk.INSERT, '\n' + '%d, %s, %s, %s, %s, %s' %(id, gname, surname, adress, mobile, email))
            self.o1T.tag_configure("center", justify='center')
            self.o1T.tag_add("center", 1.0, "end")

        vessel.cursor.close()
        vessel.cnx.close()

    def clear_click(self):
        self.gnameE.delete(0,'end')
        self.snameE.delete(0,'end')
        self.adressE.delete(0,'end')
        self.mobileE.delete(0,'end')
        self.emailE.delete(0,'end')



class DeletePage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

#LISTBOX
        self.lix = tk.Listbox(self, width=100)
        self.lix.grid(rowspan=6)
        for (id, gname, surname, adress, mobile, email) in controller.frames[ConfigurationPage].cursor:
            self.lix.insert('end', ('%d  ||  %s  ||  %s  ||  %s  ||  %s  ||  %s') % (id, gname, surname, adress, mobile, email))

#CREATING BUTTONS
        self.okB = ttk.Button(self,text='OK',
                             command=lambda controller=controller:self.ok_click(controller.frames[ConfigurationPage]))
        self.closeB = ttk.Button(self, text='close',
                                command=lambda controller=controller: self.cl_click(controller))
        self.okB.grid(row=1, column=1)
        self.closeB.grid(row=2, column=1)
#deleting seleceted row
    def ok_click(self,vessel):
        self.value = self.lix.get(self.lix.curselection()) #getting strings from selected line in istbox
        self.valist = self.value.split()  # converting it to list of words
        self.vind = self.valist[0]    # getting index
        vessel.cursor.execute('DELETE FROM book WHERE id=%s' % (self.vind))
        vessel.cnx.commit()
        self.lix.delete('anchor')
#closing deletePage
    def cl_click(self,vessel):
        vessel.frames[ConfigurationPage].cursor.close()
        vessel.frames[ConfigurationPage].cnx.close()
        vessel.show_frame(HomePage)




app = AdressBookApp()
app.mainloop()

0 个答案:

没有答案