Home > Enterprise >  tkinter: How to create "N" Entry widgets and insert string-lines into them?
tkinter: How to create "N" Entry widgets and insert string-lines into them?

Time:01-24

I'm stuck on a little problem and I need help please.

I start by reading my variable X and I count the number of lines N.

Then I create N Entry widgets.

So my problem is that I would like to insert each line in each entry. I can't find a solution, maybe I should create a list with the entries but then I don't know what to do? :/

all_my_entries = []
X = str(fiche_pat_table[5])
N= len(x.split('\n'))

for y in range(N):
  tk.Label(my_frame, text="antécédent", bg="#F5CBA7").grid(row=y 1, column=0)
  my_entry= tk.Entry(my_frame)
  my_entry.grid(row=y 1, column=1)
  all_my_entries.append(my_entry)

hi i could edit my code here :/

from tkinter import *
import tkinter as tk

root = Tk()
root.geometry("700x500")

all_atcd_entries=[]
x = str("line1 \n line2")
n_atcd = len(x.split('\n'))
ligne_atcd = x.split('\n')

for y in range(n_atcd):
    tk.Label(root, text="antécédent", bg="#F5CBA7").grid(row=y   1, column=0)
    eatcd_deux = tk.Entry(root)
    eatcd_deux.grid(row=y   1, column=1)
    all_atcd_entries.append(eatcd_deux)

##### i tired that #####
for entrie in all_atcd_entries:
    for ligne in ligne_atcd:
        entrie.insert(0, ligne)


root.mainloop()

CodePudding user response:

You can use the built-in zip() function to group each entry with the corresponding line:

rom tkinter import *
import tkinter as tk

root = Tk()
root.geometry("700x500")

all_atcd_entries=[]
x = str("line1 \n line2")
n_atcd = len(x.split('\n'))
ligne_atcd = x.split('\n')

for y in range(n_atcd):
    tk.Label(root, text="antécédent", bg="#F5CBA7").grid(row=y   1, column=0)
    eatcd_deux = tk.Entry(root)
    eatcd_deux.grid(row=y   1, column=1)
    all_atcd_entries.append(eatcd_deux)

for entrie, ligne in zip(all_atcd_entries, ligne_atcd):
    entrie.insert(0, ligne)


root.mainloop()
  •  Tags:  
  • Related