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])
Related
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.
I am having trouble nesting a ScrolledWindow inside a wx.Panel.
If I create a scrolled window on its own it seems to work, however when I create it inside a wx.Panel and add the wx.Panel to the frames sizer it does not. Is there anything that I am missing?
Note:
#pa = AScrolledWindow(self) <-- if uncommented this works
pa = ScrolledWindowHolder(self) # However this does not!
import wx
class ScrolledWindowHolder(wx.Panel):
def __init__(self, parent):
super(ScrolledWindowHolder, self).__init__(parent=parent)
mysizer = wx.GridBagSizer()
self.myscrolledWindow = AScrolledWindow(self)
mysizer.Add(self.myscrolledWindow, pos=(0, 0), flag=wx.EXPAND)
self.SetSizerAndFit(mysizer)
class AScrolledWindow(wx.ScrolledWindow):
def __init__(self, parent):
super(AScrolledWindow, self).__init__(parent)
gb = wx.GridBagSizer()
self.sizer = gb
self._labels = []
for y in xrange(1, 30):
self._labels.append(wx.StaticText(self, -1, "Label #%d" % (y,)))
gb.Add(self._labels[-1], (y, 1), (1, 1))
self.SetSizer(self.sizer)
self.SetScrollRate(5, 5)
self.EnableScrolling(True, True)
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, 'Programmatic size change')
sz = wx.BoxSizer(wx.VERTICAL)
#pa = AScrolledWindow(self)
pa = ScrolledWindowHolder(self)
sz.Add(pa, 1, wx.EXPAND)
self.SetSizer(sz)
def main():
wxapp = wx.App()
fr = TestFrame()
fr.Show(True)
wxapp.MainLoop()
if __name__ == '__main__':
main()
Not sure why but the issue appears to be with the fact that you are using a GridBagSizer with a single widget in ScrolledWindowHolder, which itself contains a GridBagSizer.
If you change ScrolledWindowHolder to use a BoxSizer it works, as expected.
class ScrolledWindowHolder(wx.Panel):
def __init__(self, parent):
super(ScrolledWindowHolder, self).__init__(parent=parent)
mysizer = wx.BoxSizer(wx.HORIZONTAL)
self.myscrolledWindow = AScrolledWindow(self)
mysizer.Add(self.myscrolledWindow, 1, wx.EXPAND,0)
self.SetSizerAndFit(mysizer)
Also, change the value of y to for y in range(1, 60): will demonstrate the scrolled window more effectively.
I am new to Python and have been following along with some wxPython tutorials online and was wondering how I could populate my custom cpu gauge with actual data!
Currently the custom gauge gets its value/data from a slider widget that the user has to manual drag up or down.
I wrote a simple function named: CpuData() which will basically return a dummy CPU value ranging from 0 to 100.
Now the question I have is how do I eliminate the Slider and have my gauge automatically get populated by looping through the CpuData() function.
Sorry for the Newbie question! Any help would be greatly appreciated.
import wx
import random
def CpuData(): # Will return a random CPU value
cData = random.randint(0,100)
print cData
CpuData()
class CPU(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id, size=(80, 110))
self.parent = parent
self.SetBackgroundColour('#000000')
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnPaint(self, event):
dc = wx.PaintDC(self)
dc.SetDeviceOrigin(0, 100)
dc.SetAxisOrientation(True, True)
pos = self.parent.GetParent().GetParent().sel
rect = pos / 5
for i in range(1, 21):
if i > rect:
dc.SetBrush(wx.Brush('#075100'))
dc.DrawRectangle(10, i*4, 30, 5)
dc.DrawRectangle(41, i*4, 30, 5)
else:
dc.SetBrush(wx.Brush('#36ff27'))
dc.DrawRectangle(10, i*4, 30, 5)
dc.DrawRectangle(41, i*4, 30, 5)
class CPUWidget(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(190, 140))
self.sel = 0
panel = wx.Panel(self, -1)
centerPanel = wx.Panel(panel, -1)
self.cpu = CPU(centerPanel, -1)
hbox = wx.BoxSizer(wx.HORIZONTAL)
self.slider = wx.Slider(panel, -1, self.sel, 0, 100, (-1, -1), (25, 90),
wx.VERTICAL | wx.SL_LABELS | wx.SL_INVERSE)
self.slider.SetFocus()
hbox.Add(centerPanel, 0, wx.LEFT | wx.TOP, 20)
hbox.Add(self.slider, 0, wx.LEFT | wx.TOP, 23)
self.Bind(wx.EVT_SCROLL, self.OnScroll)
panel.SetSizer(hbox)
self.Centre()
self.Show(True)
def OnScroll(self, event):
self.sel = event.GetInt()
self.cpu.Refresh()
app = wx.App()
CPUWidget(None, -1, 'cpu')
app.MainLoop()
Conceptually, this code works by a timer calling a function that triggers a custom event to generate a new value and update the CPU Graph.
The timer is provided by wxpython however any timing function would work (eg: a thread running a sleep function).
In this specific example, a custom event is not necessary as the timer is provided by the GUI itself. Nonetheless, using events is a good practice to get into as it allows a more abstract implementation (allowing easier refactoring) and eliminates threading issues when data is generated by non-GUI threads.
import wx
import wx.lib.newevent
import random
CpuEvent, EVT_CPU_EVENT = wx.lib.newevent.NewEvent()
class CPU(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id, size=(80, 110))
self.value = 100
self.parent = parent
self.SetBackgroundColour('#ffffff')
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(EVT_CPU_EVENT, self.do_value)
def do_value(self, event):
self.value = event.value
self.Refresh()
def OnPaint(self, event):
dc = wx.PaintDC(self)
dc.SetDeviceOrigin(0, 100)
dc.SetAxisOrientation(True, True)
pos = self.value
rect = pos / 5
for i in range(1, 21):
if i > rect:
dc.SetBrush(wx.Brush('#075100'))
dc.DrawRectangle(10, i*4, 30, 5)
dc.DrawRectangle(41, i*4, 30, 5)
else:
dc.SetBrush(wx.Brush('#36ff27'))
dc.DrawRectangle(10, i*4, 30, 5)
dc.DrawRectangle(41, i*4, 30, 5)
class CPUWidget(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title)
self.cpu = CPU(self, -1)
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.do_CpuData)
hbox = wx.BoxSizer(wx.HORIZONTAL)
hbox.Add(self.cpu, 1, wx.EXPAND | wx.ALIGN_CENTER_VERTICAL, 5)
self.SetSizer(hbox)
hbox.Fit(self)
self.Centre()
self.Show(True)
self.timer.Start(1000)
def do_CpuData(self, event = None):
cData = random.randint(0, 100)
event = CpuEvent(value = cData)
wx.PostEvent(self.cpu, event)
app = wx.App(redirect = False)
CPUWidget(None, -1, 'cpu')
app.MainLoop()
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())
I am using python 2.7 and the latest version of wxpython.
I need to place 2 grids side by side and to be able to add rows dynamically. Ideally, the sizes of grids should grow dynamically, but "big enough" size should be ok for the time being.
How should I change (and simplify) my code to make this happen?
class MatricesFrame(wx.BoxSizer):
def __init__(self, parentPanel):
super(MatricesFrame, self).__init__(wx.HORIZONTAL)
self.outputsMatrix = self.addEmptyGrid(parentPanel)
self.inputsMatrix = self.addEmptyGrid(parentPanel)
addRowsButton = wx.Button(parentPanel, -1, " Add Rows")
addRowsButton.Bind(wx.EVT_BUTTON, self.addRows)
self.Add(addRowsButton)
def initResize(self, ev = None):
self.inputsMatrix.SetSize((500, 500))
self.outputsMatrix.SetSize((500, 500))
def addEmptyGrid(self, parentPanel):
panel_ = wx.Panel(parentPanel)
sizer_ = wx.BoxSizer(wx.VERTICAL)
panel_.SetSizer(sizer_)
panel_.SetSize((500, 500))
matrix_ = wx.grid.Grid(panel_)
matrix_.SetRowLabelSize(0)
matrix_.SetColLabelSize(0)
matrix_.CreateGrid(1,1)
sizer_.Add(matrix_)
self.Add(panel_)
return matrix_
def addRows(self, ev=None):
self.inputsMatrix.AppendRows(1)
self.outputsMatrix.AppendRows(1)
class TestFrame(wx.Frame):
def __init__(self, parent):
super(TestFrame, self).__init__(parent, title='test', size=(1280, 950))
panel = wx.Panel(self)
box = wx.BoxSizer(wx.VERTICAL)
self.matricesFrame = MatricesFrame(panel)
box.Add(self.matricesFrame)
panel.SetSizer(box)
self.matricesFrame.initResize()
self.Centre()
self.Show()
self.matricesFrame.initResize()
wx.EVT_IDLE(wx.GetApp(), wx.WakeUpIdle())
def main():
app = wx.App(False)
t= TestFrame(None)
app.MainLoop()
if __name__ == '__main__':
main()
For starters, if you convert your custom Sizer into a custom Panel I think it makes your code a lot easier to control. Take a look at what I came up with
class MatricesPanel(wx.Panel):
def __init__(self, parent):
super(MatricesPanel, self).__init__(parent)
sizer = wx.BoxSizer(wx.HORIZONTAL)
self.outputsMatrix = self.addEmptyGrid(sizer)
self.inputsMatrix = self.addEmptyGrid(sizer)
addRowsButton = wx.Button(self, -1, " Add Rows")
addRowsButton.Bind(wx.EVT_BUTTON, self.addRows)
sizer.Add(addRowsButton)
self.SetSizer(sizer)
self.SetAutoLayout(True)
self.Layout()
def initResize(self, ev = None):
self.inputsMatrix.SetSize((500, 500))
self.outputsMatrix.SetSize((500, 500))
self.Layout()
def addEmptyGrid(self, sizer):
matrix_ = wx.grid.Grid(self)
matrix_.SetRowLabelSize(0)
matrix_.SetColLabelSize(0)
matrix_.CreateGrid(1,1)
sizer.Add(matrix_)
return matrix_
def addRows(self, ev=None):
self.inputsMatrix.AppendRows(1)
self.outputsMatrix.AppendRows(1)
self.Layout() #refresh the frame
Working with a Panel instead of a Sizer you greatly simplify your "addEmptyGrid" method as well as now you can try setting the size of your matricies using the size of the panel, not the size of the matrices themselves. Also, this allows you the flexibility to change from wx.Panel to wx.lib.scrolledpanel.ScrolledPanel if you wanted to add scroll bars (for if you add a lot of rows).
You can then init your new panel as below:
class TestFrame(wx.Frame):
def __init__(self, parent):
super(TestFrame, self).__init__(parent, title='test', size=(1280, 950))
self.matricesPanel = MatricesPanel(self)
sizer = wx.BoxSizer()
sizer.Add(self.matricesPanel, flag=wx.EXPAND)
self.SetSizer(sizer)
self.Centre()
self.Show()
self.matricesPanel.initResize()
wx.EVT_IDLE(wx.GetApp(), wx.WakeUpIdle())
Finally, so far as I can tell, columns in a Grid have a fixed width, so your matrix.SetSize((500, 500)) calls arent' doing much. If you can find a way to set the width of the column then I suggest you rewrite initResize() to set the width relative to the width of the panel.