简体   繁体   中英

Why isn't this Python tkinter code not displaying all lines of a text file?

My Python tkinter code for a class assignment isn't working. The program is supposed to display all the lines of a text file in a tkinter window, but for some reason it won't keep reading after the "State: " line (It should be going up to the Address). No one in my class (including my teacher) has been able to troubleshoot. I've googled "why isn't python readline displaying" and "tkinter not displaying" but nothing is relevant. There are no error messages, so I can't orient myself with that either. Here is the code responsible:

def find_cus_win():
    find_cus = Toplevel(main_menu)  # estabilishes it
    find_cus.iconbitmap("Milkbar.ico")  # adds an icon
    find_cus.title("Find Customer Interface")  # titles it

    fc_heading = Label(
        find_cus, text="Find Customer", bg=heading_bg, fg=heading_fg, bd=1
    )
    fc_heading.config(height=2, width=20, font=heading_font)
    fc_heading.pack(side=TOP, fill=BOTH)

    fc_mm_frame = Frame(find_cus)
    fc_body_frame = Frame(fc_mm_frame)
    fc_but_frame = Frame(fc_body_frame)

    label_find_box = Label(fc_body_frame, text="Enter text file name:", fg=label_fg)
    label_find_box.config(font=label_font)
    label_find_box.grid(row=0)

    fc_find_box = Entry(fc_body_frame, width=20, bg=entry_bg, font=entry_font)
    fc_find_box.grid(row=1, columnspan=entry_cspan, pady=10)

    def fc_clear():
        fc_entryboxes = [fc_find_box]

        # clears entry boxes
        for entry in fc_entryboxes:
            entry.delete(0, END)

    # find customer file function
    def find():
        # defines the customer id as the entry box input
        id = str(fc_find_box.get())

        # if the selected exists, the file will be displayed
        if os.path.exists("Accounts/" + str(id) + ".txt"):
            # display customer window
            display_cus = Toplevel(find_cus)
            display_cus.iconbitmap("Milkbar.ico")  # adds an icon
            display_cus.title(str(id) + ".txt")
            display_cus.geometry("1000x1000")

            disc_heading = Label(
                display_cus, text=str(id), bg=heading_bg, fg=heading_fg, bd=1
            )
            disc_heading.config(height=2, width=20, font=heading_font)
            disc_heading.pack(side=TOP, fill=BOTH)

            disc_mm_frame = Frame(display_cus)
            disc_but_frame = Frame(disc_mm_frame)

            # opens the specified file as readable
            file = open("Accounts/" + str(id) + ".txt", "r")
            # creates labels with the lines
            lname1 = Label(
                disc_mm_frame, text=str(file.readline().rstrip()), fg=label_fg
            )
            lname1.config(font=label_font)
            lname1.grid(column=0, row=0, sticky=W)

            lname2 = Label(
                disc_mm_frame, text=str(file.readline().rstrip()), fg=label_fg
            )
            lname2.config(font=label_font)
            lname2.grid(column=0, row=1, sticky=W)

            laddress1 = Label(
                disc_mm_frame, text=str(file.readline().rstrip()), fg=label_fg
            )
            laddress1.config(font=label_font)
            laddress1.grid(column=0, row=2, sticky=W)

            lsuburb1 = Label(
                disc_mm_frame, text=str(file.readline().rstrip()), fg=label_fg
            )
            lsuburb1.config(font=label_font)
            lsuburb1.grid(column=0, row=3, sticky=W)

            lpostcode1 = Label(
                disc_mm_frame, text=str(file.readline().rstrip()), fg=label_fg
            )
            lpostcode1.config(font=label_font)
            lpostcode1.grid(column=0, row=4, sticky=W)

            lstate1 = Label(
                disc_mm_frame, text=str(file.readline().rstrip()), fg=label_fg
            )
            lstate1.config(font=label_font)
            lstate1.grid(column=0, row=5, sticky=W)

            lphone1 = Label(
                disc_mm_frame, text=str(file.readline().rstrip()), fg=label_fg
            )
            lphone1.config(font=label_font)
            lphone1.grid(column=0, row=6, sticky=W)

            lemail1 = Label(
                disc_mm_frame, text=str(file.readline().rstrip()), fg=label_fg
            )
            lemail1.config(font=label_font)
            lemail1.grid(column=0, row=7, sticky=W)

            disc_quit = Button(
            disc_but_frame, text="Quit Customer File", command=lambda: window_quit(display_cus), fg=quit_fg
            )
            disc_quit.config(font=quit_font)
            disc_quit.grid(row=0)

            disc_mm_frame.pack(fill=BOTH)
            disc_but_frame.grid(row=8)

            disc_widgets = [
                lname1,
                lname2,
                laddress1,
                lsuburb1,
                lpostcode1,
                lstate1,
                lphone1,
                lemail1,
                disc_mm_frame,
                disc_but_frame
            ]

            # changes all the backgrounds of non-entry boxes
            for widget in disc_widgets:
                widget["bg"] = "cornflower blue"

Any help is appreciated. I can post the full code in the comments if necessary

That doesn't solve your problem, but you can use:

from tkinter import *
from tkinter.scrolledtext import *
from tkinter.filedialog import *

win = Tk()

f = askopenfile()
f = f.name # extract the name of the file chosen
try:
    file = open(f, "r")
except: # if user clicks "Cancel"
    pass

text = ScrolledText(win, width=80, height=41, wrap=WORD) # create text zone
text.pack(expand=True, fill=BOTH) # pack it in the entire window
text.insert(1.0, file.read())

file.close()

win.mainloop()

which asks for a file which is displayed on a text box.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM