【问题标题】:Why Aren't My Values Showing Up In My Sqlite Database When Imported From Tkinter In Python 3在 Python 3 中从 Tkinter 导入时,为什么我的值没有出现在我的 Sqlite 数据库中
【发布时间】:2020-06-09 20:15:12
【问题描述】:

它没有给出任何错误只是没有出现如果它看起来有点波涛汹涌很抱歉感到沮丧但实际上一切正常都没有错误它甚至进入露营者ID但没有名字有人请帮助,谢谢,不要记下这个尽管大多数人实际上并没有阅读代码 [已修复]

import tkinter
from tkinter import *
import random
from random import randint
import sqlite3
from sqlite3 import *
#creating screen
page = tkinter.Tk()
#naming screen
page.title('Camp Sign-up')
#setting screen size
page.geometry("1000x500")
#creating text
Title = tkinter.Label(page, text='Registration', font=('arial') ).pack()

FullName = tkinter.Label(page, text='Full Name:', font=('arial')).place(x=110, y=50)
full = StringVar()
FullNameValue = Entry(page, textvar=full, width=25,).place(x=200, y=53)



CamperID = random.randint(100000, 1000000)

#Database



conn = sqlite3.connect('YouthCamp.db')
c = conn.cursor()
def createtable():
    c.execute("""CREATE TABLE Registration (
        CampID INT,
        full TEXT,
        )""")

try:
    createtable()
except:
    sqlite3.OperationalError

#button
def buttonclick():
    #heres where you lnk where you want your info to go
    FullNameValu= full.get()

    conn = sqlite3.connect('YouthCamp.db')
    c = conn.cursor()
    c.execute("INSERT INTO Registration(CampID, full)VALUES(?, ?)",(CamperID, FullNameValu))
    conn.commit()


#button
work = Button(page, text='Submit', width=10, height=2, bg='lightgrey', 
command=buttonclick()).place(x=475, y=450)

#heres where you execute your close files commands
conn.commit()
c.close()
conn.close()
#ending code never put this above anything
page.mainloop()

【问题讨论】:

    标签: python python-3.x sqlite tkinter


    【解决方案1】:

    您的buttonclick() 在您启动程序后立即运行,因为command=buttonclick() 应该是command=buttonclick。此外,您的数据库创建不好已经改变并重构了您的所有代码。

    import tkinter
    from tkinter import *
    import random
    from random import randint
    import sqlite3
    from sqlite3 import *
    
    
    
    
    
    # THIS IS CREATING DATABASE
    conn = sqlite3.connect('YouthCamp.db')
    c = conn.cursor()
    c.execute("""CREATE TABLE IF NOT EXISTS Registration (CampID INT, full TEXT)""")
    conn.commit()
    conn.close()
    
    
    #button
    def buttonclick():
        #heres where you lnk where you want your info to go
        FullNameValu= full.get()
        # generating the camp id
        CamperID = random.randint(100000, 1000000)
    
        conn = sqlite3.connect('YouthCamp.db')
        c = conn.cursor()
        c.execute("INSERT INTO Registration(CampID, full)VALUES(?, ?)",(CamperID, FullNameValu))
        conn.commit()
        conn.close()
    
        print("records inserted")
    
    
    
    
    #creating screen
    page = tkinter.Tk()
    #naming screen
    page.title('Camp Sign-up')
    #setting screen size
    page.geometry("1000x500")
    #creating text
    Title = tkinter.Label(page, text='Registration', font=('arial') )
    Title.pack()
    
    FullName = tkinter.Label(page, text='Full Name:', font=('arial'))
    FullName.place(x=110, y=50)
    
    full = StringVar()
    FullNameValue = Entry(page, textvar=full, width=25,)
    FullNameValue.place(x=200, y=53)
    
    #button
    work = Button(page, text='Submit', width=10, height=2, bg='lightgrey',
    command=buttonclick).place(x=475, y=450)
    
    
    
    page.mainloop()
    

    【讨论】:

    • 谢谢谢谢谢谢老实说,你为我节省了几个月的工作,因为我使用的是 buttonclick() 并且它在启动时会立即执行谢谢
    • @Jonah.Jackson 如果解决了您的问题,请接受答案。
    • 绿色支票?完毕。绝对有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    相关资源
    最近更新 更多