0

Столкнулся с проблемой. В agelistbox я добавляю слова python js, но они выписываются по два слова в одну строчку, а мне надо, чтобы в одной строчке было по одному слову, как это сделать?

Часть кода с listbox:

def new_window():
    new_window = tk.Toplevel(window)
    new_window.geometry(f"700x300+500+300")
    new_window.resizable(width=False, height=False)
    new_window.title("user1")
    agelistbox=tk.Listbox(new_window, height = 10, width = 15)
    agelistbox.place(x=0, y=0)
    age = ["python", "js"]
    for ages in age:
        agelistbox.insert(tk.END, age)

Код всей программы:

import tkinter as tk
from tkinter import messagebox
window = tk.Tk()
window.geometry(f"700x300+500+300")
window.title("user")
window.resizable(width=False, height=False)
window["bg"] = "#141212"

def exit():
    window.destroy()

def information():
    information = entrypassword.get()
    if information:
        print(information, end="-пароль   ")
    else:
        print("Пароль не был введён")

def infname():
    information = entryname.get()
    if information:
        print(information, end="-имя")
    else:
        print("Имя не было введено")
              
def recordpassword():
    information = entrypassword.get()
    information1 = entryname.get()
    recordfile = open("C:\\sublime,pycharm.i.t.d\\passwords and names.txt", "a", encoding = "utf-8")
    recordfile.write(f"{information}\n")
    recordfile.close()

def recordname():
    information = entrypassword.get()
    information1 = entryname.get()
    recordfile = open("C:\\sublime,pycharm.i.t.d\\passwords and names.txt", "a", encoding = "utf-8")
    recordfile.write(f"{information1}\n")
    recordfile.close()

def new_window():
    new_window = tk.Toplevel(window)
    new_window.geometry(f"700x300+500+300")
    new_window.resizable(width=False, height=False)
    new_window.title("user1")
    agelistbox=tk.Listbox(new_window, height = 10, width = 15)
    agelistbox.place(x=0, y=0)
    age = ["python", "js"]
    for ages in age:
        agelistbox.insert(tk.END, age)

new_window_button = tk.Button(window, text="Продолжить регистрацию...", command=new_window, activebackground="#7a6d6d", bg = "#141212", fg = "#ffffff")         
exitbutton = tk.Button(window, text="exit", command=exit, activebackground="#7a6d6d", bg = "#141212", fg = "#ffffff")
entrypassword = tk.Entry(window)
textpassword = tk.Label(window, text="Введите пароль: ", font="Arial 12", bg = "#141212", fg = "#ffffff")
entryname = tk.Entry(window)
textname = tk.Label(window,text="Введите имя: ", font = "Arial 12", bg = "#141212", fg = "#ffffff")
infbutton = tk.Button(window, text="Сохранить", command=lambda:[information(), infname()], activebackground="#7a6d6d", bg = "#141212", fg = "#ffffff")
recordbutton = tk.Button(window, text="Записать данные", command=lambda:[recordpassword(), recordname()], activebackground="#7a6d6d", bg = "#141212", fg = "#ffffff")


new_window_button.place(x=2, y=75)
exitbutton.place(x=2, y=273)
recordbutton.place(x=70, y= 50)
infbutton.place(x=2, y=50)
entrypassword.place(x=132, y=28)
entryname.place(x=108, y=4)
textpassword.place(x=1, y=25)
textname.place(x=1, y=1)
window.mainloop()

1 ответ 1

0

Вы в цикле итерируетесь по списку, и каждый раз вставляете полный список, а не отдельный элемент:

age = ["python", "js"]  # age - полный список
for ages in age:
    agelistbox.insert(tk.END, age)  # и тут полный список вставляете

Должно быть так

age = ["python", "js"]
for ages in age:
    agelistbox.insert(tk.END, ages)  # <= ages вместо age

Я бы еще поменял, чтобы у полного списка было множественное число, а у отдельного элемента - единственное, так логичнее.

ages = ["python", "js"]
for age in ages:
    agelistbox.insert(tk.END, age)

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.