简体   繁体   English

如何将数据库中的值插入到我的条目中? Tkinter

[英]How to insert values from my database to my entries? Tkinter

I don't know what I'm doing wrong when I'm trying to insert some values from my database to my entries.当我尝试将一些值从我的数据库插入到我的条目时,我不知道我做错了什么。 I have a main interface with the same code working well, but I've created a secondary window and tried to make work almost the same code but it's not working.我有一个使用相同代码的主界面运行良好,但我创建了一个辅助 window 并试图使几乎相同的代码工作,但它不工作。 It gives me this error:它给了我这个错误:

sqlite3.OperationalError: incomplete input sqlite3.OperationalError:输入不完整

This is my code:这是我的代码:

myid = StringVar()
myname = StringVar()
myowner = StringVar()
mycel = StringVar()

id_entry = Entry(windows2, textvariable=myid)
name_entry = Entry(windows2, textvariable=myname)
owner_id = Entry(windows2, textvariable=myowner)
cel_id = Entry(windows2, textvariable=mycel)

btn_search = Button(windows2, text='SEARCH', command=lambda: search())
btn_search.place(x=930, y=12, width='60', height='26')

def search():

    conn = sqlite3.connect('db1.db')
    c = conn.cursor()
    c.execute("SELECT * FROM regis WHERE idcard=" + myid.get())

    theuser = c.fetchall()

    for user in theuser:
        myid.set(user[0])
        myname.set(user[1])
        myowner.set(user[8])
        mycel.set(user[9])

    conn.commit()

    if theuser:
       messagebox.showinfo(title='ID Found', message='User found')

    else:
       messagebox.showerror(tittle=None, message='Wrong ID')

Explanation:解释:

What you should be doing is adding a master argument to ALL tkinter widgets.您应该做的是向所有 tkinter 小部件添加master参数。 When we create Tk() , it creates a tcl interpreter.当我们创建Tk()时,它会创建一个tcl解释器。 Each tcl interpreter has its own memory for its widgets.每个tcl解释器都有自己的 memory 用于它的小部件。 So when you say StringVar() , it doesn't know which tcl interpreter to belong to, so it implicitly uses the first created Tk() .所以当你说StringVar()时,它不知道属于哪个tcl解释器,所以它隐式使用第一个创建的Tk() But the first created Tk() might not hold the widget you are associating your StringVar to(with textvariable ), so it will not have its value stored.但是第一个创建的Tk()可能不包含您将StringVar关联到的小部件(与textvariable ),因此它不会存储其值。

Solution:解决方案:

What you can do is, either explicitly mention which instance of Tk() to belong to, or use Toplevel() for the creation of child windows(like mention by acw1668).您可以做的是,明确提及属于哪个Tk()实例,或者使用Toplevel()来创建子窗口(如 acw1668 提到的)。

StringVar(master=newwindow) # Same with other tkinter variables and all widgets too

or或者

newwindow = Toplevel() # Recommended because tkinter was made to work like this?

Also make sure to sanitize your inputs so it wont be prone to SQL injections:还要确保清理您的输入,这样它就不会容易出现 SQL 注入:

c.execute("SELECT * FROM regis WHERE idcard=?",(myid.get(),))

Though I would never use StringVar for entry widgets as long as I have to use trace on them.尽管只要我必须对它们使用trace ,我就永远不会将StringVar用于条目小部件。 They have their own get() method which works the same way.他们有自己的get()方法,其工作方式相同。

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

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