我正在使用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.")
问题在于这两行代码:
my_text_box = Text(bg = "black", fg = "white", insertbackground = "white")
my_text_box.grid(row = 0, column = 0)
这几行代码在创建根窗口之前运行。因此,它创建了一个隐式根窗口。之后,执行以下代码:
main = Tk()
上面的命令创建了第二个根窗口,其他所有内容都驻留在这个根窗口中。
解决方案是在创建根窗口之后移动my_text_box
的创建,并确保将根窗口作为其父窗口。