我正在使用Tkinter构建基本的文本编辑器。我有一个带有四个按钮的小文件菜单:打开,保存,新文件和退出。窗口的主要部分专门用于编辑器,偶尔会弹出一些按钮(保存,打开等) 但是,我有几个问题: 1)当我尝试用窗口边框上的退出按钮关闭主窗口时,它会在一个窗口中将自身分成主窗口,在较小的窗口中分割菜单。一切都保持完整功能,但我必须关闭两个窗口来终止程序。 当我使用菜单中的退出按钮时,这不会发生。 2)我的第二个问题与问题标题无关,但我的代码在这里:我在菜单部分编码了四个项目,但只有三个项目出现。第四种情况仅在出现上述现象时出现。它为什么这样做?我该如何解决?
非常感谢!我对这些现象的描述可能不够充分,所以这里有一个链接到我所描述的图片: https://goo.gl/vjwI5X 您可以在右上角看到窗口是黑色的(这是主文本编辑器)但没有菜单 - 紧挨着它的是菜单,在较小的窗口中,以及碰巧在打开的所有按钮时间。
这是我的代码:
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()
模块error_mes
是我创建的模块...抱歉任何混乱。它对该程序没有影响。但这是代码,如果它有帮助:
import tkMessageBox
def error():
tkMessageBox.showinfo("Error 52", "Something went wrong.")
答案 0 :(得分:1)
问题在于这两行代码:
my_text_box = Text(bg = "black", fg = "white", insertbackground = "white")
my_text_box.grid(row = 0, column = 0)
这些代码行在创建根窗口之前运行。因此,它会创建一个隐含的根窗口。稍后,您执行以下代码:
main = Tk()
上面创建了第二个根窗口,其中包含其他所有内容。
解决方案是在创建根窗口后移动my_text_box
的创建,并确保将根窗口作为其父窗口。