Gianni Spear
Gianni Spear

Reputation: 7924

Making a child window open on top

I have a child window (root) in Tkinter. When i open the new window for import data and processing, this child window go under the main window.

I used

root.lift()
root.call('wm', 'attributes', '.', '-topmost', True)

but the child window remain in the top always, also when a directory window is open

from __future__ import division
from Tkinter import *
from math import pi
import tkMessageBox
import Tkinter, Tkconstants, tkFileDialog
from numpy import nan
import Tkinter as tk
import os, shutil



class MainWindow(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.filename = None
        self.master.title("input")
        self.master.minsize(250, 150)
        self.grid(sticky=E+W+N+S)

        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)


        for i in range(1): self.rowconfigure(i, weight=1)
        self.columnconfigure(1, weight=1)

        self.button0 = Button(self, text="start", command=self.open_new_window, activeforeground="red")
        self.button0.grid(row=0, column=1, pady=2, padx=2, sticky=E+W+N+S)

    def open_new_window(self):

        root = tk.Toplevel(self)
        root.title("process")
        root.minsize(400, 150)
        root.maxsize(400, 150)
        root.grid()

        top = root.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)


        root.CHART_FILE_TYPES = [('All files', '*'),('text file', '.txt')]

        for i in range(5): root.rowconfigure(i, weight=1)
        root.columnconfigure(1, weight=1)

        root.CheckVar_a = IntVar()
        root.check_a = tk.Checkbutton(root, text="A", variable=root.CheckVar_a, onvalue=0, offvalue=1)
        root.check_a.grid(row=0, column=0, pady=0, padx=0, columnspan=3, sticky=W)

        root.CheckVar_b = IntVar()
        root.check_b = tk.Checkbutton(root, text="B", variable=root.CheckVar_b, onvalue=0, offvalue=1)
        root.check_b.grid(row=1, column=0, pady=0, padx=0, columnspan=3, sticky=W)

        root.CheckVar_c = IntVar()
        root.check_c = tk.Checkbutton(root, text="C", variable=root.CheckVar_c, onvalue=0, offvalue=1)
        root.check_c.grid(row=2, column=0, pady=0, padx=0, columnspan=3, sticky=W)

        root.CheckVar_d = IntVar()
        root.check_d = tk.Checkbutton(root, text="D", variable=root.CheckVar_d, onvalue=0, offvalue=1)
        root.check_d.grid(row=3, column=0, pady=0, padx=0, columnspan=3, sticky=W)

        def open_file():
            root.filename = tkFileDialog.askopenfilename(filetypes=[("Text Files",'.txt')])
            if not root.filename:
                tkMessageBox.showerror("Error", message="No text file (*.txt) imported ")
            return root.filename

        open_txt = tk.Button(root, text="Open", command=open_file, activeforeground="red")
        open_txt.grid(row=4, column=0, pady=2, padx=2, sticky=E+W+N+S)

        def process():
            if not root.filename:
                tkMessageBox.showerror("Error", message="None file (*.txt) active to process")
                return None
            pass

        pro = tk.Button(root, text="Process", command=process, activeforeground="red")
        pro.grid(row=4, column=1, pady=2, padx=2, sticky=E+W+N+S)


if __name__=="__main__":
   d = MainWindow()
   d.mainloop()

Upvotes: 0

Views: 459

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385970

You are using tkinter wrong. You can't have more than one instance of Tk in your program. You get one implicitly when you create a frame before creating the root window, then you get another in open_new_window.

To create a child window, create an instance of Toplevel.

Upvotes: 1

Related Questions