我在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()
當按下某個鍵時,您想要運行什麼?突出顯示? – arunkumar
我希望它突出顯示字符串中的關鍵詞,以便突出顯示python代碼。所以「如果」將以紫色文本顯示,並且不同的功能也將被着色。我知道如何讓我的代碼突出顯示文本,如果輸入一個單詞。如果textctrl中的文本發生更改,我希望它通過一段代碼運行。因此,如果我在窗口的文本框中輸入了「我喜歡蘋果」,並將它改爲「我喜歡蘋果派」,由於每次按下「餅」,它將運行一段代碼4次。 – drfrev
那麼減少這種情況的一種方法是,只有在按下空格鍵或回車鍵時纔可以運行突出顯示的代碼。因爲那將表明一個詞或一行是完整的。 – arunkumar