日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python wx提示框字体_使用wxStyledTextCtrl实现代码提示

發布時間:2023/12/4 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python wx提示框字体_使用wxStyledTextCtrl实现代码提示 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

wxStyledTextCtrl是wxPython對流行的Scintilla的包裝,Scintilla的網站(http://www.scintilla.org/),

wxStyledTextCtrl是一個功能強大的富文本編輯控件,常見的編輯器功能都能找到,包括代碼高亮,搜索替換,拷貝粘貼,回退等,后續我將逐步提供說明和例子代碼,以供大家參考。今天先說代碼提示。

創建Frame

首先創建Frame,這個Frame由工具條和wxStyledTextCtrl組成。

class myFrame(wx.Frame):

def __init__(self,parent,title):

wx.Frame.__init__(self,parent,title=title,size=(800,600))

#創建工具條

tb=wx.Frame.CreateToolBar(self,style=wx.TB_FLAT|wx.TB_HORIZONTAL)

tb.AddTool(201,u"回退",wx.Bitmap("./icos/undo.bmp"))

tb.AddTool(202,u"重做",wx.Bitmap("icos/redo.bmp"))

tb.AddSeparator()

tb.AddTool(101,u"拷貝",wx.Bitmap("icos/copy.bmp"))

tb.AddTool(102,u"粘貼",wx.Bitmap("icos/paste.bmp"))

tb.AddTool(103,u"剪貼",wx.Bitmap("icos/cut.bmp"))

tb.AddTool(104,u"搜索",wx.Bitmap("icos/search.bmp"))

tb.Realize()

#在工具條下方創建StyledTextCtrl編輯控件

self.control=stc.StyledTextCtrl(self,style=0)

#創建一個用于代碼提示的AutoComplete對象

self._autocomplete=AutoComplete()

#綁定工具條事件和處理函數

tb.Bind(wx.EVT_TOOL,self.OnToolSelected)

#綁定按鍵事件

self.control.Bind(wx.EVT_KEY_DOWN,self.OnKeyPressed)

self.control.Bind(wx.EVT_CHAR,self.OnChar)

self.Show(True)

因為代碼提示是在鍵入的過程中發生的,所以需要跟蹤鍵盤事件,所以把按鍵事件與處理函數做了綁定。

AutoComplete類

在代碼提示過程中需要動態根據用戶輸入提供建議,所以創建了一個_autocomplete對象,它的類代碼:

class AutoComplete:

def __init__(self):

self.suggests=keyword.kwlist

self.prefix=""

self.key=""

def append(self,char):

self.prefix+=char

def back(self):

if len(self.prefix)>1:

self.prefix=self.prefix[:-1]

else:

self.prefix=""

def clear(self):

self.prefix=""

@property

def length(self):

return len(self.prefix)

def getsugs(self):

return [word for word in self.suggests if word.startswith(self.prefix)]

這個類維護一個用戶當前已輸入的字符串,用戶每輸入一個字符都附加在字符串后面,每次輸入退格鍵,字符串刪掉最后的字符。getsugs()函數根據字符串在關鍵字列表中查找匹配的候選關鍵字,并返回一個推薦列表。

跟蹤鍵盤事件

def OnKeyPressed(self,evt):

key=evt.GetKeyCode()

control=evt.ControlDown()

alt=evt.AltDown()

shift=evt.ShiftDown()

if key in self.AutoCompStopwords and not control and not alt and not shift:

if self.control.AutoCompActive():

self.control.AutoCompComplete()

self._autocomplete.clear()

elif key==wx.WXK_BACK and not control and not alt and not shift:

self._autocomplete.back()

self.code_complete()

evt.Skip()

def OnChar(self,evt):

control = evt.ControlDown()

alt = evt.AltDown()

shift = evt.ShiftDown()

try:

char = chr(evt.GetUnicodeKey())

if char.isprintable() and char!=" ":

self._autocomplete.append(char)

except ValueError:

#self._autocomplete.clear()

if self.control.AutoCompActive():

self.control.AutoCompCancel()

if not control and not alt and not shift:

self.code_complete()

evt.Skip()

我們提供了兩個跟蹤鍵盤事件的函數,它們之間還是有些區別的。主要目的是區分可打印字符與功能鍵。AutoCompStopwords就是指定遇到什么樣的按鍵代碼提示結束,常見的包括回車、空格、分號、點號等。代碼提示結束有兩種情況,一種是用戶在列表中選擇其中一項,填入到編輯器中,另一種是用戶輸入的內容匹配不到任何關鍵字。

每個處理函數后面都加上了evt.Skip()目的是不要打破系統的消息處理機制,使得系統有時間處理必要的消息。有點像以前VB時代的DoEvents。

在遇到AutoCompStopwords后,需要把_autocomplete中的字符串清空,等待下一個詞的編輯。這兩個事件處理代碼中都調用了code_complete()函數,其代碼:

def code_complete(self):

choices=self._autocomplete.getsugs()

if choices and self._autocomplete.length>=1:

choices.sort()

self.control.AutoCompShow(self._autocomplete.length-1," ".join(choices))

else:

if self.control.AutoCompActive():

self.control.AutoCompCancel()

就是獲取推薦列表,并顯示在編輯器中。AutoCompShow(nlength,suggents),其中第一個參數nlength是指匹配多少個字符開始顯示,suggents是一個分隔符隔開的推薦列表字符串,分隔符可以設置,不過一般保持缺省就可以了,缺省是空格。

建立主程序

建立一個主程序的入口,并建立消息循環機制。這些都是wxPython的規定動作。

if __name__=="__main__":

app=wx.App(False)

frame=myFrame(None,'Simple Editor')

app.MainLoop()

總結

以上是生活随笔為你收集整理的python wx提示框字体_使用wxStyledTextCtrl实现代码提示的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。