我试图使用 tkinter for GUI 用 python 为数据库编写代码.但是,当我按下提交按钮时,.get()



这是我正在处理的代码。在我创建sumbit函数的部分中,我尝试使用.get((函数,但它不起作用。此代码用于我目前正在做的评估,它将在一个月内到期。我一直在 youtube 上观看 tkinter 视频,试图了解我在做什么,但在重新观看视频后,我找不到我的代码和他的代码之间的任何差异。视频的链接 https://www.youtube.com/watch?v=YR3h2CY21-U&list=PLCC34OHNcOtoC6GglhF3ncJ5rLwQrLGnV&index=19。我最多 12 分 50 秒。

from tkinter import *
import sqlite3
root = Tk()
# root.geometry("800x500")

# Databases

# Create a database or connect to one
conn = sqlite3.connect('incidents.db')
# create a cursor
c = conn.cursor()
# Create table
'''c.execute("""CREATE TABLE incidents (
first_name text,
last_name text,
address text,
city text,
state text,
zipcode integer
)""")'''

# Create Submit Function For database
def submit():
# Create a database or connect to one
conn = sqlite3.connect('incidents.db')
# Create cursor
c = conn.cursor()
# Insert Into Table
c.execute("INSERT INTO incidents VALUES (:name, :date, :address, :city, :state, :zipcode)",
{
'name': Name.get(),
'date': date.get(),
'address': address.get(),
'city': city.get(),
'state': state.get(),
'zipcode': zipcode.get()
})
# Commit Changes
conn.commit()
# Close Connection
conn.close()
# Clear The Text Boxes
Name.delete(0, END)
date.delete(0, END)
address.delete(0, END)
city.delete(0, END)
state.delete(0, END)
zipcode.delete(0, END)

# Create Text Boxes
Name = Entry(root, width=30).grid(row=0, column=1, padx=20, pady=(10, 0))
date = Entry(root, width=30).grid(row=0, column=3)
address = Entry(root, width=30).grid(row=0, column=5)
city = Entry(root, width=30).grid(row=0, column=7)
state = Entry(root, width=30).grid(row=1, column=1)
zipcode = Entry(root, width=30).grid(row=1, column=3)
delete_box = Entry(root, width=30).grid(row=1, column=5, pady=5)
# Create Text Box Labels
name_label = Label(root, text="Name").grid(row=0, column=0, pady=(10, 0))
date_label = Label(root, text="Last Name").grid(row=0, column=2)
address_label = Label(root, text="Address").grid(row=0, column=4)
city_label = Label(root, text="City").grid(row=0, column=6)
state_label = Label(root, text="State").grid(row=1, column=0)
zipcode_label = Label(root, text="Zipcode").grid(row=1, column=2)
delete_box_label = Label(root, text="Select ID").grid(row=1, column=4, pady=5)
# Create Submit Button
submit_btn = Button(root, text="Add Record To Database", command=submit)
submit_btn.grid(row=6, column=0, columnspan=2, pady=10, padx=10, ipadx=100)
# Commit Changes
conn.commit()
# Close Connection
conn.close()

root.mainloop()

当我运行代码时,我收到以下错误消息:

Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 1699, in __call__
return self.func(*args)
File "/Users/School/PycharmProjects/HelloWorld/Databases.py", line 39, in submit
'name': Name.get(),
AttributeError: 'NoneType' object has no attribute 'get'

如果有人可以帮助我,将不胜感激。

连接执行没有问题。您收到的错误是由于您在屏幕上打包小部件的方式。

如果你将一个输入框打包在你定义它的同一行中,它会返回None而 None 是 python 的一种特殊数据类型,显然没有任何属性get

而不是

Name = Entry(root, width=30).grid(row=0, column=1, padx=20, pady=(10, 0))
date = Entry(root, width=30).grid(row=0, column=3)
address = Entry(root, width=30).grid(row=0, column=5)
city = Entry(root, width=30).grid(row=0, column=7)

试试这个

Name = Entry(root, width=30)
Name.grid(row=0, column=1, padx=20, pady=(10, 0))
date = Entry(root, width=30)
date.grid(row=0, column=3)
address = Entry(root, width=30)
address.grid(row=0, column=5)
city = Entry(root, width=30)
city.grid(row=0, column=7)

希望对你有帮助

我认为您应该尝试的是: 名称=tk。StringVar(root( 啧啧。条目(根,文本变量=名称(.网格(行=0,列=1( 然后使用 Name.get(( 还有一件事代替导入 * 使用导入 tkinter 作为 tk(作为约定(。对于您的情况,您可以直接编写StingVar((和Entry,而无需tk.前缀。 希望这会起作用

相关内容

  • 没有找到相关文章

最新更新