我正在使用一个简单的文字处理程序,但每次单击文件菜单中的选项时,选项“灰显”并且无法再单击。我已经寻找答案为什么,但我似乎无法在其他任何地方找到它。 我正在使用macOS 10.12.6和Python 3.6.3,我的保存功能似乎忽略了'if prevSaved == True'并跳过'else:'(参见hastags#中的代码) 这是我的代码:
from tkinter import *
from tkinter.filedialog import *
import time
prevSaved = False
K = "Untitled"
def saveAs():
text = textInput.get(1.0,END)
filename = asksaveasfilename(title = "Choose where to save your file")
file = open(str(filename) + ".txt" , "w+")
file.write(text)
file.close()
prevSaved = True
K, ext = os.path.splitext(os.path.basename(filename))
root.title("Python Word Processor - " + K)
#def save():
#if prevSaved == True:
# text = textInput.get(1.0,END)
# file = open(str(filename) + ".txt" , "w")
# file.write(text)
# file.close()
# else:
# saveAs()
def opentxt():
prevSaved = True
filename = askopenfilename(title = "Choose the file to open", filetypes = (("txt files","*.txt"),("all files","*.*")))
file = open(str(filename), "r")
disptext = file.read()
file.close()
textInput.delete(1.0, END)
textInput.insert(END, disptext)
K, ext = os.path.splitext(os.path.basename(filename))
root.title("Python Word Processor - " + K)
root = Tk() #Creates the GUI called root
root.resizable(False,False) #Makes the window non resizable
root.geometry('720x800') #Makes the window 1280x720
root.title("Python Word Processor - " + K)
root.iconbitmap('icon.ico')
#Widgets - everything needs to have .pack() for it to show up, entries also need
#to have the pack.() on the line below o retrieve the information stored inside
#tkinter releif options - FLAT,RAISED,SUNKEN,GROOVE,RIDGE
f1 = Frame(root, width=720, height=50)
f1.pack(side=TOP)
f1.pack_propagate(0) #doesnt let its children control its size
mainmenu=Menu(root)
filemenu=Menu(mainmenu)
filemenu.add_command(label="Save As", command = saveAs)
filemenu.add_command(label="Save")#, command = save)
filemenu.add_command(label="Open", command = opentxt)
mainmenu.add_cascade(label="File", menu=filemenu)
textInput = Text(root, width = 340, height = 770, font='bold')
textInput.
root.config(menu=mainmenu)
root.mainloop()
#widgets
#wID = Widget(root, widget_details)