简体   繁体   English

Python sqlite3,tkinter显示多行

[英]Python sqlite3, tkinter display multible rows

I am new to python and recently i make dictionary using Python and Sqlite3 with tkinter. 我是python的新手,最近我使用带tkinter的Python和Sqlite3制作字典。 When I run the code it returns multiple line in IDLE but in the GUI its only display the last result. 当我运行代码时,它在IDLE中返回多行,但在GUI中它仅显示最后一个结果。 I would like to display all the information in the GUI. 我想在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. 新版本获取StringVar值的当前值,然后为返回的每一行添加返回的结果,并在每行之间添加一个空格。 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. 但是,您所做的只是将StringVar更新为当前成员对象,因此它只有最后一个值。

This is how it looks with more than one line: 这是多行显示的样子:

这是目前的样子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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