python - Tkinter Splits Menu and Main Window Upon Attempted Close -
i using tkinter build basic text editor. have small file menu 4 buttons: open, save, new file, , exit. main part of window devoted editor , buttons pop (saving, opening, etc.) however, have couple of problems: 1) when attempt close main window exit button on window border, splits main window in 1 window , menu in smaller window. remains functional, have close 2 windows terminate program. when use exit button in menu not happen. 2) second problem unrelated question title, code here so: have 4 items coded menu section, 3 showing up. fourth appears when above phenomenon occurs. why doing that? , how can fix it?
thanks much! description of phenomena insufficient, here link picture of describing: https://goo.gl/vjwi5x can see in top right hand corner window black (this main text editor) no menu--right next menu, in smaller window, along buttons happen open @ time.
here code:
from tkinter import * import sys import menu_config import tkmessagebox import error_mes #variables globally needed file_input = "" #whats put text box _file_= "" #file user wants open; readapt synonymous save? open_a_file = "" #will entry field opening 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 file open") save.grid_forget() button(main, text = "click 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 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 new file + name") button(main, text = "click 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
1 created...sorry confusion. has no effect on program. code if helps:
import tkmessagebox def error(): tkmessagebox.showinfo("error 52", "something went wrong.")
the problem lies in these 2 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 create root window. because of that, creates implicit root window. later, execute code:
main = tk()
the above creates second root window in else resides.
the solution move creation of my_text_box
after create root window, , sure , give root window parent.
Comments
Post a Comment