从 tkinter 文本小部件操作用户输入

Manipulating user input from tkinter text widget

我已经使用 tkinter 编写了一个基本的 GUI - 请参见下面的代码。我把它放在用户可以 copy/paste 进入“客户描述”的位置,它会创建一个项目列表。我认为如果用户输入的是列表格式,我正在尝试做的事情会更有意义。

用户输入示例:

  1. 管道,12",SA-106 GR.B,SCH 40,0.406" 壁面 SMLS
  2. PIPE, 8", SA-106 GR. B, SCH 40, 0.322" WALL SMLS
  3. 管道,6",SA-106 GR.C,SCH 160,0.719" WALL SMLS
  4. 弯头 45 LR,16",SA-234 WPB,SCH 40,0.500" WALL,BW SMLS

现在我有了用户输入,我需要为每个条目 运行 我的 python 程序并将其插入到标题为“dave_description”的第二个文本字段中。

我的问题是让函数执行此操作的最佳方法是什么?我似乎无法弄清楚如何让它 运行 每个条目的功能,但我还需要让它填充第二个文本框。我想出了如何将原始文本“插入”其中,但现在我已将其变成列表后不知道该怎么做。

import tkinter as tk
from tkinter import *
from tkinter import BOTH, END, LEFT

HEIGHT = 970
WIDTH = 1500

root = tk.Tk()
root.title( 'Daves Generator')

canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()

background_image = tk.PhotoImage(file='david.png')
background_label = tk.Label(root, image=background_image)
background_label.place(relwidth=1, relheight=1)

frame_one = tk.Frame(root, bg='#003d70',bd=5)
frame_one.place(relx=0.17, rely=.15, relwidth=0.3,relheight=0.8, anchor='n')

customer_description = tk.Text(frame_one, state=NORMAL, width=125, wrap=WORD, height=500,font=("Courier", 8))
customer_description.place(relwidth=1, relheight=1)

frame_two = tk.Frame(root, bg='#003d70',bd=5)
frame_two.place(relx=0.5, rely=.15, relwidth=0.3,relheight=0.8, anchor='n')

dave_description = tk.Text(frame_two, width=125, height=500,font=("Courier", 8))
dave_description.place(relwidth=1, relheight=1)

frame_three = tk.Frame(root, bg='#003d70',bd=5)
frame_three.place(relx=0.83, rely=.15, relwidth=0.3,relheight=0.8, anchor='n')

sap_code = tk.Label(frame_three, width=125, height=500,font=("Courier", 8))
sap_code.place(relwidth=1, relheight=1)

header_frame_one = tk.Label(root, text="Customer Descriptions", font=("Courier", 14), fg='#003d70')
header_frame_one.place(relx=0.17, rely=0.13, relwidth=0.2, relheight=0.025, anchor='n')

header_frame_two = tk.Label(root, text="PJ Descriptions", font=("Courier", 14), fg='#003d70')
header_frame_two.place(relx=0.5, rely=0.13, relwidth=0.2, relheight=0.025, anchor='n')

header_frame_three = tk.Label(root, text="SAP Code", font=("Courier", 14), fg='#003d70')
header_frame_three.place(relx=0.83, rely=0.13, relwidth=0.2, relheight=0.025, anchor='n')

generate_button = tk.Button(root,text="Generate!", font=("Courier", 14), fg='white', bg='#003d70', command=lambda: get_customer_description())
generate_button.place(relx=0.5, rely=0.08, relwidth=0.15, relheight=0.025, anchor='n')

def get_customer_description():
    all_descriptions = [customer_description.get("1.0", "end-1c")]
    print(all_descriptions)

root.mainloop()

谢谢马蒂斯!!发布答案,使其显示为已解决。如果你愿意 post 这样我就可以接受你的回答并给它投赞成票!

result= text_widget.get("1.0", "end-1c").split("\n")
    for line in result:
        func_1():