How come ScrolledPanel in wxpython not working this way? - python

I don't know why the following code not working, please help me out:
import wx
import wx.lib.scrolledpanel as scrolled
class TaskFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, parent = None, id = -1, title="ScrolledPanel", size = (500, 600))
MainPanel = wx.Panel(self)
NewPanel = scrolled.ScrolledPanel(parent = MainPanel, pos = (100, 100), size = (300, 200), id = -1, style = wx.TAB_TRAVERSAL|wx.SUNKEN_BORDER, name="panel" )
self.Button = wx.Button(parent = NewPanel, id = -1, label="Log", pos=(500, 30), size=(50, 20))
NewPanel.SetupScrolling()
class TaskApp(wx.App):
def OnInit(self):
self.frame = TaskFrame()
self.frame.Show()
self.SetTopWindow(self.frame)
return True
def main():
App = TaskApp(redirect = False)
App.MainLoop()
if __name__ == "__main__":
main()
The Log button should be in the NewPanel, and the NewPanel should be able to scroll, but it's not, what's the problem?

Try using a sizer. You have to place an object larger than the ScrolledPanel inside it to activate the scrolling (as far as I know), so this should do what I think you're trying to do:
class TaskFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, parent = None, id = -1, title="ScrolledPanel", size = (500, 600))
MainPanel = wx.Panel(self)
NewPanel = scrolled.ScrolledPanel(parent = MainPanel, pos = (100, 100), size = (300, 200), id = -1, style = wx.TAB_TRAVERSAL|wx.SUNKEN_BORDER, name="panel" )
PanelSizer = wx.BoxSizer()
InsidePanel = wx.Panel(NewPanel)
self.Button = wx.Button(parent=InsidePanel, id = -1, label="Log", pos=(500, 30), size=(50, 20))
PanelSizer.Add(InsidePanel, proportion=1)
NewPanel.SetSizer(PanelSizer)
NewPanel.SetupScrolling()

Related

Why is my wxPython program not working? (Code inside)

So I have to write a BMI calculator. I cannot get it to work tho.
Before I added the class and the methods it was working fine but in order to make the Compute button working I had to use a class. And once I did it broke.
Can you tell me what I am doing wrong?
import wx
class BMI(wx.Frame):
def InitUI(self):
window = wx.Frame(self, title="wx.SpinCtrl", size=(400, 300))
panel = wx.Panel(window)
label = wx.StaticText(panel, label="Body Mass Index", pos=(20, 10))
self.weight = wx.StaticText(panel, label="weight:", pos=(20, 70))
self.height = wx.StaticText(panel, label="height:", pos=(20, 140))
weightset = wx.SpinCtrl(panel, value='0', pos=(100, 70))
heightset = wx.SpinCtrl(panel, value='0', pos=(100, 140))
result = wx.StaticText(panel, label="BMI:", pos=(300, 110))
result2 = wx.StaticText(panel, label=" ", pos=(335, 110))
computeButton = wx.Button(panel, label='Compute', pos=(40, 200))
closeButton = wx.Button(panel, label='Close', pos=(250, 200))
computeButton.Bind(wx.EVT_BUTTON, self.ComBMI)
closeButton.Bind(wx.EVT_BUTTON, self.OnClose)
def ComBMI(self, e):
teglo = self.weight.GetValue()
vis = self.height.GetValue()
bmi = teglo * (pow(vis, 2))
self.result2.SetLabel(str(bmi))
def OnClose(self, e):
self.Close(True)
def main():
app = wx.App()
ex = BMI(None)
ex.Show()
app.MainLoop()
if __name__ == '__main__':
main()
Uhh, question is why is any of this even working...
Issues (probably incomplete):
No proper initializsation of class BMI (see a tutorial how to use a wx.Frame class properly)
Use of SpinCtrl will either not allow entering height in meter or limit people to 100 cm in heigth (and 100 kg in weight, by the way). Use as TextCtrl instead and parse the value with float (or modify the range/resolution of the SpinCtrl)
You do a self.weigth.GetValue() on a object attribute, but user input goes to self.weightset
Wrong BMI formula (look it up on wiki), linked to that wrong units
You need to resolve the __init__ of your class.
Use the spinctrl values not the text objects.
Define the weight to be a float or explain the value to be input is in Centimetres.
The formula is weight(Kgs) / (height (M) * height) unless you want to set off a plethora of un-necessary crash diets ;)
You may want to add the ability to choose between Metric and Imperial values
import wx
import wx.lib.agw.floatspin as FS
class BMI(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='BMI Calculatot')
panel = wx.Panel(self)
label = wx.StaticText(panel, label="Body Mass Index", pos=(20, 10))
weightT = wx.StaticText(panel, label="weight (Kgs):", pos=(20, 70))
heightT = wx.StaticText(panel, label="height (M):", pos=(20, 140))
self.weight = wx.SpinCtrl(panel, value='0', min=0, max=500, pos=(100, 70))
#self.height = wx.SpinCtrl(panel, value='0', min=100, max=250, pos=(100, 140))
self.height = FS.FloatSpin(panel, -1, min_val=1.00, max_val=2.50, increment=0.01, pos=(100, 140))
self.height.SetFormat("%f")
self.height.SetDigits(2)
resultT = wx.StaticText(panel, label="BMI:", pos=(300, 110))
self.result = wx.StaticText(panel, label=" ", pos=(335, 110))
computeButton = wx.Button(panel, label='Compute', pos=(40, 200))
closeButton = wx.Button(panel, label='Close', pos=(250, 200))
computeButton.Bind(wx.EVT_BUTTON, self.ComBMI)
closeButton.Bind(wx.EVT_BUTTON, self.OnClose)
def ComBMI(self, e):
teglo = self.weight.GetValue()
vis = self.height.GetValue()
bmi = teglo / (pow(vis, 2))
self.result.SetLabel(str(round(bmi,3)))
def OnClose(self, e):
self.Close(True)
if __name__ == '__main__':
app = wx.App()
ex = BMI()
ex.Show()
app.MainLoop()

Manipulating ComboBoxes in wxPython

I'm using Python and wxPython to create an UI that lets the user select a XML file in the first combobox and all the components (i.e. buttons) in the XML appears as choices of another combobox below. It's clearly reading correctly as it prints out the right thing in the console as I go through all the XMLs, but I just can't seem to link it back to the combobox I'm looking for.
Here's the code:
import wx
import os
import xml.dom.minidom
from xml.dom.minidom import parse
# get all xmls
path = "C:\Users\William\Desktop\RES\Param"
files = os.listdir(path)
class Panel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.xmlList = files
self.xmlPickerTitle = wx.StaticText(self, label="XML Picker", pos=(20, 30))
self.xmlPicker = wx.ComboBox(self, pos=(100, 30), size=(500, -1), choices=self.xmlList, style=wx.CB_DROPDOWN)
self.elementsTitle = wx.StaticText(self, label="Elements Found", pos=(20, 100))
# labels
self.buttonsPickerTitle = wx.StaticText(self, pos=(20,120), label="Buttons")
self.buttonList = []
self.buttonsPicker = wx.ComboBox(self, pos=(100, 120), size=(250, -1), choices=buttonList, style=wx.CB_DROPDOWN)
self.Bind(wx.EVT_COMBOBOX, self.XMLSelect,)
def XMLSelect(self, event):
xmlPicked = self.xmlList[event.GetSelection()]
DOMTree = xml.dom.minidom.parse(xmlPicked)
collection = DOMTree.documentElement
buttons = DOMTree.getElementsByTagName("Button")
for button in buttons:
if button.hasAttribute("name"):
buttonList.append(button.getAttribute("name"))
print button.getAttribute("name")
app = wx.App(False)
frame = wx.Frame(None, title = "Auto", size = (800, 600))
panel = Panel(frame)
frame.Show()
app.MainLoop()
Any ideas?
Thanks in advance!
I had an issue with the file name not containing the path so I have had to join them to pass into xmlPicked but that might be a difference between linux and Windows.
The key point is to Clear() and Append() to the ComboBox
Also, Bind to a specific ComboBox because you have 2.
Finally, set the selection for the ComboBox so that it is obvious that you have data available.
import wx
import os
import xml.dom.minidom
from xml.dom.minidom import parse
# get all xmls
path = "/home/whatever"
files = os.listdir(path)
class Panel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.xmlList = files
self.xmlPickerTitle = wx.StaticText(self, label="XML Picker", pos=(20, 30))
self.xmlPicker = wx.ComboBox(self, pos=(100, 30), size=(500, -1), choices=self.xmlList, style=wx.CB_DROPDOWN)
self.elementsTitle = wx.StaticText(self, label="Elements Found", pos=(20, 100))
# labels
self.buttonsPickerTitle = wx.StaticText(self, pos=(20,120), label="Buttons")
self.buttonList = []
self.buttonsPicker = wx.ComboBox(self, pos=(100, 120), size=(250, -1), choices=self.buttonList, style=wx.CB_DROPDOWN)
self.xmlPicker.Bind(wx.EVT_COMBOBOX, self.XMLSelect,)
def XMLSelect(self, event):
self.buttonsPicker.Clear()
xmlPicked = self.xmlList[event.GetSelection()]
xmlPicked = os.path.join(path,xmlPicked)
DOMTree = xml.dom.minidom.parse(xmlPicked)
collection = DOMTree.documentElement
buttons = DOMTree.getElementsByTagName("Button")
for button in buttons:
if button.hasAttribute("name"):
button_name = str(button.getAttribute("name"))
self.buttonsPicker.Append(button_name)
print button_name
self.buttonsPicker.SetSelection(0)
app = wx.App(False)
frame = wx.Frame(None, title = "Auto", size = (800, 600))
panel = Panel(frame)
frame.Show()
app.MainLoop()

How can I resize * wx.CheckListBox* automatically in wxPython?

If the text of label for checkboxes is more than width of a wx.CheckListBox one must change its size.
The short answer is to not set the size of the CheckListBox. This will automatically set the size to the largest item. If you do set the size parameter and the text does not fit, set the style=wx.LB_HSCROLL and a horizontal slider will be created, if needed.
Below a demonstration with both versions of self.clb
import wx
class ClbView(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, None, -1, 'CheckListBox', size=(250, 200))
def createWidgets(self, list):
self.panel = wx.Panel(self, -1)
# self.clb = wx.CheckListBox(self.panel, -1, size=(100,150),choices=list,style=wx.LB_HSCROLL)
self.clb = wx.CheckListBox(self.panel, -1, choices = list)
self.btn_exit = wx.Button(self.panel, wx.ID_ANY, 'Exit')
def sizeWidgets(self):
self.vbox = wx.BoxSizer(wx.VERTICAL)
self.vbox.Add(self.clb, 0, wx.ALL|wx.CENTER, 5)
self.vbox.Add(self.btn_exit, 0, wx.CENTER)
self.panel.SetSizer(self.vbox)
self.Centre()
class ClbControl:
def __init__(self):
self.list = ['Quite a long piece of text', 'Short text', 'X']
self.createView()
def createView(self):
self.view = ClbView(None)
self.view.createWidgets(self.list)
self.view.sizeWidgets()
self.view.Show()
self.view.btn_exit.Bind(wx.EVT_BUTTON, self.onExit)
self.view.clb.Bind(wx.EVT_CHECKLISTBOX, self.onCLB )
def onCLB(self, evt):
x = int(evt.GetSelection())
print "Box ",x," Accessed"
def onExit(self, evt):
self.view.Close()
if __name__ == '__main__':
app = wx.App()
controller = ClbControl()
app.MainLoop()

wxpython: how to make a popup window stay on top of the current application

I have an application that produces a popup window containing a progress bar. The code related to the popup window is below. I would like to force this popup window to stay on top of the application that produces this window. I tried using wx.STAY_ON_TOP, but using this style forces the popup window to stay on top of all applications, which is not what I want. Any advice/suggestion would be appreciated!
class ProgressBar(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, None, title="In progress...",
size=(300, 125))
GridBagSizer = wx.GridBagSizer()
TextFont = wx.Font(pointSize = 9, family = wx.SWISS, style = wx.NORMAL, weight = wx.NORMAL, faceName = 'Tahoma')
self.SetBackgroundColour('white')
self.gauge = wx.Gauge(self, range = 100, size = (-1, 30), style = wx.GA_HORIZONTAL, name = 'In Progress')
self.gauge.SetValue(0)
GridBagSizer.Add(self.gauge, pos = (0, 0), span = (1, 1), flag = wx.EXPAND|wx.ALL, border = 15)
self.txt = wx.StaticText(self, label = 'Retrieving data...', style = wx.ALIGN_CENTER)
self.txt.SetFont(TextFont)
box = wx.BoxSizer(wx.HORIZONTAL)
box.Add(self.txt, 0, wx.CENTER)
GridBagSizer.Add(box, pos = (1, 0), span = (1, 1),
flag = wx.EXPAND|wx.LEFT|wx.RIGHT|wx.BOTTOM,
border = 15)
GridBagSizer.AddGrowableCol(0)
#GridBagSizer.AddGrowableCol(1)
self.SetSizer(GridBagSizer)
self.SetMinSize((300, 125))
self.SetMaxSize((300, 125))
self.Layout()
self.Center()
def Update(self, step):
self.gauge.SetValue(step)
if step == 100:
self.Close()
def SetLabel(self, label):
self.txt.SetLabel(label)
self.Refresh()
self.Layout()
use style wx.FRAME_FLOAT_ON_PARENT
class ProgressBar(wx.Frame):
def __init__(self, parent): # you must set the parent ...
wx.Frame.__init__(self, parent, title="In progress...",
size=(300, 125),style=wx.FRAME_FLOAT_ON_PARENT)
...
if you want it to have the default frame stuff you can do
style=wx.DEFAULT_FRAME_STYLE|wx.FRAME_FLOAT_ON_PARENT

Destroy() wxpython simple error?

i am having an interesting problem,
This program is a simple image viewer and it can contain different images in a listbox. The listbox contains the names of the images. You can load an image to an item in the listbox. You can click any item on the listbox to see its image. For some reason Destroy() is not functioning properly. Please run the following code if you are unable to understand me,
IMAGE_NAME=[]
IMAGE_DATA=[]
IMAGE_LISTSEL=[]
import sys
import wx
def deletepic(self,parent):
try:
bitmap1.Destroy()
bmp1.Destroy()
except:
print sys.exc_info()
def sendnewpic(self,parent):
global scroll_img
deletepic(self,parent)
print IMAGE_DATA[IMAGE_LISTSEL[0]]
if IMAGE_DATA[IMAGE_LISTSEL[0]]!='':
try:
print IMAGE_DATA[IMAGE_LISTSEL[0]]
bmp1 = wx.Image(IMAGE_DATA[IMAGE_LISTSEL[0]], wx.BITMAP_TYPE_ANY).ConvertToBitmap()
bitmap1 = wx.StaticBitmap(scroll_img, -1, bmp1, (0, 0))
except:
pass
def areachange(self,pg):
print pg
try:
if IMAGE_DATA[IMAGE_LISTSEL[0]]=='':
deletepic(self,parent)
except:
pass
if pg=="Images":
self.images_area.Show()
else:
self.images_area.Hide()
class imageMax(wx.Panel):
pass
class imageTab(imageMax):
def imagesel(self,parent):
IMAGE_LISTSEL[:] = []
IMAGE_LISTSEL.append(self.listBox.GetSelection())
sendnewpic(self,parent)
def newAddImage(self,parent):
IMAGE_NAME.append('hi');
IMAGE_DATA.append('');
self.listBox.Set(IMAGE_NAME)
self.listBox.SetSelection(len(IMAGE_NAME)-1)
self.imagesel(self) #making it a selected image, globally
def reName(self,parent):
sel = self.listBox.GetSelection()
text = self.listBox.GetString(sel)
renamed = wx.GetTextFromUser('Rename item', 'Rename dialog', text)
if renamed != '':
IMAGE_NAME.pop(sel)
IMAGE_NAME.insert(sel,renamed)
self.listBox.Set(IMAGE_NAME)
self.listBox.SetSelection(sel)
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.listBox = wx.ListBox(self, size=(200, -1), choices=IMAGE_NAME, style=wx.LB_SINGLE)
self.sizer = wx.BoxSizer(wx.VERTICAL)
btnSizer = wx.BoxSizer(wx.VERTICAL) #change to horizontal for side by side
self.sizerMain = wx.BoxSizer()
self.listBox.Bind(wx.EVT_LISTBOX_DCLICK, self.reName)
self.listBox.Bind(wx.EVT_LISTBOX, self.imagesel)
btn = wx.Button(self, label="Create New",size=(200, 40))
btnTwo = wx.Button(self, label="Test 2",size=(200, 40))
btn.Bind(wx.EVT_BUTTON, self.newAddImage)
self.sizer.Add(self.listBox, proportion=1, flag=wx.TOP | wx.EXPAND | wx.LEFT, border=5)
btnSizer.Add(btn, 0, wx.ALL, 5)
btnSizer.Add(btnTwo, 0, wx.ALL, 5)
self.sizer.Add(btnSizer)
self.sizerMain.Add(self.sizer, proportion=0, flag=wx.BOTTOM | wx.EXPAND, border=0)
self.SetSizer(self.sizerMain)
class MyNotebook(wx.Notebook):
def __init__(self, *args, **kwargs):
wx.Notebook.__init__(self, *args, **kwargs)
class MyPanel(imageTab):
def OnClickTop(self, event):
scroll_img.Scroll(600, 400)
def OnClickBottom(self, event):
scroll_img.Scroll(1, 1)
def OnPageChanged(self, event):
new = event.GetSelection()
areachange(self,self.notebook.GetPageText(new))
event.Skip()
def OnPageChanging(self, event):
event.Skip()
def onOpenFile(self,parent):
""" Open a file"""
filename = wx.FileSelector()
if (filename!=''):
global bitmap1,bmp1,scroll_img
if IMAGE_DATA[IMAGE_LISTSEL[0]]!='':
deletepic(self,parent)
bmp1 = wx.Image(filename, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
bitmap1 = wx.StaticBitmap(scroll_img, -1, bmp1, (0, 0))
scroll_img.SetScrollbars(1, 1, bmp1.GetWidth(), bmp1.GetHeight())
IMAGE_DATA[IMAGE_LISTSEL[0]]=filename
print IMAGE_DATA
def __init__(self, *args, **kwargs):
global bitmap1,bmp1,scroll_img
wx.Panel.__init__(self, *args, **kwargs)
self.notebook = MyNotebook(self, size=(225, -1))
# self.button = wx.Button(self, label="Something else here? Maybe!")
tab_images = imageTab(self.notebook)
# add the pages to the notebook with the label to show on the tab
self.notebook.AddPage(tab_images, "Pics",select=True)
scroll_img = wx.ScrolledWindow(self, -1)
scroll_img.SetScrollbars(1, 1, 600, 400)
#self.button = wx.Button(scroll_img, -1, "Scroll Me", pos=(50, 20))
#self.Bind(wx.EVT_BUTTON, self.OnClickTop, self.button)
#self.button2 = wx.Button(scroll_img, -1, "Scroll Back", pos=(500, 350))
#self.Bind(wx.EVT_BUTTON, self.OnClickBottom, self.button2)
self.images_area=wx.StaticBox(self, -1, '')
self.sizerBox = wx.StaticBoxSizer(self.images_area,wx.HORIZONTAL)
#self.load_file=wx.Button(self, label='Load File')
#self.sizerBox.Add(self.load_file,0,wx.ALL,5)
self.sizerBox2 = wx.BoxSizer()
self.sizerBox.Add(scroll_img, 1, wx.EXPAND|wx.ALL, 10)
self.sizerBox2.Add(self.sizerBox, 1, wx.EXPAND|wx.ALL, 10)
self.sizer = wx.BoxSizer()
self.sizer.Add(self.notebook, proportion=0, flag=wx.EXPAND)
# self.sizer.Add(self.button, proportion=0)
btnSizer = wx.BoxSizer() #change to horizontal for side by side
btnTwo = wx.Button(self, label="Load File",size=(200, 40))
btnTwo.Bind(wx.EVT_BUTTON,self.onOpenFile)
bmp1 = None
bitmap1 = None
btnSizer.Add(btnTwo, 0, wx.TOP, 15)
self.sizerBox2.Add(btnSizer)
#self.sizerBox.Add(self.bitmap1)
self.sizer.Add(self.sizerBox2, proportion=1, flag=wx.EXPAND)
self.SetSizer(self.sizer)
self.notebook.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChanged)
areachange(self,self.notebook.GetPageText(0))
class MainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.panel = MyPanel(self)
self.Show()
app = wx.App(False)
win = MainWindow(None, size=(600, 400))
app.MainLoop()
Try this to see what the error is,
1.you press create new (any amount of times)
2.press, load file
3. click any item on the listbox (except for the one that you are in)
4. Then go back to the orginal item that you were in,
5. Then click any item, other than the one you are currently in
There will be some sort of problem, the image does not destroy itself and returns an error like the following:
(, PyDeadObjectError('The C++ part of the StaticBitmap object has been deleted, attribute access no longer allowed.',), )
I am still able to load images but the previous images do not delete.
It is hard to word this problem, if anyone can help me with this situation, it would be greatly appreciated. If you need further explanation please comment. I thank you greatly for viewing.
Here you have your code fixed to clear your current image when loading another one.
This is done basically using self.parent.bitmap.Destroy().
I modified some few things without changing the structure of your code, in order for you to recognize changes. I eliminated globals calls. Look how I also eliminated the global IMAGE_LISTSEL variable and converted it in a class attribute. That is what Robin and Fenikso were telling you. Try to do the same with IMAGE_NAME and IMAGE_DATA.
Although the code is working, it is still far from being acceptable wxpython code. You can get many examples of correctly written wxpython code in the web. If you can afford it I recommend to you wxPython in Action from Noel Rappin and Robin Dunn.
IMAGE_NAME = []
IMAGE_DATA = []
import sys
import wx
def deletepic(self):
try:
self.parent.bitmap.Destroy()
except:
print sys.exc_info()
def sendnewpic(self):
if self.parent.bitmap: deletepic(self)
if IMAGE_DATA[self.image_listsel] != '':
try:
print IMAGE_DATA[self.image_listsel]
bmp = wx.Image(IMAGE_DATA[self.image_listsel], wx.BITMAP_TYPE_ANY).ConvertToBitmap()
self.parent.scroll_img.SetScrollbars(1, 1, bmp.GetWidth(), bmp.GetHeight())
self.parent.bitmap = wx.StaticBitmap(self.parent.scroll_img, -1, bmp, (0, 0))
self.parent.Refresh()
except:
pass
def areachange(self, pg):
print pg
try:
if IMAGE_DATA[self.image_listsel] == '':
deletepic(self)
except:
pass
if pg == "Images":
self.images_area.Show()
else:
self.images_area.Hide()
class imageTab(wx.Panel):
def __init__(self, parent, grandparent):
wx.Panel.__init__(self, parent)
self.parent = grandparent
self.image_listsel = 0
self.listBox = wx.ListBox(self, size=(200, -1), choices=IMAGE_NAME, style=wx.LB_SINGLE)
self.sizer = wx.BoxSizer(wx.VERTICAL)
btnSizer = wx.BoxSizer(wx.VERTICAL) #change to horizontal for side by side
self.sizerMain = wx.BoxSizer()
self.listBox.Bind(wx.EVT_LISTBOX_DCLICK, self.reName)
self.listBox.Bind(wx.EVT_LISTBOX, self.imagesel)
btn = wx.Button(self, label="Create New",size=(200, 40))
btnTwo = wx.Button(self, label="Test 2",size=(200, 40))
btn.Bind(wx.EVT_BUTTON, self.newAddImage)
self.sizer.Add(self.listBox, proportion=1, flag=wx.TOP | wx.EXPAND | wx.LEFT, border=5)
btnSizer.Add(btn, 0, wx.ALL, 5)
btnSizer.Add(btnTwo, 0, wx.ALL, 5)
self.sizer.Add(btnSizer)
self.sizerMain.Add(self.sizer, proportion=0, flag=wx.BOTTOM | wx.EXPAND, border=0)
self.SetSizer(self.sizerMain)
def imagesel(self, evt):
self.image_listsel = self.listBox.GetSelection()
sendnewpic(self)
def newAddImage(self, evt):
IMAGE_NAME.append('hi')
IMAGE_DATA.append('')
self.listBox.Set(IMAGE_NAME)
self.listBox.SetSelection(len(IMAGE_NAME)-1)
self.imagesel(None) #making it a selected image, globally
def reName(self,parent):
sel = self.listBox.GetSelection()
text = self.listBox.GetString(sel)
renamed = wx.GetTextFromUser('Rename item', 'Rename dialog', text)
if renamed != '':
IMAGE_NAME.pop(sel)
IMAGE_NAME.insert(sel,renamed)
self.listBox.Set(IMAGE_NAME)
self.listBox.SetSelection(sel)
class MyPanel(wx.Panel):
def __init__(self, *args, **kwargs):
wx.Panel.__init__(self, *args, **kwargs)
self.notebook = wx.Notebook(self, size=(225, -1))
#
self.tab_images = imageTab(self.notebook, self)
# add the pages to the notebook with the label to show on the tab
self.notebook.AddPage(self.tab_images, "Pics", select=True)
self.scroll_img = wx.ScrolledWindow(self, -1)
self.scroll_img.SetScrollbars(1, 1, 600, 400)
self.images_area = wx.StaticBox(self, -1, '')
self.sizerBox = wx.StaticBoxSizer(self.images_area, wx.HORIZONTAL)
self.sizerBox2 = wx.BoxSizer()
self.sizerBox.Add(self.scroll_img, 1, wx.EXPAND|wx.ALL, 10)
self.sizerBox2.Add(self.sizerBox, 1, wx.EXPAND|wx.ALL, 10)
self.sizer = wx.BoxSizer()
self.sizer.Add(self.notebook, proportion=0, flag=wx.EXPAND)
#
btnSizer = wx.BoxSizer() #change to horizontal for side by side
btnTwo = wx.Button(self, label="Load File", size=(200, 40))
btnTwo.Bind(wx.EVT_BUTTON, self.onOpenFile)
self.bmp = None
self.bitmap = None
btnSizer.Add(btnTwo, 0, wx.TOP, 15)
self.sizerBox2.Add(btnSizer)
#
self.sizer.Add(self.sizerBox2, proportion=1, flag=wx.EXPAND)
self.SetSizer(self.sizer)
self.notebook.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChanged)
areachange(self, self.notebook.GetPageText(0))
def OnClickTop(self, event):
self.scroll_img.Scroll(600, 400)
def OnClickBottom(self, event):
self.scroll_img.Scroll(1, 1)
def OnPageChanged(self, event):
new = event.GetSelection()
areachange(self, self.notebook.GetPageText(new))
event.Skip()
def OnPageChanging(self, event):
event.Skip()
def onOpenFile(self, evt):
""" Open a file"""
filename = wx.FileSelector()
if filename != '':
IMAGE_DATA[ self.tab_images.image_listsel] = filename
self.tab_images.imagesel(None)
print IMAGE_DATA
class MainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.panel = MyPanel(self)
self.Show()
app = wx.App(False)
win = MainWindow(None, size=(600, 400))
app.MainLoop()
Sometimes you are using bmp1 and bitmap1 as local variables and sometimes as globals. Since you are making multiple instances of them without saving the prior references anywhere then you are losing your references to the already existing objects. When you Destroy() them then you are only destroying the most recently created instances.
Try adding them to some sort of collection (like a list) instead and then you can access any of the items from the list when you need them later. Also try to avoid using global variables. Store your variables in the object instances that they belong to.

Categories