简体   繁体   中英

Python sqlite3, tkinter display multible rows

I am new to python and recently i make dictionary using Python and Sqlite3 with tkinter. When I run the code it returns multiple line in IDLE but in the GUI its only display the last result. I would like to display all the information in the GUI. Thanks you for any help and suggestion.

import tkinter
import sqlite3
class Dictionary:
def __init__(self,master =None):
    self.main_window = tkinter.Tk()
    self.main_window.title("Lai Mirang Dictionary")
    self.main_window.minsize( 600,400)
    self.main_window.configure(background = 'paleturquoise')
    self.frame1 = tkinter.Frame()
    self.frame2= tkinter.Frame(bg = 'red')
    self.frame3 =tkinter.Text( foreground = 'green')
    self.frame4 = tkinter.Text()


    self.lai_label = tkinter.Label(self.frame1,anchor ='nw',font = 'Times:12', text = 'Lai',width =25,borderwidth ='3',fg='blue')
    self.mirang_label = tkinter.Label(self.frame1,anchor ='ne',font = 'Times:12', text = 'Mirang',borderwidth ='3',fg ='blue')

    self.lai_label.pack(side='left')
    self.mirang_label.pack(side = 'left')

    self.kawl_buttom= tkinter.Button(self.frame2 ,fg = 'red',font = 'Times:12',text ='Kawl',command=self.dic,borderwidth ='3',)

    self.lai_entry = tkinter.Entry(self.frame2, width= 28, borderwidth ='3',)
    self.lai_entry.focus_force()
    self.lai_entry.bind('<Return>', self.dic,)
    self.lai_entry.configure(background = 'khaki')


    self.lai_entry.pack(side='left')
    self.kawl_buttom.pack(side = 'left')

    self.value = tkinter.StringVar()
    self.mirang_label= tkinter.Label(self.frame3,font = 'Times:12',fg = 'blue',textvariable = self.value,justify = 'left',wraplength ='260',width = 30, height = 15,anchor = 'nw')
    self.mirang_label.pack()
    self.mirang_label.configure(background = 'seashell')


    self.copyright_label = tkinter.Label(self.frame4,anchor ='nw',font = 'Times:12:bold',text = "copyright @ cchristoe@gmail.com",width = 30,borderwidth = 3, fg = 'purple',)
    self.copyright_label.pack()

    self.frame1.pack()
    self.frame2.pack()
    self.frame3.pack()
    self.frame4.pack()


    tkinter.mainloop()
    self.main_window.quit()

def dic(self, event = None):
    conn = sqlite3.connect('C:/users/christoe/documents/sqlite/laimirangdictionary.sqlite')
    c = conn.cursor()
    kawl = self.lai_entry.get()
    kawl = kawl + '%'
    c.execute("SELECT * FROM laimirang WHERE lai LIKE ?", (kawl,))
    c.fetchall      
    for member in c:
        out = (member)
        self.value.set(out)
        print(out, )


dic=Dictionary()

You need to change:

 c.execute("SELECT * FROM laimirang WHERE lai LIKE ?", (kawl,))
 c.fetchall      
 for member in c:
     out = (member)
     self.value.set(out)
     print(out, )

To:

for member in c.execute("SELECT * FROM laimirang WHERE lai LIKE ?", (kawl,)):
    current_text = self.value.get()
    new_text = current_text +( "\n" if current_text else "") +" ".join(member)
    self.value.set(new_text)

The new version gets the current value of the StringVar value and then appends the results returned with a space inbetween for each row returned with each row on its own line. What you were doing however involved just updating the StringVar to be the current member object, and therefore it only ever had the last values.

This is how it looks with more than one line:

这是目前的样子

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM