TA的每日心情 | 慵懒 2013-7-6 15:08 |
---|
签到天数: 2 天 连续签到: 2 天 [LV.1]初来乍到
|
发表于 2013-2-19 20:55:16
|
显示全部楼层
你根本沒有把 mb 添加到 window 上,要加上去你要把這兩個對象都放到 box 裏面- #!/usr/bin/python
- #coding:utf8
- from gi.repository import Gtk, Pango, GtkSource, Gdk, GObject
- import os
- class SearchDialog(Gtk.Dialog):
- """这里是一个在文本中搜索字符串的对话框。"""
- def __init__(self, parent):
- Gtk.Dialog.__init__(self, "Search", parent,
- Gtk.DialogFlags.MODAL, buttons=(
- Gtk.STOCK_FIND, Gtk.ResponseType.OK,
- Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL
- )
- )
-
- box = self.get_content_area()
-
- box.add(Gtk.Label("查找:"))
-
- self.entry = Gtk.Entry()
- box.add(self.entry)
-
- self.show_all()
-
-
- class TextViewWindow(Gtk.Window):
- """程序的主要部分"""
-
- history = []
- box = Gtk.Box(1, 0)
-
- def __init__(self):
- Gtk.Window.__init__(self, title="带背景的文本编辑器")
- self.add(self.box)
- GObject.signal_new("changed", BEditDocument, GObject.SIGNAL_RUN_FIRST, GObject.TYPE_NONE, ())
-
- self.set_default_size(560, 350)
- self.create_notebook()
- self.create_menubar()
- self.create_toolbar()
-
- self.on_new()
- #self.set_style()
- #载入历史记录
- #self.load_config()
-
- def create_toolbar(self):
- """建立工具栏,这里的工具栏,实际上是选项卡上的一些标签页。"""
-
- button = [Gtk.ToolButton.new_from_stock(Gtk.STOCK_NEW),
- Gtk.MenuToolButton.new_from_stock(Gtk.STOCK_OPEN),
- Gtk.ToolButton.new_from_stock(Gtk.STOCK_SAVE),
- Gtk.ToolButton.new_from_stock(Gtk.STOCK_PRINT),
- Gtk.ToolButton.new_from_stock(Gtk.STOCK_UNDO),
- Gtk.ToolButton.new_from_stock(Gtk.STOCK_REDO),
- Gtk.ToolButton.new_from_stock(Gtk.STOCK_CUT),
- Gtk.ToolButton.new_from_stock(Gtk.STOCK_COPY),
- Gtk.ToolButton.new_from_stock(Gtk.STOCK_PASTE),
- Gtk.ToolButton.new_from_stock(Gtk.STOCK_FIND),
- Gtk.ToolButton.new_from_stock(Gtk.STOCK_FIND_AND_REPLACE),
- Gtk.ToolButton.new_from_stock(Gtk.STOCK_PREFERENCES)]
-
- for i in button:
- self.notebook.append_page(Gtk.Box(spacing=6), i)
-
- button[0].connect("clicked", self.on_new)
- button[1].connect("clicked", self.on_open_file)
- button[2].connect("clicked", self.on_save)
- button[9].connect("clicked", self.on_find)
- button[-1].connect("clicked", self.on_preferences)
-
- def create_menubar(self):
- """建立一个菜单栏"""
- mb = Gtk.MenuBar()
-
- #文件菜单
- filemenu = Gtk.Menu()
-
- new = Gtk.MenuItem.new_with_label("新建")
- filemenu.append(new)
-
- openitem = Gtk.MenuItem.new_with_label("打开")
- filemenu.append(openitem)
-
- sep = Gtk.SeparatorMenuItem()
- filemenu.append(sep)
-
- save = Gtk.MenuItem.new_with_label("保存")
- filemenu.append(save)
-
- filem = Gtk.MenuItem.new_with_label("文件")
- filem.set_submenu(filemenu)
-
- mb.set_vexpand(False)
- mb.append(filem)
-
- #查看菜单
- viewmenu = Gtk.Menu()
-
- wrap = Gtk.MenuItem.new_with_label("自动换行")
- viewmenu.append(wrap)
-
- viewm = Gtk.MenuItem.new_with_label("查看")
- viewm.set_submenu(viewmenu)
-
- mb.set_vexpand(False)
- mb.append(viewm)
- self.box.add(mb)
-
-
- def create_notebook(self):
- """建立选项卡"""
- self.notebook = Gtk.Notebook()
- self.box.add(self.notebook)
-
- def on_new(self, widget = None):
- """在这里新建一个文档"""
- new_textview = BEditDocument()
- new_textview.connect("changed", self.on_update_title)
- new_scrolledwindow = Gtk.ScrolledWindow()
- new_scrolledwindow.set_hexpand(True)
- new_scrolledwindow.set_vexpand(True)
- new_scrolledwindow.add(new_textview)
-
- new_label = Gtk.Label("无标题文档")
- self.notebook.append_page(new_scrolledwindow, new_label)
- #切换到刚插入的一页
- self.show_all()
- self.notebook.set_current_page(self.notebook.get_n_pages()-1)
-
- def on_open_file(self, widget):
- dialog = Gtk.FileChooserDialog("打开文件", self,
- Gtk.FileChooserAction.OPEN,
- (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
- Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
-
- self.add_filters(dialog)
-
- response = dialog.run()
- if response == Gtk.ResponseType.OK:
- # 判断最后一个标签里面是否是空文档
- last_doc = self.notebook.get_nth_page(-1).get_child()
- if last_doc.filename != "无标题文档" or last_doc.change_number > 0 or last_doc.stext != "":
- self.on_new()
- last_doc = self.notebook.get_nth_page(-1).get_child()
- last_doc.open(dialog.get_filename())
- self.history.append(dialog.get_filename())
- print self.wrap_mode
- self.save_config()
-
- dialog.destroy()
-
- def on_save(self, widget = None):
- cur_wid = self.notebook.get_nth_page(self.notebook.get_current_page()).get_child()
- # 判断文件是否有文件名
- if cur_wid.filepath == "":
- dialog = Gtk.FileChooserDialog("另存为", self,
- Gtk.FileChooserAction.SAVE,
- (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
- Gtk.STOCK_SAVE, Gtk.ResponseType.OK))
-
- self.add_filters(dialog)
-
- response = dialog.run()
- if response == Gtk.ResponseType.OK:
- cur_wid.save(dialog.get_filename())
- self.history.append(dialog.get_filename())
- self.save_config()
- dialog.destroy()
- else:
- # 直接使用已经存在的文件名
- filename = cur_wid.save()
- self.history.append(filename)
- self.save_config()
-
-
- def on_find(self, widget = None):
- dialog = SearchDialog(self)
- response = dialog.run()
- if response == Gtk.ResponseType.OK:
- cursor_textbuffer = self.notebook.get_nth_page(self.notebook.get_current_page()).get_child().get_buffer()
- cursor_mark = cursor_textbuffer.get_insert()
- start = cursor_textbuffer.get_iter_at_mark(cursor_mark)
- if start.get_offset() == self.textbuffer.get_char_count():
- start = self.textbuffer.get_start_iter()
-
- self.search_and_mark(dialog.entry.get_text(), start)
-
- dialog.destroy()
-
- def on_preferences(self, widget = None):
- print """显示属性对话框"""
-
- def on_update_title(self, widget = None):
- """更新标题栏"""
- cw = self.notebook.get_nth_page(self.notebook.get_current_page())
- cwt = cw.get_child()
- if cwt.change_number == 0:
- self.set_title(cwt.filename+" ("+cwt.filepath+") - bedit")
- new_label = Gtk.Label(cwt.filename)
- self.notebook.set_tab_label(cw, new_label)
- else:
- self.set_title("*" + cwt.filename+" ("+cwt.filepath+") - bedit")
- new_label = Gtk.Label("*" + cwt.filename)
- self.notebook.set_tab_label(cw, new_label)
- new_label.set_name("BEditTabLabel")
-
- def add_filters(self, dialog):
- filter_text = Gtk.FileFilter()
- filter_text.set_name("文本文件")
- filter_text.add_mime_type("text/plain")
- dialog.add_filter(filter_text)
-
- filter_py = Gtk.FileFilter()
- filter_py.set_name("Python 文件")
- filter_py.add_mime_type("text/x-python")
- dialog.add_filter(filter_py)
-
- filter_any = Gtk.FileFilter()
- filter_any.set_name("所有文件")
- filter_any.add_mime_type("*")
- dialog.add_filter(filter_any)
-
- def set_style(self):
- """用来设置外观"""
-
- css = """
- GtkWindow {
- background-image: url('/home/liu/.local/share/gedit/plugins/wallpaper_74694_1024.jpg');
- }
- GtkNotebook {
- background-color: RGBA(224,224,224,0.5);
- }
- GtkScrolledWindow , GtkSourceView {
- background-color: RGBA(224,0,0,0);
- }
- GtkSourceView:selected { background-color: #C80; }
- """
- style_provider = Gtk.CssProvider()
- style_provider.load_from_data(css)
- Gtk.StyleContext.add_provider_for_screen(
- Gdk.Screen.get_default(),
- style_provider,
- Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
- )
-
- def load_config(self):
- file_object = open(os.environ['HOME']+"/.local/share/bedit/config")
- try:
- for eachline in file_object:
- els = eachline.split()
- if els[0] == "wrap_mode":
- if els[1] == "NONE":
- self.wrap_mode = Gtk.WrapMode.NONE
- if els[1] == "CHAR":
- self.wrap_mode = Gtk.WrapMode.CHAR
- if els[1] == "WORD":
- self.wrap_mode = Gtk.WrapMode.WORD
- if els[1] == "WORD_CHAR":
- self.wrap_mode = Gtk.WrapMode.WORD_CHAR
- else:
- self.history.append(els[0])
- print self.history
- finally:
- file_object.close()
-
- def save_config(self):
- file_object = open(os.environ['HOME']+"/.local/share/bedit/config","w")
- try:
- if self.wrap_mode == Gtk.WrapMode.NONE:
- file_object.write("wrap_mode NONE\n")
- elif self.wrap_mode == Gtk.WrapMode.CHAR:
- file_object.write("wrap_mode CHAR\n")
- elif self.wrap_mode == Gtk.WrapMode.WORD:
- file_object.write("wrap_mode WORD\n")
- elif self.wrap_mode == Gtk.WrapMode.WORD_CHAR:
- file_object.write("wrap_mode WORD_CHAR\n")
- for eachline in self.history:
- file_object.write(eachline+'\n')
- finally:
- file_object.close()
-
- class BEditDocument(GtkSource.View):
- """为 GtkSource.View 添加一些原来没有的东西。"""
- def __init__(self):
- GtkSource.View.__init__(self)
- self.filename = "无标题文档"
- self.filepath = ""
- self.stext = ""
- self.change_number = 0
-
- #修改后的信号
- self.get_buffer().connect("changed", self.text_change)
- #self.load_config()
-
-
- def text_change(self, buffer):
- text = buffer.get_text(buffer.get_start_iter(), buffer.get_end_iter(), False)
- if self.stext == text:
- if self.change_number > 0:
- self.change_number = 0
- #发出改变标题栏的信号
- self.emit('changed')
- #如果被修改了
- else:
- self.change_number = self.change_number+1
- if self.change_number == 1:
- self.emit('changed')
-
- def open(self, filename):
- # 在这里读取文件
- file_object = open(filename)
- try:
- self.stext = file_object.read()
- self.get_buffer().set_text(self.stext)
- # 将绝对路径分解成路径和文件名
- self.filepath , self.filename = os.path.split(filename)
- self.emit('changed')
- finally:
- file_object.close()
-
- def save(self, filename = ""):
- """在这里保存文件"""
- if filename == "":
- file_object = open(self.filepath + "/" + self.filename, 'w')
- else:
- file_object = open(filename, 'w')
- self.filepath , self.filename = os.path.split(filename)
- try:
- self.stext = self.get_buffer().get_text(self.get_buffer().get_start_iter(), self.get_buffer().get_end_iter(), False)
- file_object.write(self.stext)
- # 将绝对路径分解成路径和文件名
- self.change_number = 0
- self.emit('changed')
- finally:
- file_object.close()
-
- return self.filepath + "/" + self.filename
-
- def load_config(self):
- """读取配置文件"""
- file_object = open(os.environ['HOME']+"/.local/share/bedit/config")
- try:
- for eachline in file_object:
- els = eachline.split()
- if els[0] == "wrap_mode":
- if els[1] == "NONE":
- self.set_wrap_mode(Gtk.WrapMode.NONE)
- if els[1] == "CHAR":
- self.set_wrap_mode(Gtk.WrapMode.CHAR)
- if els[1] == "WORD":
- self.set_wrap_mode(Gtk.WrapMode.WORD)
- if els[1] == "WORD_CHAR":
- self.set_wrap_mode(Gtk.WrapMode.WORD_CHAR)
- finally:
- file_object.close()
-
-
- if __name__ == "__main__":
- win = TextViewWindow()
- win.connect("delete-event", Gtk.main_quit)
- #win.show_all()
- Gtk.main()
复制代码 註釋掉了你的一些代碼,然後加上了菜單,不過菜單位置不太對,懶得改了直接給你吧 |
|