0

I need to copy this file to this text widget, and I don't understand why my text widget is NoneType object?

enter image description here

import tkinter
from tkinter import filedialog as fd

class word_example(tkinter.Frame):
    def __init__(self, parent):
        tkinter.Frame.__init__(self, parent)
        self.parent = parent
        self.parent.configure(background='#838B8E')
        self.parent.geometry("950x680+190+50")
        self.parent.title("Changer")

        self.set_all()

    def set_all(self):
        self.change_with = tkinter.Entry(self.parent, width=25).place(x = 120, y = 50)
        self.change_with_label = tkinter.Label(self.parent, text="Change with: ", 
                                               width=12, height=2).place(x=20, y=40)

        self.change_to = tkinter.Entry(self.parent, width=25).place(x=120, y=120)
        self.change_to_label = tkinter.Label(self.parent, text="Change to: ",
                                             width=12, height=2).place(x=20, y=110)

        self.open_file_butt = tkinter.Button(self.parent, text="Open file", 
                                             command=self.upload_file).place(x=52, y=175)

        self.start_file_text = tkinter.Text(self.parent, width=115, height=28).place(x=11, y=220)

        self.file_safe_option_label = tkinter.Label(self.parent, text="Safe method: ", 
                                                    width=12, height=2).place(x=320, y=40)

        self.v = tkinter.IntVar()

        self.file_safe_option_1 = tkinter.Radiobutton(self.parent, variable=self.v, value=0, 
                                                      text="Rewrite file", height=2, width=11)
        self.file_safe_option_2 = tkinter.Radiobutton(self.parent, variable=self.v, value=1,
                                                      text="Make new file", height=2, width=11)

        self.file_safe_option_1.place(x=450, y=40)
        self.file_safe_option_2.place(x=450, y=80)

        self.run_but = tkinter.Button(text="Run", background="#62C13F", foreground="#272727",
                                      width=6, height=2, font=38).place(x=830, y=150)

    def upload_file(self):
        self.num_file = 0
        self.file_name = fd.askopenfilename()
        with open(self.file_name, "r") as main_text_file:
            for line in main_text_file:
                self.formated_line = f"{line}\n"
                self.start_file_text.insert(self.num_file, self.formated_line)
                self.num_file += 1


def main():
    window = tkinter.Tk()
    word_example(window)
    window.mainloop()


if __name__ == "__main__":
    main()

What did I do wrong?

enter link description here

Robert
  • 7,394
  • 40
  • 45
  • 64
  • `start_file_text` is the return value of the `place()` method, not the text box itself. My guess is that `place` returns `None`. Try doing the `place(...)` in a separate statement. – tdelaney Aug 16 '22 at 19:41

1 Answers1

2

I believe that the .place() method returns None instead of the tkinter.Text object.

Replace the following:

self.start_file_text = tkinter.Text(self.parent, width=115, height=28).place(x=11, y=220)

with

self.start_file_text = tkinter.Text(self.parent, width=115, height=28)
self.start_file_text.place(x=11, y=220)

EDIT: You're still going to adjust the indent level of the above code, but other than that, it should work.

Keyacom
  • 94
  • 5