Tkinter own text dialog?

Can someone please tell me why this isn't working and I'm not getting the text I entered back.

 class eigene_dialoge:    def notiz_dialog(title):        notiz = "inhalt"#o(title,"r")        w_editnotiz = tk.Tk()        def cancel():            w_editnotiz.destroy        def ok():            w_editnotiz.destroy        l_wttitle = tk.Label(w_editnotiz, text="Titel:")        l_wtitle = tk.Entry(w_editnotiz)        l_wtitle.insert(0,title)        l_wtinhalt = tk.Label(w_editnotiz, text="Inhalt:")        l_winhalt = tk.Entry(w_editnotiz)        l_winhalt.insert(0,notiz.read())        l_button_save = tk.Button(w_editnotiz, text="Save" ,command=ok)        l_button_cancel = tk.Button(w_editnotiz, text="Abbrechen" ,command=cancel)        l_wttitle.pack()        l_wtitle.pack()        l_wtinhalt.pack()        l_winhalt.pack()        l_button_save.pack()        l_button_cancel.pack()               l_button_save.pack(fill="x")        l_button_cancel.pack(fill="x")        w_editnotiz.mainloop()        return [l_wtitle.get(),l_winhalt.get()] print(eigene_dialoge.notiz_dialog("test"))
(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
1 Answer
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
regex9
1 year ago

(1) Strings do not have a method read more. Inserting into the input field should therefore look like this instead:

l_winhalt.insert(0, notiz)

2) At the time when you read the input fields, they no longer exist.

One solution would be another sequence of operations:

# ...

result = [l_wtitle.get(), l_winhalt.get()]
w_editnotiz.mainloop()

return result

It would be even better if you were to use the traditional tkinter structure. There is only one root window for each application if you need multiple window instances, use this Frame-Widget. For building the GUI you can work object-oriented.

import tkinter as tk

class Dialogue:
  def __init__(self, window):
    self.window = window

  def cancel(self):
    self.window.destroy()

  def ok(self):
    self.window.destroy()

class NoteDialogue(Dialogue):
  def __init__(self, window, title):
    Dialogue.__init__(self, window)

    note = "Inhalt"

    title_label = tk.Label(self.window, text="Titel:")
    self.title_field = tk.Entry(self.window)
    self.title_field.insert(0, title)
    content_label = tk.Label(self.window, text="Inhalt:")
    self.content_field = tk.Entry(self.window)
    self.content_field.insert(0, note)
    save_button = tk.Button(self.window, text="Save" ,command=self.ok)
    cancel_button = tk.Button(self.window, text="Abbrechen" ,command=self.cancel)

    title_label.pack()
    self.title_field.pack()
    content_label.pack()
    self.content_field.pack()
    save_button.pack(fill="x")
    cancel_button.pack(fill="x")

  def get_inputs(self):
    return [self.title_field.get(), self.content_field.get()]

root = tk.Tk()

dialogue = NoteDialogue(root, "Test")
print(dialogue.get_inputs())

root.mainloop()