Python sqlite3,tkinter显示多行

时间:2012-08-15 02:06:22

标签: python sqlite tkinter

我是python的新手,最近我使用Python和Sqlite3使用tkinter创建字典。 当我运行代码时,它在IDLE中返回多行,但在GUI中它只显示最后的结果。我想在GUI中显示所有信息。感谢您的任何帮助和建议。

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()

1 个答案:

答案 0 :(得分:0)

您需要更改:

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

要:

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)

新版本获取StringVar值的当前值,然后在返回的结果中附加返回的结果,其中每行返回的行都在其自己的行上。然而,您所做的只是将StringVar更新为当前成员对象,因此它只有最后一个值。

这是多行的看法:

This is what it currently looks like