为什么此Python tkinter代码未显示文本文件的所有行?

问题描述 投票:0回答:1

我的用于类分配的Python tkinter代码不起作用。该程序supposed可以在tkinter窗口中显示文本文件的所有行,但是由于某些原因,它不会在“ State:”行之后继续读取(它应该上升到地址)。我班上的所有人(包括我的老师)都无法进行故障排除。我已经用Google搜索“为什么不显示python readline”和“ tkinter不显示”,但是没有任何关系。没有错误消息,因此我也无法定位。这是负责的代码:

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"

感谢您的帮助。如果需要,我可以在评论中发布完整的代码

python tkinter
1个回答
0
投票

无法解决您的问题,但是您可以使用:

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) # pach it in the entire window
text.insert(1.0, file.read())

file.close()

win.mainloop()

它要求显示在文本框中的文件。

© www.soinside.com 2019 - 2024. All rights reserved.