Issue between functionality and creating new Tkinter windows

I just discovered why some functions doesn’t work properly in my script…
Let me explain…
So, using Tkinter, I created a root window where some function opens in it.
For other functions (to write datas in a csv file), I’ve programmed (inside them) to open a new window to have another one, while the root window is still open. For this last way, it doesn’t write datas into the csv file and IDE won’t print any error because the function is correct and it work properly if isolated from the rest of the program.
But… if I delete from the function the opening of a new window but simply open the function into the root window, it works!
My question is… why?
I would keep the new window for a better GUI clarity, so, is there a way to make the function work with a new window?
A little example would something like that (this doesn’t work):


from tkinter import *
import csv

root = Tk

def work_with_csv():
    new_win = Tk()
    frame_nw = Frame(new_win)

    name = StringVar()
    surname = StringVar()
    birth = StringVar()

    def write_in_csv():
        a_name = name.get()
        a_surname = surname.get()
        a_birth = birth.get()

        with open('path/to/document.csv', 'a', encoding='UTF-8') as csv_file:
             writer = csv.writer(csv_file)
             writer.writerow([a_name, a_surname, a_birth])
        csv_file.close()

I think we need to see some code. It isn’t clear to me what “open the
function into the root window” etc means.

Don’t forget that GUIs need to run the “main loop” to operate, and that
other actions (writing to CSV files etc) are usually mediated by
callbacks in response to GUI actions.

Anyway, hopefully something in your code will be apparent.

Don’t forget to enclose code between lines of triple backticks:

 ```
 your code
 goes here
 ```

There’s a </> button in the compose window to help with this bit.

CHeers,
Cameron Simpson cs@cskk.id.au

I suspect your problem is that you’re using Tk() twice to open a new window. You really don’t want to call that twice, it actually basically creates two entirely separate Tkinter systems that don’t know how to talk to each other. What you want to do is use Toplevel instead for your new window, which takes the root window as its parent.

God bless you my friend!!! It was months that I was searching for a solution. I heard something about Toplevel but documentation is strictly technical. So I found a little tutorial and now I solved… I hope:-)) (and first I finally understood what Toplevel is).
As a total newbie with so little time to spend for learning to code is quite difficult follow a regular learning path!
Thanks a lot again!

Sorry, I don’t understand why you said this to me… what I had leaved out from the backticks?

The code was missing in the email version I received. Seems ok on the web forum. Sorry for the miscommunication.

Toplevel is valid for tkinter’s frames too?

Not sure what you mean, but yes you can put frames inside a toplevel, any widget in general. It behaves the same really as Tk does. It’s actually a good idea to put a ttk.Frame inside your toplevels immediately, because that’ll make it have the correct background colour.

Simply, my doubt was if Toplevel affects only windows created with ’ Tk() ’ or other widgets too. You have understood well what I meant.
Thanks for you post.