Home python List Box Tkinter are not correctly displayed lines

List Box Tkinter are not correctly displayed lines

Author

Date

Category

Faced a problem. In AgelistBox, I add Python JS words, but they are written out two words in one line, and I need to be in one line to one word, how to do it?

Part of code with 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)

Code of the entire program:

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 = "- Password")
  ELSE:
    Print ("Password was not introduced")
DEF infname ():
  Information = entryName.get ()
  If information:
    Print (Information, End = "- Name")
  ELSE:
    Print ("Name was not introduced")
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 = "Continue registration ...", Command = New_Window, ActivebackGround = "# 7A6D6D", bg = "# 141212", FG = "#FFFFFFF")
exitbutton = tk.button (Window, text = "exit", Command = Exit, ActivebackGround = "# 7A6D6D", bg = "# 141212", FG = "#FFFFFFF")
EntryPassword = TK.Entry (Window)
TextPassword = TK.Label (Window, Text = "Enter the password:", font = "arial 12", bg = "# 141212", FG = "#FFFFFF")
EntryName = TK.Entry (Window)
TextName = TK.Label (Window, Text = "Enter the name:", font = "arial 12", bg = "# 141212", FG = "#FFFFFFF")
InfButton = TK.Button (Window, Text = "Save", Command = Lambda: [information (), infname ()], ActivebackGround = "# 7A6D6D", bg = "# 141212", fg = "#FFFFFF")
RecordButton = TK.Button (Window, Text = "Write Data", Command = Lambda: [RecordPassword (), RecordName ()], ActivebackGround = "# 7A6D6D", bg = "# 141212", FG = "#FFFFFFF")
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 ()

Answer 1

You are in the loop and then insert the list, and each time insert the full list, and not a separate item:

age = ["python", "js"] # age - full list
For Ages in Age:
  agelistbox.insert (tk.end, age) # and here insert the full list

should be so

age = ["python", "js"]
For Ages in Age:
  agelistbox.insert (TK.End, Ages) # & lt; = Ages instead of AGE

I would still change the full list to have a multiple number, and the individual element is the only one, so logical.

ages = ["python", "js"]
For Age in Ages:
  AgelistBox.insert (TK.End, Age) 

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions