2013-06-30 70 views
2

我試圖通過python重新制作記事本問題是我的菜單不會打包。我正在使用tkinter模塊。到目前爲止,嘗試的產品應該是一個帶有上方菜單欄的文本框。點擊每個菜單按鈕應該拉起一個手段。我沒有寫每個菜單選項的任何功能,但讓我知道我所有的「add_command」 S沒有命令Tkinter文本編輯器不顯示菜單部件

from tkinter import * 
import tkinter.messagebox 
import tkinter 

# Text Box 
class ScrolledText(Frame): 
     def __init__(self, parent=None, text='', file=None): 
       Frame.__init__(self, parent) 
     self.pack(expand=YES, fill=X)    #make expandable 
     self.makewidgets() 
     self.settext(text,file) 

    def makewidgets(self): 
     sbar = Scrollbar(self) 
     text = Text(self, relief=SUNKEN) 
     sbar.config(command=text.yview)     # xlink sbar and text 
     text.config(yscrollcommand=sbar.set)   # moving one moves other 
     sbar.pack(side=RIGHT, fill=Y)     #pack first-clip last 
     text.pack(side=LEFT, expand=YES, fill=BOTH)  # text clipped first 
     self.text = text 

def settext(self, text='', file=None): 
    if file: 
     text = open(file, 'r').read() 
    self.text.delete('1.0', END) 
    self.text.insert('1.0', text) 
    self.text.mark_set(INSERT, '1.0') 
    self.text.focus() 

def gettext(self): 
    return self.text.get('1.0', END+'-1c') 

# Menu Bar 
class SimpleEditor(ScrolledText): 
    def __init__(self, parent=None, file=None): 
     frm = Frame(parent) 
     frm.pack(fill=X) 
     #Menus 
     mf = Menubutton(root, text='File', width=5, activebackground='lightblue') 
     mf.grid(row=0, column=0, sticky=W) 
     mf.menu = Menu(mf, tearoff=0) 
     mf["menu"] = mf.menu 

     me = Menubutton(root, text='Edit', width=5, activebackground='lightblue') 
     me.grid(row=0, column=1) 
     me.menu = Menu(me, tearoff=0) 
     me["menu"] = me.menu 

     mo = Menubutton(root, text='Format', width=8, activebackground='lightblue') 
     mo.grid(row=0, column=2, sticky=W) 
     mo.menu = Menu(mo, tearoff=0) 
     mo["menu"] = mo.menu 

     mv = Menubutton(root, text='File', width=5, activebackground='lightblue') 
     mv.grid(row=0, column=3, sticky=W) 
     mv.menu = Menu(mv, tearoff=0) 
     mv["menu"] = mv.menu 

     mh = Menubutton(root, text='Help', width=5, activebackground='lightblue') 
     mh.grid(row=0, column=4, sticky=W) 
     mh.menu = Menu(mh, tearoff=0) 
     mh["menu"] = mh.menu 

     # File Options 
     mf.menu.add_command(label='New   Ctrl+N', activebackground='lightblue') 
     mf.menu.add_command(label='Open...  Ctrl+O', activebackground='lightblue') 
     mf.menu.add_command(label='Save   Ctrl+S', activebackground='lightblue') 
     mf.menu.add_command(label='Save as...', activebackground='lightblue') 
     mf.menu.add_separator() 
     mf.menu.add_command(label='Page Setup...', activebackground='lightblue') 
     mf.menu.add_command(label='Print...  Ctrl+P', activebackground='lightblue') 
     mf.menu.add_separator() 
     mf.menu.add_command(label='Exit', activebackground='lightblue') 

     # Edit Options 
     me.menu.add_command(label='Undo   Ctrl+Z', activebackground='lightblue') 
     me.menu.add_separator() 
     me.menu.add_command(label='Cut   Ctrl+X', activebackground='lightblue') 
     me.menu.add_command(label='Copy   Ctrl+C', activebackground='lightblue') 
     me.menu.add_command(label='Paste  Ctrl+V', activebackground='lightblue') 
     me.menu.add_command(label='Delete   Del', activebackground='lightblue') 
     me.menu.add_separator() 
     me.menu.add_command(label='Find   Ctrl+F', activebackground='lightblue') 
     me.menu.add_command(label='Find Next  F3', activebackground='lightblue') 
     me.menu.add_command(label='Replace... Ctrl+H', activebackground='lightblue') 
     me.menu.add_command(label='Go To...  Ctrl+G', activebackground='lightblue') 
     me.menu.add_separator() 
     me.menu.add_command(label='Select All Ctrl+A', activebackground='lightblue') 
     me.menu.add_command(label='Time/Date  F5', activebackground='lightblue') 

     # Format Options 
     mo.menu.add_checkbutton(label='Word Wrap', activebackground='lightblue') 
     mo.menu.add_command(label='Font...', activebackground='lightblue') 

     # View Options 
     mv.menu.add_checkbutton(label='Status Bar', activebackground='lightblue') 

     # Help Options 
     mh.menu.add_command(label='View Help', activebackground='lightblue') 
     mh.menu.add_separator() 
     mh.menu.add_command(label='About Notepad', activebackground='lightblue') 


if __name__ == '__main__': 
    root = Tk() 
    if len(sys.argv) > 1: 
     ScrolledText(file=sys.argv[1]).mainloop() 
    else: 
     ScrolledText(text='').mainloop() 
    root.mainloop() 
+0

由於某種原因,我發現網格不會隨包裝一起出現。我重新設置了所有包裝菜單,它工作正常。有沒有人有任何線索,爲什麼會這樣? –

+0

您不能在同一個包含小部件中使用包和網格,因爲每個都會響應正在打包/網格化的小部件中的更改。所以,包會根據其算法增加或縮小小部件。網格會看到小部件改變大小並嘗試重新排列小部件以適應_its_算法。包會看到一些小部件改變了大小,並嘗試重新排列小部件以適合_its_算法。網格會看到一些小部件改變了大小,並且...無限。 –

回答

0

這是高度異常,使菜單按鈕爲滾動文本的孩子小部件。使菜單欄的正確方法是創建一個菜單(不是菜單按鈕),然後安裝菜單到根:

menubar = tk.Menu(root) 
    root.configure(menu=menubar) 
    fileMenu = tk.Menu(menubar) 
    menubar.add_cascade(label="File", menu=fileMenu) 
    ... 

這樣做,這樣保證你得到適當的,原生的菜單欄。