Is there a mechanism that detects (highlight and generate mouse event) a file or a folder path in a wx.TextCtrl?
A similar mechanism exists for URL detection (using style = wx.TE_AUTO_URL with wx.TE_RICH).
Example:
"This is an example how I want TextCtrl to detect C:\Temp\Folder\temp.txt as a path."
Thanks, Omer.
To the best of my knowledge, there is no built-in style that would do what you want. However, you can always subclass wx.TextCtrl.
The custom widget provided below changes the style for string subsections matching a regular expression. When the cursor hovers over one of these substrings, the custom command event "EVT_TXT_PATH" is generated.
The regex may need adjusting depending on what paths you want the control to recognize.
I grabbed one from the regular expression library that might work for you:
http://regexlib.com/REDetails.aspx?regexp_id=127
import wx
import wx.lib.newevent
import re
re_path = re.compile(r'([A-Z]:\\[^/:\*\?<>\|]+\.\w{2,6})|(\\{2}[^/:\*\?<>\|]+\.\w{2,6})')
TextPathEvent, EVT_TXT_PATH = wx.lib.newevent.NewCommandEvent()
class PathTextCtrl(wx.TextCtrl):
def __init__(self, parent, id=wx.ID_ANY, value="",
pos=wx.DefaultPosition, size=wx.DefaultSize,
style=0, validator=wx.DefaultValidator,
name=""):
style = style | wx.TE_RICH
super(PathTextCtrl, self).__init__(parent, id, value, pos, size,
style,validator, name)
self.groups = []
self.SetDefaultStyle(wx.TextAttr(wx.BLACK, wx.NullColor))
self.Bind(wx.EVT_TEXT, self.OnText, self)
self.Bind(wx.EVT_MOTION, self.OnMotion, self)
def OnText(self, evt):
self.StyleText()
evt.Skip()
def StyleText(self):
self.groups = []
string = self.GetValue()
n = len(string)
defstyle = self.GetDefaultStyle()
self.SetStyle(0, n, defstyle)
font = self.GetFont()
font.SetUnderlined(True)
style = wx.TextAttr(colText=wx.BLUE, font=font)
index = 0
while True:
match = re_path.search(string, index, n)
if match:
start, end = match.span()
self.groups.append((start, end))
self.SetStyle(start, end, style)
index = end+1
else: return
def OnMotion(self, evt):
pos = evt.GetPosition()
result = self.HitTestPos(pos)
for group in self.groups:
x, y = group
if x <= result[1] <= y:
string = self.GetValue()[x:y]
print string
event = TextPathEvent(id=self.GetId(), url=string)
wx.PostEvent(self, event)
evt.Skip()
class TestFrame(wx.Frame):
def __init__(self, parent, id=wx.ID_ANY, title=""):
super(TestFrame, self).__init__(parent, id, title)
panel = wx.Panel(self)
text = PathTextCtrl(panel, size=(400, -1))
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(text, 0, wx.ALL, 10)
panel.SetSizer(sizer)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(panel)
self.SetSizer(sizer)
self.Fit()
if __name__ == '__main__':
app = wx.App(redirect=True)
top = TestFrame(None, title="PathTextCtrl Demonstration")
top.Show()
app.MainLoop()
Related
I would want to have a ComboBox with autocomplete as I have a list of more than 1000 items and would like to be able to select one element without having to go through the whole list, by only having part of the item string in the ComboBox.
I have been looking around and the question has been answered multiple times and I even checked the following link from a previous question here:
Auto-Completion In wxPython wxComboBox
and this other link:
https://github.com/RajaS/ACTextCtrl
However, when I try to run the example codes I always get the error: "module 'wx' has no attribute 'SimpleHtmlListBox'/'HtmlListBox' ".
What might be the reason for the error? And are there maybe other ways to achieve an autocomplete ComboBox?
I have converted the code in the first link you gave to make it work in python 3, wxPython Phoenix. Some changes has been added to make the combo box work better. The first comboBox has been tested it in Mac OSX. But when I tested it Linux gtk+, it doesn't work as expected so I created a work around in second line with a TextCtrl as a filter for comboBox.
import wx
class PromptingComboBox(wx.ComboBox) :
def __init__(self, parent, choices=[], style=0, **par):
wx.ComboBox.__init__(self, parent, wx.ID_ANY, style=style|wx.CB_DROPDOWN, choices=choices, **par)
self.choices = choices
self.Bind(wx.EVT_TEXT, self.OnText)
self.Bind(wx.EVT_KEY_DOWN, self.OnPress)
self.ignoreEvtText = False
self.deleteKey = False
def OnPress(self, event):
if event.GetKeyCode() == 8:
self.deleteKey = True
event.Skip()
def OnText(self, event):
currentText = event.GetString()
if self.ignoreEvtText:
self.ignoreEvtText = False
return
if self.deleteKey:
self.deleteKey = False
if self.preFound:
currentText = currentText[:-1]
self.preFound = False
for choice in self.choices :
if choice.startswith(currentText):
self.ignoreEvtText = True
self.SetValue(choice)
self.SetInsertionPoint(len(currentText))
self.SetTextSelection(len(currentText), len(choice))
self.preFound = True
break
class TrialPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, wx.ID_ANY)
choices = ['grandmother', 'grandfather', 'cousin', 'aunt', 'uncle', 'grandson', 'granddaughter']
for relative in ['mother', 'father', 'sister', 'brother', 'daughter', 'son']:
choices.extend(self.derivedRelatives(relative))
self.choices = choices = sorted(choices)
mainSizer = wx.FlexGridSizer(2, 2, 5, 10)
self.SetSizer(mainSizer)
mainSizer.Add(wx.StaticText(
self, -1, "Worked in Mac - python 3 - wx phoenix"))
cb1 = PromptingComboBox(self, choices=choices)
mainSizer.Add(cb1)
mainSizer.Add(wx.StaticText(self, -1, "Work arround in Linux-gtk"))
sizer2 = wx.BoxSizer(wx.HORIZONTAL)
mainSizer.Add(sizer2)
filterCtrl = wx.TextCtrl(self, -1, size=(150, -1))
filterCtrl.Bind(wx.EVT_TEXT, self.OnFilter)
sizer2.Add(filterCtrl)
self.cb2 = wx.ComboBox(self, -1, size=(150, -1), choices=choices)
sizer2.Add(self.cb2)
def derivedRelatives(self, relative):
return [relative, 'step' + relative, relative + '-in-law']
def OnFilter(self, event):
currentText = event.GetString().upper()
tmpChoices = [c for c in self.choices if c.startswith(currentText)]
if tmpChoices != []:
self.cb2.SetItems(tmpChoices)
self.cb2.SetValue(tmpChoices[0])
else:
self.cb2.SetValue('')
self.cb2.SetItems([])
if __name__ == '__main__':
app = wx.App(False)
frame = wx.Frame (None, -1, 'Demo PromptingComboBox Control and Work around',
size=(700, 400))
TrialPanel(frame)
frame.Show()
app.MainLoop()
I am trying to couple wx.ListBox with wx.combo.Comboctrl. A sample code is below. For some reason, the items in the ListBox are not clickable/selectable. I wonder how I can make it work. Thanks!
EDIT: Missing code added
import wx, wx.combo
class MainFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, title="", size=(300, 100))
gbs = wx.GridBagSizer()
ComboCtrlBox = wx.combo.ComboCtrl(self)
ComboCtrlPopup = ListBoxComboPopup()
ComboCtrlBox.SetPopupControl(ComboCtrlPopup)
ComboCtrlPopup.ListBox.Append("Apple")
ComboCtrlPopup.ListBox.Append("Banana")
ComboCtrlPopup.ListBox.Append("Orange")
ComboCtrlPopup.ListBox.Bind(wx.EVT_LISTBOX, self.OnSelect) #ADDED
gbs.Add(ComboCtrlBox, pos = (0, 0), span = (1, 1), flag = wx.EXPAND|wx.ALL, border = 10)
gbs.AddGrowableCol(0)
self.SetSizer(gbs)
self.Layout()
def OnSelect(self, evt): #ADDED
print "HAHA"
class ListBoxComboPopup(wx.combo.ComboPopup):
def Init(self):
self.ItemList = []
def Create(self, parent):
self.ListBox = wx.ListBox(parent, -1, size = (-1, 20), choices = self.ItemList)
def GetControl(self):
return self.ListBox
def OnPopup(self):
pass
#-----------------------------------------------------------------------------#
if __name__ == '__main__':
APP = wx.App(False)
FRAME = MainFrame(None)
FRAME.Show()
APP.MainLoop()
You are missing a few things in your ListBoxComboPopup class needed to make it work well with the ComboCtrl. At a minimum you are missing some event binding and handler to catch selection events from the ListBox, and an implementation of the GetStringValue method which the combo will call to get the value to be displayed. Please see the ComboCtrl sample in the wxPython demo for more details and example code.
as in the title, is it possible to add a custom event listener for wx widget and if so, how? More particularly I am trying to add one which listens to the changes of SelStart and SelEnd attributes of wx.Slider.
Thanks!
Below is the code for custom listener for wx.slider, I am calling it inside other listener's handler.
You can call this customize listener from the point where SelStart and SelEnd changing.
import wx
import wx.lib.newevent
DialogRespondEvent, EVT_DIALOG_RESPOND_EVENT = wx.lib.newevent.NewEvent()
class Mywin(wx.Frame):
def __init__(self, parent, title):
super(Mywin, self).__init__(parent, title = title,size = (250,150))
self.InitUI()
def InitUI(self):
pnl = wx.Panel(self)
vbox = wx.BoxSizer(wx.VERTICAL)
self.Bind(EVT_DIALOG_RESPOND_EVENT, self.testing)
self.sld = wx.Slider(pnl, value = 10, minValue = 1, maxValue = 100,
style = wx.SL_HORIZONTAL|wx.SL_LABELS)
vbox.Add(self.sld,1,flag = wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL | wx.TOP, border = 20)
self.sld.Bind(wx.EVT_SLIDER, self.OnSliderScroll)
self.txt = wx.StaticText(pnl, label = 'Hello',style = wx.ALIGN_CENTER)
vbox.Add(self.txt,1,wx.ALIGN_CENTRE_HORIZONTAL)
pnl.SetSizer(vbox)
self.Centre()
self.Show(True)
def testing(self,evt):
print "you can do whatever you want here"
def OnSliderScroll(self, e):
obj = e.GetEventObject()
val = obj.GetValue()
font = self.GetFont()
font.SetPointSize(self.sld.GetValue())
self.txt.SetFont(font)
evt = DialogRespondEvent()
wx.PostEvent(self, evt)
ex = wx.App()
Mywin(None,'Slider demo')
ex.MainLoop()
I have a listbox with a set of strings. The set of strings I want to display depends on which radio button is selected. I would like it such that while the user is interacting with the Form, if they ever change the radio button it will update the list box.
Here is my code (I'm leaving the array for t87 and t89 out because they are very long (assume they exist):
def OnBtnSuperTesting(self, event):
class MainWindow(wx.Frame):
def __init__(self, parent, title):
self.dirname=''
wx.Frame.__init__(self, parent, title=title, size=(320,440))
self.SetBackgroundColour(wx.WHITE)
self.CenterOnScreen()
self.CreateStatusBar()
self.radioT89 = wx.RadioButton(self, -1, 'T89 only', pos = (2,0), style = wx.RB_GROUP)
self.radioT87 = wx.RadioButton(self, -1, 'T87 only', pos = (154, 0))
self.radioKeySort = wx.RadioButton(self, -1, 'Sort by Key', pos = (2,40), style = wx.RB_GROUP)
self.radioAtoZ = wx.RadioButton(self, -1, 'Sort Name A-Z', pos = (2,60))
self.radioZtoA = wx.RadioButton(self, -1, 'Sort Name Z-A', pos = (2,80))
self.checkCode = wx.CheckBox(self, -1, 'Generate Code', pos = (154,40))
self.checkBuild = wx.CheckBox(self, -1, 'Generate Build Report', pos = (154, 60))
self.ln = wx.StaticLine(self, -1, pos = (0,15), size = (300,3), style = wx.LI_HORIZONTAL)
self.ln2 = wx.StaticLine(self, -1, pos = (150,15), size = (3,100), style = wx.LI_VERTICAL)
self.radioT87.Bind(wx.EVT_RADIOBUTTON, self.updateList)
#self.Bind(wx.EVT_RADIOBUTTON, self.radioT89, self.updateList())
self.listbox = wx.ListBox(self, -1, pos = (0,120), size = (300,200), choices = T89, style = (wx.LB_SINGLE|wx.LB_HSCROLL))
self.go = wx.Button(self,-1, label = 'Go!', pos = (110, 325))
# Setting up the menu.
filemenu= wx.Menu()
menuAbout= filemenu.Append(wx.ID_ABOUT, "&About"," Information about this program")
menuExit = filemenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program")
# Creating the menubar.
menuBar = wx.MenuBar()
menuBar.Append(filemenu,"&File")
self.SetMenuBar(menuBar)
# Events.
self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
self.SetAutoLayout(1)
self.Show()
def OnExit(self,e):
self.Close(True) # Close the frame.
def updateList(self):
if self.radioT87.GetValue() == True:
choices = T87
self.listbox.Set(choices)
else:
choices = T89
self.listbox.Set(choices)
app = wx.App(False)
frame = MainWindow(None, "Supervisory Testing")
app.MainLoop()
When you create each radiobutton you can create a bind event. What this does (as you have implemented later on in your code) is execute a command function when the bind event occurs. In your case it would look like this:
self.Bind(wx.EVT_RADIOBUTTON,self.RadioButton,self.DoSomething)
Explanation:
wx.EVT_RADIOBUTTON
This is the event that is triggered when the user changes the Radiobutton's status. It may or may not have attributes.
self.RadioButton
This is the radiobutton which you would like to bind. In your case "self.radioAtoZ" or similar.
self.DoSomething
THis is the callback function. You can make it whatever you want such as:
def DoSomething(self):
if self.radioAtoZ.getStatus():
rearrangeNumbersFromAtoZ
print 'Re-arranged numbers from A to Z'
else:
etc.
EDIT:
self.RadioButton.Bind(EVT_RADIOBUTTON, self.DoSomething)
Your structure for self.DoSomething should be like this:
Class MainWindow:
def __init_(self, parent):
def DoSomething(self):
#dostuff
Also in response to your other comment:
when a function is called within a Bind event, it passes the event to the function by default. In addition, all functions have the "self" arg, thus 2 given args. You need to change the following:
def DoSomething(self, event):
#dostuff
I decided to rewrite the OP's code to demonstrate how to bind 2 RadioButton's to 2 different event handlers and update the ListBox:
import wx
########################################################################
class MyFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="Radios!")
panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.VERTICAL)
self.radioAtoZ = wx.RadioButton(panel, label='Sort Name A-Z',
style = wx.RB_GROUP)
self.radioAtoZ.Bind(wx.EVT_RADIOBUTTON, self.sortAZ)
sizer.Add(self.radioAtoZ, 0, wx.ALL|wx.EXPAND, 5)
self.radioZtoA = wx.RadioButton(panel, label='Sort Name Z-A')
self.radioZtoA.Bind(wx.EVT_RADIOBUTTON, self.sortZA)
sizer.Add(self.radioZtoA, 0, wx.ALL|wx.EXPAND, 5)
choices = ["aardvark", "zebra", "bat", "giraffe"]
self.listbox = wx.ListBox(panel, choices=choices)
sizer.Add(self.listbox, 0, wx.ALL, 5)
panel.SetSizer(sizer)
self.Show()
#----------------------------------------------------------------------
def sortAZ(self, event):
""""""
choices = self.listbox.GetStrings()
choices.sort()
self.listbox.SetItems(choices)
#----------------------------------------------------------------------
def sortZA(self, event):
""""""
choices = self.listbox.GetStrings()
choices.sort()
choices.reverse()
self.listbox.SetItems(choices)
if __name__ == "__main__":
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
You will want to take a look at the following wiki article on the differences of binding it this way versus the other:
http://wiki.wxpython.org/self.Bind%20vs.%20self.button.Bind
Most of the time when you create a group of widgets that do different things, you bind them to different event handlers. If you want to bind all the RadioButtons to the same handler, then you'll probably need to name each widget with a unique name so that when they come to the handler, you can tell which button was pressed. Then you can use a series of if statements to decide what to do to the list box. Here's a tutorial that talks about that sort of thing:
http://www.blog.pythonlibrary.org/2011/09/20/wxpython-binding-multiple-widgets-to-the-same-handler/
I want to implement a grid with the cells that have the following behaviour:
cell text should be wrapped if it doesn't fit to the cell
newlines (\n) in the cell text should be processed as well
i.e. the same behaviour as in table editors like MS Excel, OO Calc, etc. when you enable the 'wrap words' option for cells.
I'm trying to do this as follows:
import wx
import wx.grid
class MyGrid(wx.grid.Grid):
def __init__(self, parent = None, style = wx.WANTS_CHARS):
wx.grid.Grid.__init__(self, parent, -1, style = style)
self.CreateGrid(10, 10)
self.editor = wx.grid.GridCellAutoWrapStringEditor()
self.SetDefaultEditor(self.editor)
self.SetDefaultRenderer(wx.grid.GridCellAutoWrapStringRenderer())
self.SetCellValue(0, 0, "Line1\nLine2\nLine3")
self.SetRowSize(0, 100)
class MyFrame(wx.Frame):
def __init__(self, parent = None, title = "Multiline"):
wx.Frame.__init__(self, parent, -1, title)
self.Bind(wx.EVT_CHAR_HOOK, self.on_frame_char_hook)
panel = wx.Panel(self)
vbox = wx.BoxSizer(wx.VERTICAL)
panel.SetSizer(vbox)
grid = MyGrid(panel)
vbox.Add(grid, 1, wx.EXPAND | wx.ALL, 5)
self.grid = grid
btn_exit = wx.Button(panel, -1, "Exit")
vbox.Add(btn_exit, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALL, 10)
#Proceed CTRL+ENTER as newline in the cell editor
def on_frame_char_hook(self, event):
if event.CmdDown() and event.GetKeyCode() == wx.WXK_RETURN:
if self.grid.editor.IsCreated():
self.grid.editor.StartingKey(event)
else:
event.Skip
else:
event.Skip()
if __name__ == "__main__":
app = wx.PySimpleApp()
f = MyFrame()
f.Center()
f.Show()
app.MainLoop()
But this code doesn't work as expected - newlines processed correctly in the cell editor, but ignored in the cell renderer. If I remove the self.SetDefaultRenderer(wx.grid.GridCellAutoWrapStringRenderer()) then newlines processed correctly both in the editor and renderer, but obviously auto wrapping in the renderer doesn't work.
Does anybody know how to solve this?
Solved this problem by writing a custom renderer:
from wx.lib import wordwrap
import wx.grid
class CutomGridCellAutoWrapStringRenderer(wx.grid.PyGridCellRenderer):
def __init__(self):
wx.grid.PyGridCellRenderer.__init__(self)
def Draw(self, grid, attr, dc, rect, row, col, isSelected):
text = grid.GetCellValue(row, col)
dc.SetFont( attr.GetFont() )
text = wordwrap.wordwrap(text, grid.GetColSize(col), dc, breakLongWords = False)
hAlign, vAlign = attr.GetAlignment()
if isSelected:
bg = grid.GetSelectionBackground()
fg = grid.GetSelectionForeground()
else:
bg = attr.GetBackgroundColour()
fg = attr.GetTextColour()
dc.SetTextBackground(bg)
dc.SetTextForeground(fg)
dc.SetBrush(wx.Brush(bg, wx.SOLID))
dc.SetPen(wx.TRANSPARENT_PEN)
dc.DrawRectangleRect(rect)
grid.DrawTextRectangle(dc, text, rect, hAlign, vAlign)
def GetBestSize(self, grid, attr, dc, row, col):
text = grid.GetCellValue(row, col)
dc.SetFont(attr.GetFont())
text = wordwrap.wordwrap(text, grid.GetColSize(col), dc, breakLongWords = False)
w, h, lineHeight = dc.GetMultiLineTextExtent(text)
return wx.Size(w, h)
def Clone(self):
return CutomGridCellAutoWrapStringRenderer()
For column headers, I was able to insert a \n (newline).
self.m_grid1.SetColLabelValue(8, "Reference \n Level")