繁体   English   中英

为什么这个 Python tkinter 代码不显示文本文件的所有行?

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

我的 class 分配的 Python tkinter 代码不起作用。 该程序应该在 tkinter window 中显示文本文件的所有行,但由于某种原因,它不会在“状态:”行之后继续读取(它应该上升到地址)。 我的 class(包括我的老师)中没有人能够排除故障。 我用谷歌搜索了“为什么 python 读取行不显示”和“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"

任何帮助表示赞赏。 如有必要,我可以在评论中发布完整代码

这并不能解决您的问题,但您可以使用:

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()

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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