2011-08-16 80 views
0

我在wxpython中做了一個簡單的文本編輯器。我希望它能夠編輯諸如python之類的代碼,因此我希望它能夠以與IDLE或Notepad ++類似的方式突出顯示文本。我知道我會如何突出它,但我想要運行它的最佳方式。我不知道是否有可能,但我真正喜歡的是在按下按鍵時運行,而不是在循環檢查按下,以便節省處理時間。wxpython當textctrl發生變化時運行

import wx 
class MainWindow(wx.Frame): 
    def __init__(self, parent, title): 
     wx.Frame.__init__(self, parent, title=title, size=(500,600)) 
     style = wx.TE_MULTILINE|wx.BORDER_SUNKEN|wx.TE_RICH2 
     self.status_area = wx.TextCtrl(self, -1, 
            pos=(10, 270),style=style, 
            size=(380,150)) 
     self.status_area.AppendText("Type in your wonderfull code here.") 
     fg = wx.Colour(200,80,100) 
     at = wx.TextAttr(fg) 
     self.status_area.SetStyle(3, 5, at) 
     self.CreateStatusBar() # A Statusbar in the bottom of the window 

     # Setting up the menu. 
     filemenu= wx.Menu() 

     filemenu.Append(wx.ID_ABOUT, "&About","Use to edit python code") 
     filemenu.AppendSeparator() 
     filemenu.Append(wx.ID_EXIT,"&Exit"," Terminate the program") 

     # Creating the menubar. 
     menuBar = wx.MenuBar() 
     menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar 
     self.SetMenuBar(menuBar) # Adding the MenuBar to the Frame content. 
     self.Show(True) 


app = wx.App(False) 
frame = MainWindow(None, "Python Coder") 
app.MainLoop() 

如果一個循環需要什麼將使它循環的最好辦法,while循環,或

def Loop(): 
    <code> 
    Loop() 

我的新代碼添加綁定:

import wx 

class MainWindow(wx.Frame): 
    def __init__(self, parent, title): 
     wx.Frame.__init__(self, parent, title=title, size=(500,600)) 
     style = wx.TE_MULTILINE|wx.BORDER_SUNKEN|wx.TE_RICH2 
     self.status_area = wx.TextCtrl(self, -1, 
            pos=(10, 270),style=style, 
            size=(380,150)) 
     #settup the syntax highlighting to run on a key press 
     self.Bind(wx.EVT_CHAR, self.onKeyPress, self.status_area) 
     self.status_area.AppendText("Type in your wonderfull code here.") 
     fg = wx.Colour(200,80,100) 
     at = wx.TextAttr(fg) 
     self.status_area.SetStyle(3, 5, at) 
     self.CreateStatusBar() # A Statusbar in the bottom of the window 

     # Setting up the menu. 
     filemenu= wx.Menu() 

     filemenu.Append(wx.ID_ABOUT, "&About","Use to edit python code") 
     filemenu.AppendSeparator() 
     filemenu.Append(wx.ID_EXIT,"&Exit"," Terminate the program") 

     # Creating the menubar. 
     menuBar = wx.MenuBar() 
     menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar 
     self.SetMenuBar(menuBar) # Adding the MenuBar to the Frame content. 
     self.Show(True) 


    def onKeyPress (self, event): 
     print "KEY PRESSED" 
     kc = event.GetKeyCode() 
     if kc == WXK_SPACE or kc == WXK_RETURN: 
      Line = self.status_area.GetValue() 
      print Line 
app = wx.App(False) 
frame = MainWindow(None, "Python Coder") 
app.MainLoop() 
+0

當按下某個鍵時,您想要運行什麼?突出顯示? – arunkumar

+0

我希望它突出顯示字符串中的關鍵詞,以便突出顯示python代碼。所以「如果」將以紫色文本顯示,並且不同的功能也將被着色。我知道如何讓我的代碼突出顯示文本,如果輸入一個單詞。如果textctrl中的文本發生更改,我希望它通過一段代碼運行。因此,如果我在窗口的文本框中輸入了「我喜歡蘋果」,並將它改爲「我喜歡蘋果派」,由於每次按下「餅」,它將運行一段代碼4次。 – drfrev

+0

那麼減少這種情況的一種方法是,只有在按下空格鍵或回車鍵時纔可以運行突出顯示的代碼。因爲那將表明一個詞或一行是完整的。 – arunkumar

回答

0

在您的主窗口__init__功能中加入這個

self.Bind(wx.EVT_CHAR, self.onKeyPress, self.status_area) 

然後在主窗口

def onKeyPress (self, event): 
    kc = event.GetKeyCode() 
    if kc == WXK_SPACE or kc == WXK_RETURN: 
     #Run your highlighting code here 
定義onkeypress事件

試想想它,這可能不是這樣的代碼高亮的最有效的方式。讓我看看這個。但在此期間你可以試試這個。

編輯: 看看這個 - StyledTextCtrl。我認爲它更符合你需要的內容。

+0

我嘗試了你給我的東西,但是當我調試它時,我無法讓它做任何事情。 – drfrev

+0

嘗試StyledTextCtrl? – arunkumar

+0

我知道如何爲文本着色,我只是需要它在按下鍵盤上的按鍵時運行,這就是您的代碼看起來像它的樣子,但我把「打印」測試「」你有#運行突出顯示的代碼在這裏,當我打字時,它從不打印。 StyledTextCtrl對我來說看起來太複雜了。 – drfrev

相關問題