Joseph Farah
Joseph Farah

Reputation: 2534

Tkinter Splits Menu and Main Window Upon Attempted Close

I am using Tkinter to build a basic text editor. I have a small File menu with four buttons: Open, Save, New File, and Exit. The main part of the window is devoted to the editor and some buttons pop up occasionally (saving, opening, etc.) However, I have a couple of problems: 1) When I attempt to close the main window with the exit button on the window border, it splits itself into the main window in one window and the menu in a smaller window. Everything remains fully functional, but I have to close two windows to terminate the program. When I use the exit button in the menu this does not happen. 2) My second problem is unrelated to the question title, but my code is here so: I have four items coded into the menu section, but only three are showing up. The fourth only appears when the above phenomenon occurs. Why is it doing that? and how can I fix it?

Thanks very much! My description of the phenomena probably will be insufficient, so here is a link to a picture of what I am describing: https://goo.gl/vjwI5X You can see in the top right hand corner the window is black (this is the main text editor) but no menu--right next to it is the menu, in the smaller window, along with all buttons that happen to be open at the time.

Here is the my code:

from Tkinter import *
import sys
import menu_config
import tkMessageBox
import error_mes

#Variables that are globally needed
file_input = "" #whats put into the text box
_FILE_= "" #File the user wants to open; readapt to be synonymous with save?
open_a_file = "" #will be the entry field for opening a file
target = ""
new_file_ = ""
new_file_name = ""
def get_from_text():
    global file_input
    try:
        file_input = my_text_box.get("1.0", END)
        print file_input
    except:
        file_input = 'UHOH'
        print file_input

def save():
    global file_input, FILE_, target, _FILE_
    file_input = my_text_box.get("1.0", END)
    target = open(_FILE_, "r+w")
    target.truncate()
    target.write(file_input)

def exit_application():
    sys.exit(0)

def menu_open_file():
    global _FILE_, open_a_file, save
    try:
        open_a_file = Entry()
        open_a_file.grid(row = 3, column = 0)
        open_a_file.insert(0, "Path to File to Open")
        save.grid_forget()
        Button(main, text = "Click to Open", command = get_file).grid(row = 4, 
                                                                    column = 0)
    except:
        #tkMessageBox.showinfo("Error Code 52", "Something went wrong.")
        error_mes.error()

def get_file():
    global _FILE_, open_a_file
    _FILE_ = open_a_file.get()
    target = open(_FILE_, "r+w")
    opened_file = target.read()
    try:
        my_text_box.insert(INSERT, opened_file)
    except:
        tkMessageBox.showinfo("This is wrong", "Something went wrong!")

def new_file():
    global new_file_
    my_text_box.delete(1.0, END)
    try:
        new_file_ = Entry()
        new_file_.grid(row = 3, column = 0)
        new_file_.insert(0, "Path to new file + name")
        Button(main, text = "Click to Save", command = save_new_file).grid(row = 4, 
                                                                    column = 0)
    except:
        tkMessageBox.showinfo("Error Code 52", "Something went wrong.")

def save_new_file():
    global new_file_, new_file_name
    new_file_name = new_file_.get()



my_text_box = Text(bg = "black", fg = "white", insertbackground = "white")
my_text_box.grid(row = 0, column = 0)



def main():
    main = Tk()
    #The Menu

    first_menu = Menu(main)
    main.config(menu = first_menu)
    fileMenu = Menu(first_menu)
    fileMenu.add_command(label = "Open", command = menu_open_file)
    fileMenu.add_command(label = "New File...", command = new_file)
    fileMenu.add_command(label = "Save", command = save)
    fileMenu.add_command(label = "Exit", command = exit_application)
    first_menu.add_cascade(label = "File", menu = fileMenu)

    savebutton = Button(main, text = "Save", command = save)
    savebutton.grid(row = 3, column = 0)
    main.mainloop()

if __name__ == '__main__':
    main()

The module error_mes is one I created...sorry for any confusion. It has no effect on the program. But this is the code if it helps:

import tkMessageBox

def error():
    tkMessageBox.showinfo("Error 52", "Something went wrong.")

Upvotes: 1

Views: 71

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386332

The problem lies in these two lines of code:

my_text_box = Text(bg = "black", fg = "white", insertbackground = "white")
my_text_box.grid(row = 0, column = 0)

These lines of code run before you create the root window. Because of that, it creates an implicit root window. Later, you execute this code:

main = Tk()

The above creates a second root window in which everything else resides.

The solution is to move the creation of my_text_box after you create the root window, and to be sure and give it the root window as its parent.

Upvotes: 1

Related Questions