I have a problem with adding script to GUI. Python script (which run crawls and compare scraped date) and GUI work fine. I'm not sure how to do it exacly, I just copied code into "def run" but then I got error: "valueerror: signal only works in main thread". Could someone explain how can I do that? Thanks.
import wx
import wx.lib.mixins.listctrl as listmix
from threading import *
musicdata = {
0 : ("1", "The Price Of Love", "Rock"),
1 : ("2", "Tom's Diner", "Rock"),
2 : ("4", "Praying For Time", "Rock"),
3 : ("3", "Here We Are", "Rock"),
4 : ("Linda Ronstadt", "Don't Know Much", "Rock"),
5 : ("Michael Bolton", "How Am I Supposed To Live Without You", "Blues"),
6 : ("Paul Young", "Oh Girl", "Rock"),
}
class WorkerThread(Thread):
def __init__(self, event):
Thread.__init__(self)
self._want_abort = 0
self.start()
def run(self):
while not self._want_abort:
#Here should I place code from script?
if self._want_abort:
return
def abort(self):
self._want_abort = 1
########################################################################
class TestListCtrl(wx.ListCtrl):
#----------------------------------------------------------------------
def __init__(self, parent, ID=wx.ID_ANY, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=0):
wx.ListCtrl.__init__(self, parent, ID, pos, size, style)
########################################################################
class TestListCtrlPanel(wx.Panel, listmix.ColumnSorterMixin):
#----------------------------------------------------------------------
def __init__(self, parent):
wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer1 = wx.BoxSizer(wx.HORIZONTAL)
self.list_ctrl = TestListCtrl(self, size=(-1,100),
style=wx.LC_REPORT
|wx.BORDER_SUNKEN
|wx.LC_SORT_ASCENDING
)
self.list_ctrl.InsertColumn(0, "%")
self.list_ctrl.InsertColumn(1, "Bookmakers")
self.list_ctrl.InsertColumn(2, "Date", wx.LIST_FORMAT_RIGHT)
self.list_ctrl.InsertColumn(3, "League")
self.list_ctrl.InsertColumn(4, "Teams")
self.list_ctrl.InsertColumn(5, "1")
self.list_ctrl.InsertColumn(6, "X")
self.list_ctrl.InsertColumn(7, "2")
self.list_ctrl.InsertColumn(8, "Link")
items = musicdata.items()
index = 0
for key, data in items:
self.list_ctrl.InsertStringItem(index, data[0])
self.list_ctrl.SetStringItem(index, 1, data[1])
self.list_ctrl.SetStringItem(index, 2, data[2])
self.list_ctrl.SetStringItem(index, 3, data[2])
self.list_ctrl.SetItemData(index, key)
index += 1
# Now that the list exists we can init the other base class,
# see wx/lib/mixins/listctrl.py
self.itemDataMap = musicdata
listmix.ColumnSorterMixin.__init__(self, 3)
self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColClick, self.list_ctrl)
sizer.Add(sizer1, .1, wx.ALL|wx.EXPAND, 5)
self.SetSizer(sizer)
button1 = wx.Button(self,-1,"START")
self.Bind( wx.EVT_BUTTON,self.button1,button1)
sizer1.Add(button1,0,0,0)
button2 = wx.Button(self,-1,"STOP")
self.Bind( wx.EVT_BUTTON,self.button2,button2)
sizer1.Add(button2,0,0,0)
sizer2 = wx.BoxSizer(wx.HORIZONTAL)
sizer2.Add(self.list_ctrl,1,wx.EXPAND)
sizer.Add(sizer2,1,wx.EXPAND)
def button1(self,event):
self.worker = None
if not self.worker:
self.worker = WorkerThread(self)
def button2(self, event):
if self.worker:
self.worker.abort()
#----------------------------------------------------------------------
# Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
def GetListCtrl(self):
return self.list_ctrl
#----------------------------------------------------------------------
def OnColClick(self, event):
print "column clicked"
event.Skip()
########################################################################
class MyForm(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Test Program", size=(1000,600))
a = TestListCtrlPanel(self)
#----------------------------------------------------------------------
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()
Related
i have wx.Notebook and 2 pages:
nb = wx.Notebook(PanelLobby,wx.ID_ANY,pos=(100,100),size=(413,214))
nb.AddPage(page1,"Page 1",select=True)
nb.AddPage(page2,"Page 2")
i want to add pages to it on button press,
i tried to bind a wx.EVT_LEFT_DOWN event but with no luck.
thanks in advance.
This is actually pretty easy. Here's one way to do it:
import random
import wx
########################################################################
class TabPanel(wx.Panel):
#----------------------------------------------------------------------
def __init__(self, parent):
""""""
wx.Panel.__init__(self, parent=parent)
colors = ["red", "blue", "gray", "yellow", "green"]
self.SetBackgroundColour(random.choice(colors))
btn = wx.Button(self, label="Press Me")
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(btn, 0, wx.ALL, 10)
self.SetSizer(sizer)
########################################################################
class DemoFrame(wx.Frame):
"""
Frame that holds all other widgets
"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, wx.ID_ANY,
"Notebook Tutorial",
size=(600,400)
)
panel = wx.Panel(self)
self.tab_num = 3
self.notebook = wx.Notebook(panel)
tabOne = TabPanel(self.notebook)
self.notebook.AddPage(tabOne, "Tab 1")
tabTwo = TabPanel(self.notebook)
self.notebook.AddPage(tabTwo, "Tab 2")
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.notebook, 1, wx.ALL|wx.EXPAND, 5)
btn = wx.Button(panel, label="Add Page")
btn.Bind(wx.EVT_BUTTON, self.addPage)
sizer.Add(btn)
panel.SetSizer(sizer)
self.Layout()
self.Show()
#----------------------------------------------------------------------
def addPage(self, event):
""""""
new_tab = TabPanel(self.notebook)
self.notebook.AddPage(new_tab, "Tab %s" % self.tab_num)
self.tab_num += 1
#----------------------------------------------------------------------
if __name__ == "__main__":
app = wx.App(False)
frame = DemoFrame()
app.MainLoop()
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)
I want to ask is it possible to add wx.Panel with event button in wxpython? There are plenty examples how to switch panels Hide first one and show second, but they are useless for me. I want to create panel with add button. For example I have panel something like this
import wx
import wx.grid as grid
class MainPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent = parent)
class SecondPanel(wx.Panel):
def __init__(self, parent,a,b):
wx.Panel.__init__(self, parent=parent)
MyGrid=grid.Grid(self)
MyGrid.CreateGrid(a, b)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(MyGrid, 0, wx.EXPAND)
self.SetSizer(sizer)
class MainFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="test",
size=(800,600))
self.splitter = wx.SplitterWindow(self)
self.panelOne = MainPanel(self.splitter)
self.panelTwo = SecondPanel(self.splitter, 1, 1)
txtOne = wx.StaticText(self.panelOne, -1, label = "piradoba", pos = (20,10))
self.txtTwo = wx.StaticText(self.panelOne, -1, label = "", pos = (40,80))
self.txtPlace = wx.TextCtrl(self.panelOne, pos = (20,30))
button = wx.Button(self.panelOne, label = "search", pos = (40,100))
button.Bind(wx.EVT_BUTTON, self.Onbutton)
self.splitter.SplitHorizontally(self.panelOne, self.panelTwo)
self.splitter.SetMinimumPaneSize(20)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.splitter, 1, wx.EXPAND)
self.SetSizer(sizer)
def Onbutton(self, event):
var=self.txtPlace.GetValue()
if len(var) == 9 or len(var) == 11:
???????????????????????????????????????????????
if __name__ == "__main__":
app = wx.App(False)
frame = MainFrame()
frame.Show()
app.MainLoop()
for example now I want to add new panel with this event what can I do? and I want to create this panel with event.
I don't know if it is what you need but in this example you have:
panel with button and event
button call function in mainframe
mainframe add next panel (with grid) to boxsizer
Tested on Linux Mint + Python 2.7.4
import wx
import wx.grid as grid
class MainPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent = parent)
self.txtOne = wx.StaticText(self, -1, label = "piradoba", pos = (20,10))
self.txtPlace = wx.TextCtrl(self, pos = (20,30))
self.txtTwo = wx.StaticText(self, -1, label = "", pos = (20,40))
button = wx.Button(self, label = "search", pos = (20,70))
button.Bind(wx.EVT_BUTTON, self.onButton)
def onButton(self, event):
var=self.txtPlace.GetValue()
if len(var) == 9 or len(var) == 11:
print "???"
# MainPanel->SplitterWindow->MainFrame ( 2x GetParent() )
self.GetParent().GetParent().AddPanel()
class SecondPanel(wx.Panel):
def __init__(self, parent,a,b):
"""Constructor"""
wx.Panel.__init__(self, parent=parent)
MyGrid=grid.Grid(self)
MyGrid.CreateGrid(a, b)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(MyGrid, 0, wx.EXPAND)
self.SetSizer(sizer)
class MainFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="test", size=(800,600))
self.splitter = wx.SplitterWindow(self)
self.panelOne = MainPanel(self.splitter)
self.panelTwo = SecondPanel(self.splitter, 1, 1)
self.splitter.SplitHorizontally(self.panelOne, self.panelTwo)
self.splitter.SetMinimumPaneSize(20)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.splitter, 2, wx.EXPAND)
self.SetSizer(self.sizer)
def AddPanel(self):
self.newPanel = SecondPanel(self, 1, 1)
self.sizer.Add(self.newPanel, 1, wx.EXPAND)
self.sizer.Layout()
if __name__ == "__main__":
app = wx.App(False)
frame = MainFrame()
frame.Show()
app.MainLoop()
I have a custom listctrl in my application that I would like the ability to select multiple rows (and deselect) much like one would do in a ListBox. Currently I have a listctrl that I am able to grab single selections; however, once I click on another row in my listctrl, the previous "un-highlights". I would like it to stay highlighted unless I click on it again -- so the user knows which rows he/she has selected (exact same way a ListBox works). I tried adding wx.LC_MULTIPLE_SEL to the listctrl line, but this did not work.
Help? Thanks!
I took this example from the following site by Mike Driscoll Python Blog. I adapted to it to grab row selections. Essentially I select a row and the index is appended to a list called InfoList. When I select a new row, it appends correctly, but I would like the row to stay "highlighted" in the actual list. Then I could add another line to remove an item from the list if I select it again in the listctrl.
import wx
import wx.lib.mixins.listctrl as listmix
InfoList = []
musicdata = {
0 : ("Bad English", "The Price Of Love", "Rock"),
1 : ("DNA featuring Suzanne Vega", "Tom's Diner", "Rock"),
2 : ("George Michael", "Praying For Time", "Rock"),
3 : ("Gloria Estefan", "Here We Are", "Rock"),
4 : ("Linda Ronstadt", "Don't Know Much", "Rock"),
5 : ("Michael Bolton", "How Am I Supposed To Live Without You", "Blues"),
6 : ("Paul Young", "Oh Girl", "Rock"),
}
########################################################################
class TestListCtrl(wx.ListCtrl):
#----------------------------------------------------------------------
def __init__(self, parent, ID=wx.ID_ANY, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=0):
wx.ListCtrl.__init__(self, parent, ID, pos, size, style)
########################################################################
class TestListCtrlPanel(wx.Panel, listmix.ColumnSorterMixin):
#----------------------------------------------------------------------
def __init__(self, parent):
wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS)
self.list_ctrl = TestListCtrl(self, size=(-1,100),
style=wx.LC_REPORT
|wx.BORDER_SUNKEN
|wx.LC_SORT_ASCENDING
)
self.list_ctrl.InsertColumn(0, "Artist")
self.list_ctrl.InsertColumn(1, "Title", wx.LIST_FORMAT_RIGHT)
self.list_ctrl.InsertColumn(2, "Genre")
items = musicdata.items()
index = 0
for key, data in items:
self.list_ctrl.InsertStringItem(index, data[0])
self.list_ctrl.SetStringItem(index, 1, data[1])
self.list_ctrl.SetStringItem(index, 2, data[2])
self.list_ctrl.SetItemData(index, key)
index += 1
# Now that the list exists we can init the other base class,
# see wx/lib/mixins/listctrl.py
self.itemDataMap = musicdata
listmix.ColumnSorterMixin.__init__(self, 3)
self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColClick, self.list_ctrl)
### I ADDED THIS ###
self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelectMustHave, self.list_ctrl)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
self.SetSizer(sizer)
### I ADDED THIS ###
def OnSelectMustHave(self,event):
info = event.GetData()
InfoList.append(info)
print info,InfoList
#----------------------------------------------------------------------
# Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
def GetListCtrl(self):
return self.list_ctrl
#----------------------------------------------------------------------
def OnColClick(self, event):
print "column clicked"
event.Skip()
########################################################################
class MyForm(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "List Control Tutorial")
# Add a panel so it looks the correct on all platforms
panel = TestListCtrlPanel(self)
#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()
its gross but this will do what you want
def OnSelectMustHave(self,event):
info = event.GetData()
InfoList.append(info)
def do_select():
map(lambda x:self.list_ctrl.Select(x,1) ,set(InfoList))
wx.CallLater(100,do_select)
print info,InfoList
return
however since you have the list of selected elements in InfoList something like the below solution may work
def OnSelectMustHave(self,event):
info = event.GetData()
InfoList.append(info)
map(lambda x:self.list_ctrl.SetItemBackgroundColour(x,wx.LIGHT_GREY) ,set(InfoList))
print info,InfoList
return
A regular ListCtrl works:
import wx
########################################################################
class MyForm(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "List Control Tutorial")
# Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, wx.ID_ANY)
self.index = 0
self.list_ctrl = wx.ListCtrl(panel, size=(-1,100),
style=wx.LC_REPORT
|wx.BORDER_SUNKEN
)
self.list_ctrl.InsertColumn(0, 'Subject')
self.list_ctrl.InsertColumn(1, 'Due')
self.list_ctrl.InsertColumn(2, 'Location', width=125)
btn = wx.Button(panel, label="Add Line")
btn.Bind(wx.EVT_BUTTON, self.add_line)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.list_ctrl, 1, wx.ALL|wx.EXPAND, 5)
sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
panel.SetSizer(sizer)
#----------------------------------------------------------------------
def add_line(self, event):
line = "Line %s" % self.index
self.list_ctrl.InsertStringItem(self.index, line)
self.list_ctrl.SetStringItem(self.index, 1, "01/19/2010")
self.list_ctrl.SetStringItem(self.index, 2, "USA")
self.index += 1
#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()
And if I modify this to use the two mixins you mentioned, it also works:
import wx
import wx.lib.mixins.listctrl as listmix
########################################################################
class MyListCtrl(wx.ListCtrl, listmix.TextEditMixin, listmix.ColumnSorterMixin):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.ListCtrl.__init__(self, parent, style=wx.LC_REPORT|wx.BORDER_SUNKEN)
listmix.ColumnSorterMixin.__init__(self, 3)
listmix.TextEditMixin.__init__(self)
#----------------------------------------------------------------------
def GetListCtrl(self):
return self
########################################################################
class MyForm(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "List Control Tutorial")
# Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, wx.ID_ANY)
self.index = 0
self.list_ctrl = MyListCtrl(panel)
self.list_ctrl.InsertColumn(0, 'Subject')
self.list_ctrl.InsertColumn(1, 'Due')
self.list_ctrl.InsertColumn(2, 'Location', width=125)
btn = wx.Button(panel, label="Add Line")
btn.Bind(wx.EVT_BUTTON, self.add_line)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.list_ctrl, 1, wx.ALL|wx.EXPAND, 5)
sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
panel.SetSizer(sizer)
#----------------------------------------------------------------------
def add_line(self, event):
line = "Line %s" % self.index
self.list_ctrl.InsertStringItem(self.index, line)
self.list_ctrl.SetStringItem(self.index, 1, "01/19/2010")
self.list_ctrl.SetStringItem(self.index, 2, "USA")
self.index += 1
#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()
I am using wxPython 2.6 and wxPython 2.8.12 on Windows 7. I can select multiple items one at a time and deselect them as well.
Suppose I need to replace the raw_input function in the following code with a wxPython dialog box that asks for user input and returns the value to program:
...
x = raw_input("What's your name?")
print 'Your name was', x
...
I'm just looking for a simple way to do that.
Thanks
Here is another simple way that does what I was looking for:
import wx
def ask(parent=None, message='', default_value=''):
dlg = wx.TextEntryDialog(parent, message, defaultValue=default_value)
dlg.ShowModal()
result = dlg.GetValue()
dlg.Destroy()
return result
# Initialize wx App
app = wx.App()
app.MainLoop()
# Call Dialog
x = ask(message = 'What is your name?')
print 'Your name was', x
This is fairly trivial. Here is one way.
import wx
class Frame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(-1, -1))
self.panel = wx.Panel(self)
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
self.btn = wx.Button(self.panel, -1, "Name-a-matic")
self.Bind(wx.EVT_BUTTON, self.GetName, self.btn)
self.txt = wx.TextCtrl(self.panel, -1, size=(140,-1))
self.txt.SetValue('name goes here')
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.btn)
sizer.Add(self.txt)
self.panel.SetSizer(sizer)
self.Show()
def GetName(self, e):
dlg = wx.TextEntryDialog(self.panel, 'Whats yo name?:',"name-o-rama","",
style=wx.OK)
dlg.ShowModal()
self.txt.SetValue(dlg.GetValue())
dlg.Destroy()
def OnCloseWindow(self, e):
self.Destroy()
app = wx.App()
frame = Frame(None, 'My Nameomatic')
app.MainLoop()
And here is another way:
import wx
class NameDialog(wx.Dialog):
def __init__(self, parent, id=-1, title="Enter Name!"):
wx.Dialog.__init__(self, parent, id, title, size=(-1, -1))
self.mainSizer = wx.BoxSizer(wx.VERTICAL)
self.buttonSizer = wx.BoxSizer(wx.HORIZONTAL)
self.label = wx.StaticText(self, label="Enter Name:")
self.field = wx.TextCtrl(self, value="", size=(300, 20))
self.okbutton = wx.Button(self, label="OK", id=wx.ID_OK)
self.mainSizer.Add(self.label, 0, wx.ALL, 8 )
self.mainSizer.Add(self.field, 0, wx.ALL, 8 )
self.buttonSizer.Add(self.okbutton, 0, wx.ALL, 8 )
self.mainSizer.Add(self.buttonSizer, 0, wx.ALL, 0)
self.Bind(wx.EVT_BUTTON, self.onOK, id=wx.ID_OK)
self.Bind(wx.EVT_TEXT_ENTER, self.onOK)
self.SetSizer(self.mainSizer)
self.result = None
def onOK(self, event):
self.result = self.field.GetValue()
self.Destroy()
def onCancel(self, event):
self.result = None
self.Destroy()
class Frame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(-1, -1))
self.panel = wx.Panel(self)
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
self.btn = wx.Button(self.panel, -1, "Name-a-matic")
self.Bind(wx.EVT_BUTTON, self.GetName, self.btn)
self.txt = wx.TextCtrl(self.panel, -1, size=(140,-1))
self.txt.SetValue('name goes here')
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.btn)
sizer.Add(self.txt)
self.panel.SetSizer(sizer)
self.Show()
def GetName(self, e):
dlg = NameDialog(self)
dlg.ShowModal()
self.txt.SetValue(dlg.result)
def OnCloseWindow(self, e):
self.Destroy()
app = wx.App()
frame = Frame(None, 'My Nameomatic')
app.MainLoop()