0

无法从 Tkinter、Python 中的文本小部件中提取数据。但是,它适用于代码中的 Entry 小部件。

我的目的是从用户那里获取段落输入,然后将数据添加到 CSV 文件中,然后使用 Photoshop 生成处方。

这是代码:

主文件

import abc
import csv, datetime
from tkinter import *
from tkinter import ttk
now=datetime.datetime.now().strftime("%d-%m-%Y")

def DataAdder2CSV():
    global edate, eSNO, eage, egender, ename, ePID, econtact, ecomp, eallergy, ehistory, eR
    a=edate.get()
    b=eSNO.get()
    c=eage.get()
    d=egender.get()
    e=ename.get()
    f=ePID.get()
    g=econtact.get()
    data=[a,b,c,d,e,f,g,ecomp, eallergy, ehistory, eR]
    print(data)
    
    file=open("Patient_Data.csv","a", newline="")
    writer=csv.writer(file, delimiter=",")

    writer.writerow(data)

    file.close()
    
def PrescGen():
    pass
root=Tk()
root.config(bg="#add8e6")

megaF=LabelFrame(root,bg="#add8e6", borderwidth=0)
megaF.pack()

greet=Label(megaF,text="Prescription Maker", bg="#add8e6", fg="black", font="LucidaConsole 30").pack(pady=20)

dateF=Frame(megaF, padx=10, pady=10, bg="#c3dde6")
dateF.pack()

date=Label(dateF,text="Date:", bg="#c3dde6").grid(row=0,column=0)

edate=Entry(dateF, width=10)
edate.insert(0,now)
edate.grid(row=0, column=1)

mainL=Frame(root,borderwidth=0, padx=10, pady=10, bg="#c3dde6")
mainL.pack(pady=20, padx=40)

PDlf=Frame(mainL, padx=10, pady=13, bg="#c3dde6")
PDlf.grid(row=0, column=0, sticky=NW)

sno=Label(PDlf,text="Sno:", bg="#c3dde6").grid(row=0,column=0)
eSNO=Entry(PDlf, width=3)
eSNO.grid(row=0, column=1)

age=Label(PDlf,text="Age:", bg="#c3dde6").grid(row=1,column=0)
eage=Entry(PDlf, width=3)
eage.grid(row=1, column=1, pady=10)

gender=Label(PDlf,text="Gender:", bg="#c3dde6").grid(row=2,column=0)
egender=Entry(PDlf, width=3)
egender.grid(row=2, column=1)

name=Label(PDlf,text="Name:", bg="#c3dde6").grid(row=0,column=3)
ename=Entry(PDlf, width=15)
ename.grid(row=0, column=4)

Pid=Label(PDlf,text="PatientID:", bg="#c3dde6").grid(row=1,column=3)
ePID=Entry(PDlf, width=15)
ePID.grid(row=1, column=4, pady=10)

contact=Label(PDlf,text="Contact:", bg="#c3dde6").grid(row=2,column=3)
econtact=Entry(PDlf, width=15)
econtact.grid(row=2, column=4)

blank=Label(PDlf,text="         ", bg="#c3dde6").grid(row=1,column=2)

contentLF=LabelFrame(mainL, padx=10, pady=10, bg="#c3dde6", borderwidth=0)
contentLF.grid(row=0, column=2, sticky=SE)
#Never gonna give you up, never gonna let you down
blank=Label(PDlf,text="         ", bg="#c3dde6").grid(row=0,column=2)

complaint=Label(contentLF,text="Complaint:", bg="#c3dde6").grid(row=0,column=1)
ecomp=Text(contentLF, width=50, height=3).grid(row=0, column=2)

allergy=Label(contentLF,text="Allergy:", bg="#c3dde6").grid(row=1,column=1)
eallergy=Text(contentLF, width=50, height=2)
eallergy.grid(row=1, column=2, pady=10)

history=Label(contentLF,text="History:", bg="#c3dde6").grid(row=2,column=1)
ehistory=Text(contentLF, width=50, height=5)
ehistory.grid(row=2, column=2)

R=Label(contentLF,text="Diagnosis:", bg="#c3dde6").grid(row=3,column=1)
eR=Text(contentLF, width=50, height=12)
eR.grid(row=3, column=2, pady=10)

buttF=LabelFrame(mainL, bg="#c3dde6", borderwidth=0)
buttF.grid(row=4, column=2, sticky=E)
genPres=Button(buttF,text="Generate Prescription", bg="#ff80b9", command=PrescGen).grid(row=0, column=0, padx=10, sticky=E)

csvAdd=Button(buttF,text="Add to Database", bg="#49ff9a", command=DataAdder2CSV).grid(row=0, column=1,padx=10, sticky=E)

pic=Frame(mainL, padx=10, pady=15, bg="#c3dde6")
pic.grid(row=1, column=0, sticky=NW)

root.title("Prescription Maker")
root.state("zoomed")

root.mainloop()

这是输出:

美丽的图形用户界面

这是我将所有数据提取到的 CSV 文件:

在 Content 选项卡之后发生的事情是由于数据是 Text 小部件而不是 Entry 小部件的形式。

如何获取用户的段落输入并将数据添加到 CSV 文件(Tkinter)?

4

0 回答 0