如何从 Python 3.x 的 Tkinter 中的文本小部件获取段落数据?如何从用户那里获取段落输入并将数据添加到 CSV 文件中?

问题描述

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

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

代码如下:

ma​​in.py

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,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",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,bg="#c3dde6")
mainL.pack(pady=20,padx=40)

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

sno=Label(PDlf,text="Sno:",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:",column=3)
ename=Entry(PDlf,width=15)
ename.grid(row=0,column=4)

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

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

blank=Label(PDlf,text="         ",column=2)

contentLF=LabelFrame(mainL,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,column=2)

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

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

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

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

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

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

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

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

root.mainloop()

输出如下:

Beautigul GUI

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

The things happening after Content tab are due to the fact that the data is in the form of Text widget and not Entry widget.

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)