0

So i just want to make this window a text widget thats always on top. Im trying to add this widget but it keeps creating another window...

import tkinter as tk
from tkinter import ttk

class App(tk.Frame):

    def __init__(self, master, *args, **kwargs):
        tk.Frame.__init__(self, master, *args, **kwargs)

        self.pack()

        hello="hello"

        self.tb = tk.Text(self)
        self.tb.pack(expand=1, fill = tk.BOTH)
        self.tb.insert(tk.END, hello)

        topLevelWindow = tk.Toplevel(self)
        # Make topLevelWindow remain on top until destroyed, or attribute changes.
        topLevelWindow.attributes('-topmost', 'true')

if __name__ == "__main__":
    root = tk.Tk()
    main = App(root)
    root.mainloop()
mikazz
  • 159
  • 1
  • 11
  • 2
    It keeps creating another window because ... it creates another window. What do you think `tk.Toplevel(self)` does? – Bryan Oakley Dec 07 '17 at 19:29

1 Answers1

0

If you want your text widget to be active you need to call this instead:

self.tb.focus_set()

What you're doing is instead create a Toplevel widget, which is a window that stays topmost, as in above all windows in desktop, you should remove:

    topLevelWindow = tk.Toplevel(self)
    # Make topLevelWindow remain on top until destroyed, or attribute changes.
    topLevelWindow.attributes('-topmost', 'true')

If you also want that your entire window you can do that for your root instead in your 'main' according to this answer:

root.call('wm', 'attributes', '.', '-topmost', '1')

Finally to have:

import tkinter as tk
from tkinter import ttk

class App(tk.Frame):

    def __init__(self, master, *args, **kwargs):
        tk.Frame.__init__(self, master, *args, **kwargs)

        self.pack()

        hello="hello"

        self.tb = tk.Text(self)
        self.tb.pack(expand=1, fill = tk.BOTH)
        self.tb.insert(tk.END, hello)

        self.tb.focus_set()

if __name__ == "__main__":
    root = tk.Tk()
    root.call('wm', 'attributes', '.', '-topmost', '1')
    main = App(root)
    root.mainloop()

Also if you want to make other widgets unfocusable:

widget.config(takefocus=False)
Nae
  • 14,209
  • 7
  • 52
  • 79