【发布时间】:2011-10-28 09:53:23
【问题描述】:
我正在用 wxpython 制作一个简单的文本编辑器。我希望它能够编辑诸如 python 之类的代码,因此我希望它以类似于 IDLE 或 Notepad++ 的方式突出显示文本。我知道如何突出显示它,但我想要运行它的最佳方式。我不知道这是否可能,但我真正想要的是在按下一个键时运行,而不是在循环中检查它是否被按下,以便节省处理时间。
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()
【问题讨论】:
-
当一个键被按下时你想运行什么?突出显示?
-
我希望它突出显示字符串中的关键字,以便突出显示 python 代码。所以“如果”将是紫色文本,并且不同的功能也会被着色。如果输入了一个单词,我知道如何使我的代码突出显示文本。如果 textctrl 中的文本发生更改,我希望它通过一段代码运行。因此,如果我在窗口的文本框中输入“我喜欢苹果”并将其更改为“我喜欢苹果派”,由于“派”中的每次按键,它都会运行代码块 4 次。跨度>
-
嗯,减少这种情况的一种方法可能是仅在按下空格键或回车键时运行突出显示代码。因为这表明一个单词或一行是完整的。
-
听起来不错,但是在输入 textctrl 时如何判断是否按下了空格键或回车键。
-
您当前没有在文本控件中按下键时运行高亮代码吗?只需检查事件的关键代码。如果是空格或回车对应的键码,则运行你的highlighing,否则通过。
标签: text wxpython highlighting