wxPython: Check if tree node has focus (TreeCtrl) - python

Do not be intimidated by the code. It is really straightforward. (Run it if you want)
I am trying to figure out a way to do something when a tree node has focus. I know if I want to do something if the TreeCtrl has focus then it would be:
self.tree.Bind(wx.EVT_SET_FOCUS, some_function)
But I don't want that, I want it for tree node. Any help?
For my application, a pop up window would show up for every tree node selected, and disappears if the tree node loses focus.
import wx
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(600, 400))
self.splitter = wx.SplitterWindow(self, -1)
self.leftPanel = wx.Panel(self.splitter, -1)
self.tree = MyTree(self.leftPanel, 1, wx.DefaultPosition, (300, 300))
self.tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelChanged, id=1)
self.rightPanel = wx.Panel(self.splitter, -1)
self.splitter.SplitVertically(self.leftPanel, self.rightPanel)
sizer = wx.BoxSizer ( )
sizer.Add ( self.splitter, 1, wx.EXPAND )
self.SetSizer ( sizer )
self.tree.SetFocus()
def OnSelChanged(self, event):
print 'Sel Changed'
class MyTree(wx.TreeCtrl):
def __init__(self, panel, id, pos, size):
self.panel = panel
wx.TreeCtrl.__init__(self, panel, id, pos=pos, size=size, style=wx.TR_HAS_BUTTONS)
wx.EVT_TREE_ITEM_EXPANDING(self,-1,self.OnItemExpanding)
wx.EVT_TREE_ITEM_COLLAPSED(self,-1,self.OnItemCollapsed)
wx.EVT_SET_FOCUS(self,self.OnGotFocus)
self.root = self.AddRoot('Root')
self.SetPyData(self.root,0)
self.SetItemHasChildren(self.root)
self.Expand(self.root)
self.SetImageList(wx.ImageList(16,16))
def OnItemExpanding(self,evt):
node = evt.GetItem()
data = self.GetPyData(node)
if data:
for i in range(1,2):
leaf = self.AppendItem(node,'%s.%s' % (data,i))
self.SetPyData(leaf,'%s.%s' % (data,i))
else:
for i in range(1,2):
leaf = self.AppendItem(node,'%s' % (i))
self.SetPyData(leaf,'%s' % (i))
self.SetItemHasChildren(leaf)
def OnItemCollapsed(self,evt):
self.DeleteChildren(evt.GetItem())
def OnGotFocus(self,evt):
print 'tree got focus'
evt.Skip()
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, "This is a test")
frame.Show(True)
self.SetTopWindow(frame)
return True
app = MyApp(0)
app.MainLoop()

I believe you want
...
self.tree.Bind(wx.EVT_TREE_SEL_CHANGED,self.OnTreeSelectionChange)
self.tree.Bind(wx.EVT_KILL_FOCUS,self.OnKillTreeFocus)
...
def OnTreeSelectionChange(self, evt):
print "OnSelChanged: ", self.GetItemText(evt.GetItem())

Related

wxPython append and pop message box inside panel serving like a notification center

I would like to make a wxpython program that has a notification center just like the one on windows or mac. Whenever I have a message, the message will show inside the the notification panel, and the user could close that message afterwards.
I have a sample code for illustration as follows:
import wx
import wx.lib.scrolledpanel as scrolled
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title)
topPanel = wx.Panel(self)
panel1 = wx.Panel(topPanel, -1)
button1 = wx.Button(panel1, -1, label="generate message")
self.panel2 = scrolled.ScrolledPanel(
topPanel, -1, style=wx.SIMPLE_BORDER)
self.panel2.SetAutoLayout(1)
self.panel2.SetupScrolling()
button1.Bind(wx.EVT_BUTTON, self.onAdd)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(panel1,-1,wx.EXPAND|wx.ALL,border=10)
sizer.Add(self.panel2,-1,wx.EXPAND|wx.ALL,border=10)
self.sizer2 = wx.BoxSizer(wx.VERTICAL)
topPanel.SetSizer(sizer)
self.panel2.SetSizer(self.sizer2)
def onAdd(self, event):
new_text = wx.TextCtrl(self.panel2, value="New Message")
self.sizer2.Add(new_text,0,wx.EXPAND|wx.ALL,border=1)
self.panel2.Layout()
self.panel2.SetupScrolling()
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, 'frame')
frame.Show(True)
return True
app = MyApp(0)
app.MainLoop()
In the above I code, the right panel (i.e. panel2) serves as a notification center that all the messages should shown inside it. On the left panel (i.e. panel1) I have a button to generate message just to mimic the notification behavior. Ideally the message on the right panel should be a message box that you could close (maybe a frame? Or a MessageDialog?)
Any hint or advice is much appreciated, and an example would be the best!
Thanks!
Finally figured out myself, it was easier than I initially thought.
Here is the code:
import wx
import wx.lib.scrolledpanel as scrolled
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title)
self.number_of_panels = 0
topPanel = wx.Panel(self)
panel1 = wx.Panel(topPanel, -1)
button1 = wx.Button(panel1, -1, label="generate message")
self.panel2 = scrolled.ScrolledPanel(
topPanel, -1, style=wx.SIMPLE_BORDER)
self.panel2.SetAutoLayout(1)
self.panel2.SetupScrolling()
button1.Bind(wx.EVT_BUTTON, self.onAdd)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(panel1,0,wx.EXPAND|wx.ALL,border=5)
sizer.Add(self.panel2,1,wx.EXPAND|wx.ALL,border=5)
self.sizer2 = wx.BoxSizer(wx.VERTICAL)
topPanel.SetSizer(sizer)
self.panel2.SetSizer(self.sizer2)
def onAdd(self, event):
self.number_of_panels += 1
panel_label = "Panel %s" % self.number_of_panels
panel_name = "panel%s" % self.number_of_panels
new_panel = wx.Panel(self.panel2, name=panel_name, style=wx.SIMPLE_BORDER)
self.closeButton = wx.Button(new_panel, label='Close %s' % self.number_of_panels)
self.closeButton.panel_number = self.number_of_panels
self.closeButton.Bind(wx.EVT_BUTTON, self.OnClose)
self.sizer2.Add(new_panel,0,wx.EXPAND|wx.ALL,border=1)
self.panel2.Layout()
self.panel2.SetupScrolling()
def OnClose(self, e):
if self.panel2.GetChildren():
e.GetEventObject().GetParent().Destroy()
self.number_of_panels -= 1
self.panel2.Layout() # Reset layout after destroy the panel
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, 'frame')
frame.Show(True)
return True
app = MyApp(0)
app.MainLoop()
Basically I can destroy the newly created panel. I just need to know which panel it is when I click the close button. This should work very similar to the Notification Center.

wxPython Drag and Drop files onto image, to add to array

I have a simple wxPython application with 1 image "Drop files here!" and 2 buttons.
I want the user to be able to drag and drop files onto the top section/image, at which point the image changes and the files are loaded into an array.
That's all I need but I have hit a major roadblock getting the drag and drop to work. Can someone please take a look at my code and figure out how/where to integrate the Drag and drop event? Any help would be great.
UI image
import wx
class DropTarget(wx.FileDropTarget):
def OnDropFiles(self, x, y, filenames):
print(filenames)
image = Image.open(filenames[0])
image.thumbnail((PhotoMaxSize, PhotoMaxSize))
image.save('thumbnail.png')
pub.sendMessage('dnd', filepath='thumbnail.png')
return True
def __init__(self, parent, ID, title):
wx.FileDropTarget.__init__(self, parent, ID, title, size=(300, 340), style= wx.CLOSE_BOX)
#self.widget = widget
class MyFrame(wx.Frame):
def __init__(self, parent, ID, title):
wx.Frame.__init__(self, parent, ID, title, size=(300, 340), style= wx.CLOSE_BOX)
panel1 = wx.Panel(self,-1, style=wx.SUNKEN_BORDER)
panel2 = wx.Panel(self,-1, style=wx.SUNKEN_BORDER)
panel1.SetBackgroundColour("BLUE")
panel2.SetBackgroundColour("RED")
image_file = 'bgimage1.png'
bmp1 = wx.Image(
image_file,
wx.BITMAP_TYPE_ANY).ConvertToBitmap()
# image's upper left corner anchors at panel
# coordinates (0, 0)
self.bitmap1 = wx.StaticBitmap(
self, -1, bmp1, (0, 0))
# show some image details
str1 = "%s %dx%d" % (image_file, bmp1.GetWidth(),
bmp1.GetHeight())
# button
closeButton = wx.Button(self.bitmap1, label='Generate', pos=(30, 280))
closeButton.Bind(wx.EVT_BUTTON, self.OnClose)
clearButton = wx.Button(self.bitmap1, label='Clear', pos=(170, 280))
clearButton.Bind(wx.EVT_BUTTON, self.OnClose)
box = wx.BoxSizer(wx.VERTICAL)
box.Add(panel1, 5, wx.EXPAND)
box.Add(panel2, 1, wx.EXPAND)
self.SetAutoLayout(True)
self.SetSizer(box)
self.Layout()
def OnDropFiles(self, x, y, filenames):
self.window.updateDisplay(filenames)
for name in filenames:
self.window.WriteText(name + "\n")
print(name)
return True
def OnClose(self, e):
self.Close(True)
app = wx.App()
frame = MyFrame(None, -1, "Sizer Test")
frame.Show()
app.MainLoop()
You have the class DropTarget back to front with the init after the dropfiles. You also need to put the image and buttons on to one of the panels.
See below:
import wx
class DropTarget(wx.FileDropTarget):
def __init__(self, obj):
wx.FileDropTarget.__init__(self)
self.obj = obj
def OnDropFiles(self, x, y, filenames):
print("Drop Event",filenames)
# image = Image.open(filenames[0])
# image.thumbnail((PhotoMaxSize, PhotoMaxSize))
# image.save('new.png')
# pub.sendMessage('dnd', filepath='new.png')
return True
class MyFrame(wx.Frame):
def __init__(self, parent, ID, title):
wx.Frame.__init__(self, parent, ID, title, size=(300, 340))
panel1 = wx.Panel(self,-1, style=wx.SUNKEN_BORDER)
panel2 = wx.Panel(self,-1, style=wx.SUNKEN_BORDER)
panel1.SetBackgroundColour("BLUE")
panel2.SetBackgroundColour("RED")
image_file = 'bgimage1.png'
bmp1 = wx.Image(image_file,wx.BITMAP_TYPE_ANY).ConvertToBitmap()
self.bitmap1 = wx.StaticBitmap(panel1, -1, bmp1, (0, 0))
# button
closeButton = wx.Button(panel2, -1, label='Generate',pos=(30, 280))
closeButton.Bind(wx.EVT_BUTTON, self.OnClose)
clearButton = wx.Button(panel2, -1, label='Clear',pos=(170, 280))
clearButton.Bind(wx.EVT_BUTTON, self.OnClose)
self.file_drop_target = DropTarget(self)
self.SetDropTarget(self.file_drop_target)
box = wx.BoxSizer(wx.VERTICAL)
box.Add(panel1, 0, wx.EXPAND,0)
box.Add(panel2, 0, wx.EXPAND,0)
self.SetAutoLayout(True)
self.SetSizer(box)
self.Layout()
def OnClose(self, e):
self.Close(True)
app = wx.App()
frame = MyFrame(None, -1, "Sizer Test")
frame.Show()
app.MainLoop()
This may not be what you want to achieve but at least it's a startiing point and the drag and drop works.

wxPython : Update the label on Panel with GridBagSizer and Timer

I want to update the panel "label", but I think I am wrong with Refresh/Update/Remove method .
I write 2 python file, the "WriteData.py" would auto-update a txt file, and the "Main.py" want to show the txt value on wx.panel.
I run the 2 python file at the same time, use Timer to auto update data every 3 sec .
And I use the GridBagSizer hope to arrange these panel position.
But I don't know how to arrange the new updating panel position, Also don't know how to remove previous panel .
Hope you give me some advice, or even point out my mistake.
I also appreciate for some example code about this !
Here is the "Main.py"
import wx
import time
def ReadData():
with open('RealTime.txt') as f:
for line in f:
data = line.split()
results = map(float, data)
return results
class BlockWindow(wx.Panel):
# code on book "wxPython in action" Listing 11.1
def __init__(self, parent, ID=-1, label="",
pos = wx.DefaultPosition, size = (100, 25)):
wx.Panel.__init__(self, parent, ID, pos, size,
wx.RAISED_BORDER, label)
self.label = label
self.SetMinSize(size)
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnPaint(self, evt):
sz = self.GetClientSize()
dc = wx.PaintDC(self)
w,h = dc.GetTextExtent(self.label)
dc.SetFont(self.GetFont())
dc.DrawText(self.label, (sz.width-w)/2, (sz.height-h)/2)
class MyPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, size=(0,0))
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
self.timer.Start(3000)
def OnTimer(self, evt):
Data = ReadData()
sizer = wx.GridBagSizer(hgap=5, vgap=-1)
bw = BlockWindow(self, label="Item 1" )
sizer.Add(bw, pos=(4, 2))
#bw.Refresh()
bw = BlockWindow(self, label="Updated : %.3f" % Data[0])
sizer.Add(bw, pos=(5, 2))
bw.Refresh()
#bw.Update(self, label ="Updated : %.3f" % Data[0] )
mainSizer = wx.BoxSizer(wx.VERTICAL)
mainSizer.Add(sizer, 0, wx.EXPAND|wx.ALL, 10)
self.SetSizer(mainSizer)
self.Fit()
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title=' Frame Title')
mypanel = MyPanel(self)
self.SetSize(wx.Size(800,600))
self.Centre()
app = wx.App(False)
MyFrame().Show()
app.MainLoop()
Here is the 'WriteData.py',
import sched, time
from datetime import datetime as dt
data = ['5.564', '3.4', '2.176', '7.3', '4.4', '5.5', '2.3', '4.4', '5.1']
index = 0
while True:
start = dt.now().hour
stop = dt.now().hour + 1
if index >7 : index=1
if dt.now().hour in range(start, stop): # start, stop are integers (eg: 6, 9)
# call to your scheduled task goes here
f2 = open('RealTime.txt', 'w')
f2.write("%s " % data[index])
index = index + 1
f2.close()
time.sleep(3)
else:
time.sleep(3)
When I run the 2 .py file , I got this situation Running example
Hope you help me solve this .
I use python2.7 on win10.
Best regards, Kuo-Ting Tang
You don't need to recreate everything from scratch each time an update is needed. Just move the initialization code (where you create BlockWindows and sizers to the constructor of MyPanel. It seems that all you want to do is update the label of the second panel, to achieve this you could write a method in BlockWindow that will update the label and call Refresh so that OnPaint will be triggered and will take care of the rest.
class BlockWindow(wx.Panel):
# code on book "wxPython in action" Listing 11.1
def __init__(self, parent, ID=-1, label="",
pos = wx.DefaultPosition, size = (100, 25)):
wx.Panel.__init__(self, parent, ID, pos, size,
wx.RAISED_BORDER, label)
self.label = label
self.SetMinSize(size)
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnPaint(self, evt):
sz = self.GetClientSize()
dc = wx.PaintDC(self)
w,h = dc.GetTextExtent(self.label)
dc.SetFont(self.GetFont())
dc.DrawText(self.label, (sz.width-w)/2, (sz.height-h)/2)
def UpdateLabel(self, label):
self.label = label
self.Refresh()
class MyPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, size=(0,0))
sizer = wx.GridBagSizer(hgap=5, vgap=-1)
bw = BlockWindow(self, label="Item 1" )
sizer.Add(bw, pos=(4, 2))
self.block = BlockWindow(self, label="")
sizer.Add(self.block, pos=(5, 2))
mainSizer = wx.BoxSizer(wx.VERTICAL)
mainSizer.Add(sizer, 0, wx.EXPAND|wx.ALL, 10)
self.SetSizer(mainSizer)
self.Fit()
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
self.timer.Start(3000)
def OnTimer(self, evt):
Data = ReadData()
self.block.UpdateLabel("Updated : %.3f" % Data[0])

Switch to another panel wxpython

I'm learning wxpython.I read the documentation and after playing little bit with it, now I'm creating a small application containing some panels. On One panel I have created a login page. OnSubmit of the login panel, I want to switch to another panel. I'm not sure how to do that. Here is my code (ScreenGrab might help you) :
(Unwanted function and class definitions not shown here):
Toolbook_Demo.py
class ToolbookDemo( wx.Toolbook ) :
def __init__( self, parent ) :
print ""
wx.Toolbook.__init__( self, parent, wx.ID_ANY, style=
wx.BK_DEFAULT
#wx.BK_TOP
#wx.BK_BOTTOM
#wx.BK_LEFT
#wx.BK_RIGHT
)
# Make an image list using the LBXX images
il = wx.ImageList( 32, 32 )
for x in range( 4 ) :
imgObj = getattr( images, 'LB%02d' % ( x+1 ) )
bmp = imgObj.GetBitmap()
il.Add( bmp )
self.AssignImageList( il )
imageIdGenerator = getNextImageID( il.GetImageCount() )
panellogin = userlogin.TabPanel( self )
print panellogin.Hide()
notebookPageList = [ (userlogin.TabPanel( self ), 'Login'),
(panelTwo.TabPanel( self ), 'Panel Two'),
(panelThree.TabPanel( self ), 'Panel Three'),
(panelOne.TabPanel( self ), 'Home')]
imID = 0
for page, label in notebookPageList :
self.AddPage( page, label, imageId=imageIdGenerator.next() )
imID += 1
# An undocumented method in the official docs :
self.ChangeSelection( 0 ) # Select and view this notebook page.
# Creates no events - method SetSelection does.
self.Bind( wx.EVT_TOOLBOOK_PAGE_CHANGING, self.OnPageChanging )
self.Bind( wx.EVT_TOOLBOOK_PAGE_CHANGED, self.OnPageChanged )
userlogin.py
import wx
class TabPanel( wx.Panel ) :
""" This will be [inserted into] the first notebook tab. """
def __init__( self, parent ) :
wx.Panel.__init__( self, parent=parent, id=wx.ID_ANY )
sizer = wx.FlexGridSizer(rows=3, cols=2, hgap=5, vgap=15)
# Username
self.txt_Username = wx.TextCtrl(self, 1, size=(150, -1))
lbl_Username = wx.StaticText(self, -1, "Username:")
sizer.Add(lbl_Username,0, wx.LEFT|wx.TOP| wx.RIGHT, 50)
sizer.Add(self.txt_Username,0, wx.TOP| wx.RIGHT, 50)
# Password
self.txt_Password = wx.TextCtrl(self, 1, size=(150, -1), style=wx.TE_PASSWORD)
lbl_Password = wx.StaticText(self, -1, "Password:")
sizer.Add(lbl_Password,0, wx.LEFT|wx.RIGHT, 50)
sizer.Add(self.txt_Password,0, wx.RIGHT, 50)
# Submit button
btn_Process = wx.Button(self, -1, "&Login")
self.Bind(wx.EVT_BUTTON, self.OnSubmit, btn_Process)
sizer.Add(btn_Process,0, wx.LEFT, 50)
self.SetSizer(sizer)
def OnSubmit(self, event):
UserText = self.txt_Username.GetValue()
PasswordText = self.txt_Password.GetValue()
if authentcated(UserText, PasswordText):
#Switch to another panel
#Hide login panel until current session expires
#Show another panels only
I don't think you want to login using a panel in a Notebook-type widget that will just take you to the next tab. The user can click on the next tab without logging in. I am guessing you would like the first tab's panel to change to something else. The easiest way to do that is to call it's Hide method and Show a different panel. Fortunately, that's really easy to do. Here's an example that uses a menu to switch between the panels:
import wx
import wx.grid as gridlib
########################################################################
class PanelOne(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent=parent)
txt = wx.TextCtrl(self)
########################################################################
class PanelTwo(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent=parent)
grid = gridlib.Grid(self)
grid.CreateGrid(25,12)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(grid, 0, wx.EXPAND)
self.SetSizer(sizer)
########################################################################
class MyForm(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY,
"Panel Switcher Tutorial")
self.panel_one = PanelOne(self)
self.panel_two = PanelTwo(self)
self.panel_two.Hide()
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.panel_one, 1, wx.EXPAND)
self.sizer.Add(self.panel_two, 1, wx.EXPAND)
self.SetSizer(self.sizer)
menubar = wx.MenuBar()
fileMenu = wx.Menu()
switch_panels_menu_item = fileMenu.Append(wx.ID_ANY,
"Switch Panels",
"Some text")
self.Bind(wx.EVT_MENU, self.onSwitchPanels,
switch_panels_menu_item)
menubar.Append(fileMenu, '&File')
self.SetMenuBar(menubar)
#----------------------------------------------------------------------
def onSwitchPanels(self, event):
""""""
if self.panel_one.IsShown():
self.SetTitle("Panel Two Showing")
self.panel_one.Hide()
self.panel_two.Show()
else:
self.SetTitle("Panel One Showing")
self.panel_one.Show()
self.panel_two.Hide()
self.Layout()
# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()
You can use the same logic to swap out a panel in the Toolbook.

How to make Wxpython NewWindow appear to the Top

When right key pressed down, Popup Menu for 'edit' or 'delete' listctrl Item
If user selected 'edit', create a new window for editing
But the new window appear bottom of the old main window,
I know it may because i using wx.CallAfter(self.PopupMenu, MyPopupMenu(self, item), self.position)
But if i don't use wx.Callafter,
when i select a list item ,
Press right mouse key, choose the 'delete' menu,
A error dialog will appear " can not retrive information from list item XXX".
how to make the new Window top and active?
Thank You
import wx
DATA = {
0 : (u"Test2", "123456", ),
1 : (u"Test", "123456",),
2 : (u"doe", "156789", ),
3 : (u"John", "13455", )
}
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None)
frame.Show()
self.SetTopWindow(frame)
return True
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, wx.NewId(), size=(500, -1))
wx.Frame.CenterOnScreen(self)
self.panel = MyPanel(self)
class MyPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.list = MyListCtrl(self,3)
self.add_button = wx.Button(self, label="add")
h_sizer = wx.BoxSizer(wx.HORIZONTAL)
h_sizer.Add(self.add_button, proportion=1, flag=wx.ALL, border=5)
v_sizer = wx.BoxSizer(wx.VERTICAL)
v_sizer.Add(h_sizer, proportion=0, flag=wx.EXPAND)
v_sizer.Add(self.list, proportion=1, flag=wx.EXPAND, border=5)
self.SetSizer(v_sizer)
self.add_button.Bind(wx.EVT_BUTTON,self.onAdd)
def onAdd(self, event):
self.new_w = NewWindow(self)
self.new_w.Show()
class MyPopupMenu(wx.Menu):
def __init__(self,parent, item):
super(MyPopupMenu,self).__init__()
self.parent = parent
self.item = item
menuEdit = wx.MenuItem(self,wx.NewId(), 'edit %s' % item[0])
self.AppendItem(menuEdit)
self.Bind(wx.EVT_MENU, self.onEdit, menuEdit)
menuDel = wx.MenuItem(self,wx.NewId(), 'delete %s' % item[0])
self.AppendItem(menuDel)
self.Bind(wx.EVT_MENU, self.OnDelete, menuDel)
def onEdit(self,e):
self.parent.parent.edit_w = NewWindow(self.parent.parent)
self.parent.parent.edit_w.Show()
def OnDelete(self,e):
self.parent.DeleteAllItems()
class MyListCtrl(wx.ListCtrl):
def __init__(self, parent, columns):
wx.ListCtrl.__init__(self, parent, style=wx.LC_REPORT)
self.parent = parent
self.R_MOUSE = 0
self.InsertColumn(0, "name")
self.InsertColumn(1, "phone")
self.InsertColumn(2, "address")
self.SetColumnWidth(0, 200)
self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelect)
self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
self.itemDataMap = {}
self.refresh_list()
def refresh_list(self):
self.DeleteAllItems()
for entry in DATA.items():
self.Append(entry[1])
self.SetItemData(entry[0],entry[0])
def OnRightDown(self, event):
self.R_MOUSE = 1
self.position = event.GetPosition()
event.Skip()
def OnSelect(self, event):
index = event.GetIndex()
item = []
for i in range(3):
item.append(self.GetItem(itemId=index, col=i).GetText())
self.SetItemState(index, 0, wx.LIST_STATE_SELECTED)
self.SetItemBackgroundColour(index, wx.Colour(255,255,0))
if self.R_MOUSE:
self.R_MOUSE = 0
self.PopupMenu(MyPopupMenu(self, item), self.position)
class NewWindow(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, size=(400,250), style=wx.DEFAULT_FRAME_STYLE)
self.parent = parent
self.CenterOnParent()
panel = wx.Panel(self)
def onClick(self,event):
self.Close()
if __name__ == "__main__":
app = MyApp()
app.MainLoop()
You can use the flag wx.STAY_ON_TOP
try this:
class NewWindow(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, size=(400,250), style=wx.DEFAULT_FRAME_STYLE| wx.STAY_ON_TOP)

Categories

Resources