I would like to design a numeric keyboard and I do not know which function to use to have real time display when I clicked a button, say 1 is displayed in the textctrl when the button 1 is clicked like a calculator display. And it can display like 1234 when 1234 buttons are clicked in series. And I wonder how can I add backspace, clear functions to that
import wx
class iCal(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Text")
panel = MainPanel(self)
class MainPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent)
self.frame = parent
text_1 = wx.TextCtrl(self, value=float(num))
button_0 = wx.Button(self, label="0")
button_1 = wx.Button(self, label="1")
button_2 = wx.Button(self, label="2")
button_0.Bind(wx.EVT_LEFT_DOWN,self.output0)
button_1.Bind(wx.EVT_LEFT_DOWN,self.output1)
button_2.Bind(wx.EVT_LEFT_DOWN,self.output2)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(text_1, 0, wx.ALL|wx.CENTER, 5)
sizer.Add(button_0, 0, wx.ALL|wx.CENTER, 5)
sizer.Add(button_1, 0, wx.ALL|wx.CENTER, 5)
sizer.Add(button_2, 0, wx.ALL|wx.CENTER, 5)
self.SetSizer(sizer)
def self.output0(self,event):
if float(num) > 0:
num = num + str(0)
def self.output1(self,event):
num = num + str(1)
def self.output1(self,event):
num = num + str(1)
if __name__ == '__main__':
app = wx.App(False)
frame = iCal()
frame.Show()
app.MainLoop()
Numberic Keyboard
import wx
class iCal(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Text")
panel = MainPanel(self)
class MainPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent)
self.frame = parent
self.text_1 = wx.TextCtrl(self, value="0")
button_0 = wx.Button(self, label="0")
button_1 = wx.Button(self, label="1")
button_2 = wx.Button(self, label="2")
clear = wx.Button(self, label="Clear")
button_0.Bind(wx.EVT_LEFT_DOWN,self.output0)
button_1.Bind(wx.EVT_LEFT_DOWN,self.output1)
button_2.Bind(wx.EVT_LEFT_DOWN,self.output2)
clear.Bind(wx.EVT_LEFT_DOWN,self.clear)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.text_1, 0, wx.ALL|wx.CENTER, 5)
sizer.Add(button_0, 0, wx.ALL|wx.CENTER, 5)
sizer.Add(button_1, 0, wx.ALL|wx.CENTER, 5)
sizer.Add(button_2, 0, wx.ALL|wx.CENTER, 5)
sizer.Add(clear, 0, wx.ALL|wx.CENTER, 5)
self.SetSizer(sizer)
def output0(self,event):
num = "0"
my_string = self.text_1.GetValue() + num
self.text_1.SetValue(my_string)
def output1(self,event):
num = "1"
my_string = self.text_1.GetValue() + num
self.text_1.SetValue(my_string)
def output2(self,event):
num = "2"
my_string = self.text_1.GetValue() + num
self.text_1.SetValue(my_string)
def clear(self,event):
my_string = self.text_1.GetValue()
my_string = my_string[:len(my_string)-1]
self.text_1.SetValue(my_string)
if __name__ == '__main__':
app = wx.App(False)
frame = iCal()
frame.Show()
app.MainLoop()
If you are going to post code, at least run it first to make sure that you aren't posting rubbish.
Read about manipulating strings before you post: Strings
Related
I have two BoxSizers, the first one
sizer = wx.BoxSizer (wx.VERTICAL)
sizer.Add (self.grid2, 1, wx.EXPAND)
panel2.SetSizer (sizer)
and another vertical BoxSizers, to the left of the button grid, both BoxSizers interfere.
vbox1 = wx.FlexGridSizer (rows = 1, cols = 3, hgap = 5, vgap = 5)
buttonsBox1 = wx.BoxSizer (wx.VERTICAL)
buttonsBox1.Add (self.buttonoborved)
vbox1.Add (buttonsBox1)
vbox1.Add (self.grid2)
vbox1.Add (midPan1, wx.ID_ANY, wx.EXPAND | wx.ALL, 20)
panel2.SetSizer (vbox1)
An error occurs - Adding a window already in a sizer, detach it first!
How can they be called at the same time.
Edit:
That are two BoxSizer, one in other, but how can be put buttons in there.
import wx
import wx.grid
from wx.lib.scrolledpanel import ScrolledPanel
class TestPanel(ScrolledPanel):
def __init__(self, parent):
ScrolledPanel.__init__(self, parent, wx.ID_ANY, size=(640, 480))
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self._create_table(), 1, wx.EXPAND | wx.ALL, 5)
self.SetSizer(self.sizer)
self.SetupScrolling()
self.SetAutoLayout(1)
def _create_table(self):
_table = wx.grid.Grid(self, -1)
_table.CreateGrid(0, 2)
for i in range(1723): # Work normally If I use 1722 rows
_table.AppendRows()
_table.SetCellValue(i, 0, str(i))
return _table
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY,
title="Scroll table", size=(640, 480))
self.fSizer = wx.BoxSizer(wx.VERTICAL)
self.fSizer.Add(TestPanel(self), 1, wx.EXPAND)
self.SetSizer(self.fSizer)
self.Show()
if __name__ == "__main__":
app = wx.App(False)
frame = TestFrame()
app.MainLoop()
It depends where you want to place the buttons but let's assume they should be at the bottom.
import wx
import wx.grid
from wx.lib.scrolledpanel import ScrolledPanel
class TestPanel(ScrolledPanel):
def __init__(self, parent):
ScrolledPanel.__init__(self, parent, wx.ID_ANY, size=(640, 480))
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self._create_table(), 1, wx.EXPAND | wx.ALL, 5)
self.b1 = wx.Button(self, -1, "Button 1")
self.b2 = wx.Button(self, -1, "Button 2")
self.b3 = wx.Button(self, -1, "Button 3")
button_sizer = wx.BoxSizer(wx.HORIZONTAL)
button_sizer.Add(self.b1)
button_sizer.Add(self.b2)
button_sizer.Add(self.b3)
self.sizer.Add(button_sizer)
self.SetSizer(self.sizer)
self.SetupScrolling()
self.SetAutoLayout(1)
def _create_table(self):
_table = wx.grid.Grid(self, -1)
_table.CreateGrid(0, 2)
for i in range(1723): # Work normally If I use 1722 rows
_table.AppendRows()
_table.SetCellValue(i, 0, str(i))
return _table
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY,
title="Scroll table", size=(640, 480))
self.fSizer = wx.BoxSizer(wx.VERTICAL)
self.fSizer.Add(TestPanel(self), 1, wx.EXPAND)
self.SetSizer(self.fSizer)
self.Show()
if __name__ == "__main__":
app = wx.App(False)
frame = TestFrame()
app.MainLoop()
I am dynamically changing the number of wx objects in my GUI. The code is working but the alignment of the objects is not perfect. The static text objects are not middle aligned with the text objects.
Notice that the word 'if' is not perfectly aligned with 'field name'.
Code:
import wx
########################################################################
class MyPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
self.numRows = 0
self.frame = parent
#create sizers
self.mainSizer = wx.BoxSizer(wx.VERTICAL)
controlSizer = wx.BoxSizer(wx.HORIZONTAL)
self.colSizer = wx.BoxSizer(wx.VERTICAL)
self.addButton = wx.Button(self, label="Add")
self.addButton.Bind(wx.EVT_BUTTON, self.onAddWidget)
controlSizer.Add(self.addButton, 0, wx.CENTER|wx.ALL, 5)
self.removeButton = wx.Button(self, label="Remove")
self.removeButton.Bind(wx.EVT_BUTTON, self.onRemoveWidget)
controlSizer.Add(self.removeButton, 0, wx.CENTER|wx.ALL, 5)
self.mainSizer.Add(controlSizer, 0, wx.CENTER)
self.mainSizer.Add(self.colSizer, 0, wx.CENTER|wx.ALL, 10)
self.SetSizer(self.mainSizer)
#----------------------------------------------------------------------
def onAddWidget(self, event):
""""""
self.numRows += 1
#create the objects
rowSizer =wx.BoxSizer(wx.HORIZONTAL)
ifText = wx.StaticText(self, -1, 'If')
fieldBox1 = wx.TextCtrl(self, -1, 'Field Name')
dropDown1 = wx.Choice(self, -1, choices=['<', '<=', '=', '>=', '>'])
fieldBox2 = wx.TextCtrl(self, -1, 'Field Name')
thenText = wx.StaticText(self, -1, 'Then')
fieldBox3 = wx.TextCtrl(self, -1, 'Field Name')
#create a list of all the objects
objects = [ifText, fieldBox1, dropDown1, fieldBox2, thenText, fieldBox3]
#add object to row sizer
for myObject in objects:
rowSizer.Add(myObject, 0, wx.ALL, 5)
#add the objects to the sizer
self.colSizer.Add(rowSizer, 0, wx.ALL, 5)
self.frame.fSizer.Layout()
self.frame.Fit()
#----------------------------------------------------------------------
def onRemoveWidget(self, event):
""""""
if self.colSizer.GetChildren():
self.colSizer.Hide(self.numRows-1)
self.colSizer.Remove(self.numRows-1)
self.numRows -= 1
self.frame.fSizer.Layout()
self.frame.Fit()
########################################################################
class MyFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, parent=None, title="Add / Remove Buttons")
self.fSizer = wx.BoxSizer(wx.VERTICAL)
panel = MyPanel(self)
self.fSizer.Add(panel, 1, wx.EXPAND)
self.SetSizer(self.fSizer)
self.Fit()
self.Show()
#----------------------------------------------------------------------
if __name__ == "__main__":
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
del app
Please could someone advise on how I can fix this alignment issue.
Thanks a lot in advance!
Duh! It turns out I just needed to change the alignment when adding items to the row sizer.
rowSizer.Add(myObject, 0, wx.ALIGN_CENTER, 5)
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()
My code is generating following error-
"Couldn't retrieve information about list control item 3".
import wx
DATA = [("0", "Zero"), ("1", "One"), ("2", "Two")]
class MainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.panel = wx.Panel(self)
self.list = wx.ListCtrl(self.panel, style=wx.LC_REPORT)
self.list.InsertColumn(0, "Index")
self.list.InsertColumn(1, "Number")
for data in DATA:
self.list.Append((data[0], data[1]))
self.button = wx.Button(self.panel, label="Delete")
self.button.Bind(wx.EVT_BUTTON, self.OnButton)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.list, 1, wx.ALL | wx.EXPAND, 5)
self.sizer.Add(self.button, 0, wx.ALL | wx.EXPAND, 5)
self.panel.SetSizerAndFit(self.sizer)
self.Show()
def OnButton(self, e):
current_items = self.list.GetItemCount()
while ((current_items) >= 0) :
if (self.list.GetItemText(current_items) == "1" or self.list.GetItemText(current_items-1) == "2"):
self.list.DeleteItem(current_items)
wx.MessageBox("Delete item ", 'Delete Information',wx.OK)
else:
break
current_items-=1
if __name__ == "__main__":
app = wx.App(False)
win = MainWindow(None)
win.Centre()
app.MainLoop()
Can anyone tell me what is wrong with the code?
And what should i do to resolve this error?
thanks in advance.
Maybe you wanted something like this?
import wx
DATA = [("0", "Zero"), ("1", "One"), ("2", "Two")]
class MainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.panel = wx.Panel(self)
self.list = wx.ListCtrl(self.panel, style=wx.LC_REPORT)
self.list.InsertColumn(0, "Index")
self.list.InsertColumn(1, "Number")
for data in DATA:
self.list.Append((data[0], data[1]))
self.button = wx.Button(self.panel, label="Delete")
self.button.Bind(wx.EVT_BUTTON, self.OnButton)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.list, 1, wx.ALL | wx.EXPAND, 5)
self.sizer.Add(self.button, 0, wx.ALL | wx.EXPAND, 5)
self.panel.SetSizerAndFit(self.sizer)
self.Show()
def OnButton(self, e):
current_items = self.list.GetItemCount() - 1
while ((current_items) >= 0) :
if (self.list.GetItemText(current_items) == "1" or self.list.GetItemText(current_items) == "2"):
self.list.DeleteItem(current_items)
wx.MessageBox("Delete item ", 'Delete Information',wx.OK)
else:
break
current_items-=1
if __name__ == "__main__":
app = wx.App(False)
win = MainWindow(None)
win.Centre()
app.MainLoop()
def OnButton(self, e):
itemIndex = self.list.GetNextItem(itemIndex,
wx.LIST_NEXT_ALL,
wx.LIST_STATE_SELECTED);
print itemIndex
if itemIndex == -1:
print "Nothing Selected";
# // Got the selected item index
print "SELECTED:",self.list.GetItemText(itemIndex);
try something like that
For some reason, I can't update the second label. I tested it by asking for input and print out strings and it worked so I apparently did not write the label update function correctly. Any ideas?
import wx
import time
class LeftPanel(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id, style=wx.BORDER_SUNKEN)
self.text = parent.GetParent().rightPanel.text
self.text_2 = parent.GetParent().rightPanel.text_2
button1 = wx.Button(self, -1, 'Count', (10, 10))
button2 = wx.Button(self, -1, 'Countdown', (10, 60))
button3 = wx.Button(self, -1, 'Action', (10, 110))
self.Bind(wx.EVT_BUTTON, self.OnPlus, id=button1.GetId())
self.Bind(wx.EVT_BUTTON, self.OnMinus, id=button2.GetId())
self.Bind(wx.EVT_BUTTON, self.button_Pressed, id=button3.GetId())
self.timed_Out = 1
def OnPlus(self, event):
value = 1
for t in range(50):
value = value + 1
time.sleep(1)
wx.Yield()
self.text.SetLabel(str(value))
def OnMinus(self, event):
value = 60
for t in range(value):
value = value - 1
time.sleep(1)
wx.Yield()
self.text.SetLabel(str(value/60) + ':' + str(value%60))
self.timed_Out = 0
self.text_2.SetLabel(str('End o\'line.'))
def button_Pressed(self, event):
if self.timed_Out == 1:
if self.text_2 == 'First':
self.text_2.SetLabel('Second')
elif self.text_2 == 'Second':
self.text_2.SetLabel('First')
class RightPanel(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id, style=wx.BORDER_SUNKEN)
self.text = wx.StaticText(self, -1, '0', (10,60))
self.text_2 = wx.StaticText(self,-1,'First',(10, 120))
class Communicate(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(600, 200))
panel = wx.Panel(self, -1)
self.rightPanel = RightPanel(panel, -1)
leftPanel = LeftPanel(panel, -1)
hbox = wx.BoxSizer()
hbox.Add(leftPanel, 1, wx.EXPAND | wx.ALL, 4)
hbox.Add(self.rightPanel, 1, wx.EXPAND | wx.ALL, 5)
panel.SetSizer(hbox)
self.Centre()
self.Show(True)
app = wx.App()
Communicate(None, -1, 'widgets communicate')
app.MainLoop()
In button_Pressed, you're testing self.text_2 for equality with "First" or "Second", but text_2 is a StaticText object, not a string, so the test doesn't work. Try this:
if self.text_2.GetLabelText() == 'First':