文本小部件tkinter

时间:2014-04-01 11:21:05

标签: python tkinter

我有点问题。我有一个用于用户输入的文本小部件而不是条目小部件,似乎无法从中获取数据。例如,使用条目小部件,您使用.get(),但是您将使用什么用于文本小部件?

感谢您的帮助......

Label(insertscreen, text="Please enter the Recipe ID").grid(row=0, sticky=W)
Label(insertscreen, text="Please enter the Recipe Name").grid(row=1, sticky=W)
Label(insertscreen, text="Please enter the Method of the Recipe").grid(row=2, sticky=W)
Label(insertscreen, text="Please enter the First Ingredient").grid(row=3, sticky=W)
Label(insertscreen, text="Please enter the Second Ingredient").grid(row=4, sticky=W)
Label(insertscreen, text="Please enter the Third Ingredient").grid(row=5, sticky=W)
Label(insertscreen, text="Please enter the Fourth Ingredient").grid(row=6, sticky=W)
Label(insertscreen, text="Please enter the Fifth Ingredient").grid(row=7, sticky=W)
Label(insertscreen, text="Please enter the Cooking Time, in minutes").grid(row=8, sticky=W)

Enter1= Entry(insertscreen)
Enter1.grid(row=0, column=1)
Enter2= Entry(insertscreen)
Enter2.grid(row=1, column=1)
Enter3= Text(insertscreen, width = 50, height = 10)
Enter3.grid(row=2, column=1)
Enter4= Entry(insertscreen)
Enter4.grid(row=3, column=1)
Enter5= Entry(insertscreen)
Enter5.grid(row=4, column=1)
Enter6= Entry(insertscreen)
Enter6.grid(row=5, column=1)
Enter7= Entry(insertscreen)
Enter7.grid(row=6, column=1)
Enter8= Entry(insertscreen)
Enter8.grid(row=7, column=1)
Enter9= Entry(insertscreen)
Enter9.grid(row=8, column=1)

#Entering the new recipe into the database
def submit_recipe():

global Enter1, Enter2, Enter3, Enter4, Enter5, Enter6, Enter7, Enter8, Enter9, new_db

ID = Enter1.get()
Name = Enter2.get()
Method = Enter3.get()
Ing1 = Enter4.get()
Ing2 = Enter5.get()
Ing3 = Enter6.get()
Ing4 = Enter7.get()
Ing5 = Enter8.get()
TimeofRec = Enter9.get()

2 个答案:

答案 0 :(得分:2)

文本窗口小部件使用get,但是当窗口小部件管理多行文本时,它有选择器来指定内容的区域。有关使用此小组件的教程和tkdocs的完整详细信息,请参阅manual page站点。获取第一行文本的示例:

firstline = textWidget.get("1.0", "1.0 lineend")

答案 1 :(得分:0)

从Text小部件检索文本的两种最常用方法如下:

1)获取小部件中的所有文本:

    all_text = TextWidget.get('1.0', Tkinter.END)

2)抓取当前选中的文字:

    ranges = TextWidget.tag_ranges(Tkinter.SEL)
    if ranges:
        selected_text = TextWidget.get(*ranges)