python - 在 Tkinter 上向 CSV 文件添加新信息时出现问题
问题描述
我有一个我正在研究的python项目。我需要制作一个运行 CSV、matplotlib 和其他模块的程序。我遇到了一个我无法解决的问题,通过 tkinter 将新信息保存到原始 CSV 文件中
主要问题是“保存到数据库”功能我的整个代码是:
from tkinter import *
from tkinter.ttk import *
import pandas as pd
from tkinter import messagebox
root = Tk()
root.title('DYR')
root.minsize(700, 500) # the minimum size of the window
root.geometry('500x500') # in what resolution the window will open at
root.configure(bg='LightBlue1')
nameLabel = Label(root, text=' Company Name',font=("Arial Bold", 12))
nameLabel.grid(column=2, row=0)
nameInput = Entry(width=30)
nameInput.grid(column=3, row=0)
idLabel= Label(root, text = 'Client ID',font=("Arial Bold", 12))
idLabel.grid(column=2, row=1)
id_input = Entry(width=9)
id_input.grid(column= 3, row=1)
addresslabel= Label(root, text = "Company Address", font=("Arial Bold", 12))
addresslabel.grid(column= 2, row= 2)
addressinput = Entry (width= 50 )
addressinput.grid(column =3 , row = 2)
fieldlabel= Label(root, text= "Filed",font=("Arial Bold", 12))
fieldlabel.grid(column=2 , row =3)
fieldcomobx = Combobox (root)
fieldcomobx['values']= ("Choose field",'consulting','Medical','Gaming','Cyber')
fieldcomobx.current(0) #set the selected item
fieldans = fieldcomobx.get()
fieldcomobx.grid(column=3, row=3)
numberof = Label (root, text = "Number of employees", font=("Arial Bold", 12))
numberof.grid(column = 2 , row = 4)
numberin = Entry (width= 15)
numberin.grid(column = 3 , row= 4)
contactlabel=Label(root, text = "Contact", font=("Arial Bold", 12))
contactlabel.grid(column=2, row =5)
contactin = Entry (width = 15)
contactin.grid(column=3, row =5)
lastcall = Label (root, text = "Last call ",font=("Arial Bold", 12))
lastcall.grid(column = 2 , row = 6)
lastcallin = Entry (width = 15)
lastcallin.grid(column = 3 , row=6)
def cheker():#func to check if the client exsit
import pandas as pd
path = r"C:\Users\HP\PycharmProjects\untitled\Gmar\Gmar_Project.csv"
companyName = str(nameInput.get())
df = pd.read_csv(path)
result = df[df.Company == companyName]
if len(result) == 0:
messagebox.showinfo("Cheker", "Not exist, Good luck!")
else:
messagebox.showinfo("Cheker", "The client already exists in the data base!")
btn = Button(root,text="check existence",command= cheker)
btn.grid(column=4, row = 0)
def save_to_db(): #save the information to csv
path = r"C:\Users\HP\PycharmProjects\untitled\Gmar\Gmar_Project.csv"
newRow = [str(nameInput.get()), str(id_input.get()), str(addressinput.get()),str(fieldans), str(numberin .get()),str(contactin.get()),str(lastcallin.get())]
df = pd.read_csv(path)
df2 = pd.DataFrame(newRow).T
df2.columns = ['Company','client ID','address','field','Number of employees','contact','last call'] # df.columns
df = df.append(df2, ignore_index=True)
df.to_csv(path_or_buf=path, index_label=False, index=False)
endbtn = Button (root, text = "SAVE" ,command = save_to_db())
endbtn.grid(column =3, row=8)
root.mainloop()
解决方案
问题是您fieldans
在创建组合框后设置了大约一毫秒。用户将没有机会制作一个部分。
GUI 开发中的一般经验法则是,您不应该在需要数据之前获取数据。
您需要fieldans = fieldcomobx.get()
向内移动,save_to_db
以便获得用户输入的值而不是默认值。
您还有一个问题,即您在save_to_db
GUI 启动时立即调用,而不是在用户有机会输入任何数据之后。
你需要这样定义endbtn
:
endbtn = Button (root, text = "SAVE" ,command = save_to_db)
推荐阅读
- tensorflow - 在本地环境中测试时使用笔记本“使用 Tensorflow-IO 从 Elasticsearch 流式传输结构化数据”时出错
- python - 如何使用代码单元打印功能仅在 Jupyter 笔记本中呈现超链接和文本?
- mysql - 如何修复 Wordpress 网站的 SQL 数据库错误
- sql - Sql 命令未正确结束的原因错误
- excel - Excel VBA复制粘贴然后格式化
- azure - 使用 azure 媒体服务 401 未经授权的 rest api 验证邮递员的资产 api
- esp8266 - ESP8266 创建另一个 WiFiClientSecure 总是失败 https 请求
- node.js - 使用在 AppEngine 上运行的 IAM + NodeJS 应用程序保护 Cloud Functions
- pandas - pandas 分组并连接 n 行
- javascript - CSS 在 Chart.js 的折线图中添加一个点