Show a limited time Message wxPython - python

I build an application in wxPython and in the application I want to show a message for a limited time and then, it will be disappeared by itself (the user of the application doesn't have to nothing to make it disappear)
I tried to do it like that, but it didn't closed by itself.
dialog = wx.MessageDialog(None,'message', wx.OK | wx.ICON_INFORMATION | wx.STAY_ON_TOP)
threading.Timer(2.0, dialog.Destroy).start()
dialog.ShowModal()
I also tried to do it like that and it either didn't do nothing:
dialog = wx.MessageDialog(None,'message', wx.OK | wx.ICON_INFORMATION | wx.STAY_ON_TOP)
threading.Timer(2.0, dialog.EndModal,args=wx.ID_OK).start()
dialog.ShowModal()

I don't have enough context to see what is the problem with your timer. You could try this however.
import wx
# ================================================================================
class TimedDialog(wx.Dialog):
def __init__(self, *args, **kwargs):
super(TimedDialog, self).__init__(*args, **kwargs)
self.SetSize((400, 300))
self.SetTitle('Please wait!')
self.Centre()
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimer)
self.timer.Start(2000) # 2 second interval
def OnTimer(self, event):
self.Close()
# ================================================================================
class Example(wx.Frame):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
self.SetSize((300, 200))
self.SetTitle('app')
self.Centre()
self.btn = wx.Button(self, -1, "click Me")
self.btn.Bind(wx.EVT_BUTTON, self.OnClicked)
def OnClicked(self, event):
dlg = TimedDialog(self)
dlg.ShowModal()
def main():
app = wx.App()
ex = Example(None)
ex.Show()
app.MainLoop()
if __name__ == '__main__':
main()

I got your dialog to close using dialog.DestroyLater but it was not consistent, anywhere from on time to 20 seconds late. Hardly what you wanted.
This is something I put together 2 years ago but can no longer remember why.
It's a bit over the top but hopefully it will help.
import wx
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "Busy Dialog",size=(500,200))
self.panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.VERTICAL)
self.log = wx.TextCtrl(self.panel, wx.ID_ANY, size=(400,100),style = wx.TE_MULTILINE|wx.TE_READONLY|wx.VSCROLL)
self.button = wx.Button(self.panel, label="Click me")
sizer.Add(self.log, 0, wx.EXPAND | wx.ALL, 10)
sizer.Add(self.button, 0, wx.EXPAND | wx.ALL, 10)
self.panel.SetSizer(sizer)
self.Bind(wx.EVT_BUTTON, self.OnButton)
def OnButton(self,event):
dlg = Busy(parent = self.panel)
dlg.ShowModal()
if dlg.result_text:
self.log.AppendText("Text Input: "+dlg.result_text+"\n")
dlg.Destroy()
class Busy(wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent, wx.ID_ANY, "Busy", size= (420,240))
self.panel = wx.Panel(self,wx.ID_ANY)
self.label = wx.StaticText(self.panel, label="Input", pos=(20,20))
self.textinput = wx.TextCtrl(self.panel, value="", pos=(80,20), size=(300,-1))
self.gauge = wx.Gauge(self.panel,size=(300,20),pos=(80,80), style=wx.GA_HORIZONTAL)
self.livelabel = wx.StaticText(self.panel, label="Time to live:", pos=(80,110))
self.lltime = wx.StaticText(self.panel, label="30", pos=(160,110))
self.saveButton =wx.Button(self.panel, label="Save Input", pos=(80,160))
self.closeButton =wx.Button(self.panel, label="Cancel", pos=(180,160))
self.timeoutButton =wx.Button(self.panel, label="Timer Off", pos=(280,160))
self.saveButton.Bind(wx.EVT_BUTTON, self.SaveBusyString)
self.closeButton.Bind(wx.EVT_BUTTON, self.OnQuit)
self.timeoutButton.Bind(wx.EVT_BUTTON, self.OnNoTimeout)
self.Bind(wx.EVT_CLOSE, self.OnQuit)
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER,self.OnTimer, self.timer)
self.lifetimer = wx.Timer(self)
self.Bind(wx.EVT_TIMER,self.OnLifeTimer, self.lifetimer)
self.timer.Start(100)
self.lifetimer.Start(1000)
self.timeoutbutton_pressed = False
self.gauge.SetBackgroundColour(wx.Colour(0, 127, 255, 255)) #Slate Blue
self.gauge.SetRange(100)
self.gauge.SetValue(0)
self.life = 30
self.direction = 1
self.result_text = None
self.Show()
def OnTimer(self, evt): #Update gauge
x = int(self.gauge.GetValue())
if x == 0:
self.direction = 1
elif x == 100:
self.direction = -1
x+=self.direction
self.gauge.SetValue(x)
def OnLifeTimer(self, evt): #Update time to live
if self.timeoutbutton_pressed == True:
return
self.life -= 1
self.lltime.SetLabelText(str(self.life))
if self.life < 1:
self.result_text = self.textinput.GetValue()
self.OnQuit(None)
def OnNoTimeout(self, evt): # toggle time to live
if self.timeoutbutton_pressed == False:
self.timeoutbutton_pressed = True
self.timeoutButton.SetLabel("Timer On")
else:
self.timeoutbutton_pressed = False
self.timeoutButton.SetLabel("Timer Off")
def OnQuit(self, event):
self.timer.Stop()
self.lifetimer.Stop()
self.Destroy()
def SaveBusyString(self, event): # return input
self.result_text = self.textinput.GetValue()
self.timer.Stop()
self.lifetimer.Stop()
self.Destroy()
app = wx.App()
frame = MyFrame(None)
frame.Show()
app.MainLoop()

Related

How do I make Find dialog use selections instead of SetStyles?

How do I make my text editor's Find function use selections instead of SetStyles? It's interfering with the Lexer and the self.tc.SetLexer(stc.STC_LEX_NULL) in this question. I don't wanna use SetStyles because I feel like it will interfere with other SetStyles in the future, so I wanna use selections instead, how do I do this?
PS: I am using Windows
Link to whats happening: imgur.com/a/oeLImVQ
Code:
import wx
import wx.stc as stc
import keyword
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.tc = stc.StyledTextCtrl(self, style=wx.TE_MULTILINE | wx.TE_WORDWRAP)
self.bt_find = wx.Button(self, -1, "find")
self.bt_css = wx.Button(self, -1, "CSS")
self.Bind(wx.EVT_BUTTON, self.on_button, self.bt_find)
self.Bind(wx.EVT_FIND, self.on_find)
self.Bind(wx.EVT_BUTTON, self.CSS, self.bt_css)
self.pos = 0
self.size = 0
#
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.tc, 1, wx.EXPAND, 0)
sizer.Add(self.bt_find, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
sizer.Add(self.bt_css, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
self.SetSizer(sizer)
sizer.Fit(self)
self.Layout()
def on_button(self, event):
self.txt = self.tc.GetValue()
self.data = wx.FindReplaceData() # initializes and holds search parameters
dlg = wx.FindReplaceDialog(self.tc, self.data, 'Find')
dlg.Show()
def on_find(self, event):
self.tc.StartStyling(pos=0, mask=0xFF)
self.tc.SetStyling(length=len(self.txt), style=0)
fstring = event.GetFindString()
self.size = len(fstring)
while True:
self.pos = self.txt.find(fstring, self.pos)
if self.pos < 0:
break
self.tc.StyleSetSpec(1, "fore:#FF0000,back:#000000")
self.tc.StartStyling(pos=self.pos, mask=0xFF)
self.tc.SetStyling(length=self.size, style=1)
self.pos += 1
self.pos = 0
def CSS(self, e):
self.tc.SetLexer(stc.STC_LEX_CSS)
self.tc.SetKeyWords(0, " ".join(keyword.kwlist))
self.tc.StyleSetSpec(wx.stc.STC_CSS_ATTRIBUTE, 'fore:#0000FF')
self.tc.StyleSetSpec(wx.stc.STC_CSS_CLASS, 'fore:#0000FF')
self.tc.StyleSetSpec(wx.stc.STC_CSS_COMMENT, 'fore:#008000')
self.tc.StyleSetSpec(wx.stc.STC_CSS_DEFAULT, 'fore:#000000')
self.tc.StyleSetSpec(wx.stc.STC_CSS_DIRECTIVE, 'fore:#0000FF')
self.tc.StyleSetSpec(wx.stc.STC_CSS_DOUBLESTRING, 'fore:#800080')
self.tc.StyleSetSpec(wx.stc.STC_CSS_ID, 'fore:#008080')
self.tc.StyleSetSpec(wx.stc.STC_CSS_IDENTIFIER, 'fore:#000000')
self.tc.StyleSetSpec(wx.stc.STC_CSS_IDENTIFIER2, 'fore:#000000')
self.tc.StyleSetSpec(wx.stc.STC_CSS_IMPORTANT, 'fore:#000000')
self.tc.StyleSetSpec(wx.stc.STC_CSS_OPERATOR, 'fore:#800000')
self.tc.StyleSetSpec(wx.stc.STC_CSS_PSEUDOCLASS, 'fore:#008080')
self.tc.StyleSetSpec(wx.stc.STC_CSS_SINGLESTRING, 'fore:#800080')
self.tc.StyleSetSpec(wx.stc.STC_CSS_TAG, 'fore:#008080')
self.tc.StyleSetSpec(wx.stc.STC_CSS_UNKNOWN_IDENTIFIER, 'fore:#000000')
self.tc.StyleSetSpec(wx.stc.STC_CSS_UNKNOWN_PSEUDOCLASS, 'fore:#008080')
self.tc.StyleSetSpec(wx.stc.STC_CSS_VALUE, 'fore:#668B8B')
if __name__ == "__main__":
app = wx.App()
frame_1 = MyFrame(None, wx.ID_ANY, "")
frame_1.Show()
app.MainLoop()
Thanks in advance

Can someone please add an image as background to this program? (WXPython)

Yeah, I know... i had asked a lot today (And so many thanks, Furas!). But look, I just need one thing and it'll be done. Could some one, please, for the love of God, add and image as background to this program with WXPython? It does not matter what image you put. Just put a random image, is the only thing i need. Thanks.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import wx, pygame, sys, random, os
from pygame.locals import *
from random import choice
from block import O, I, S, Z, L, J, T
class Example(wx.Frame):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
panel = wx.Panel(self)
hbox = wx.BoxSizer()
sizer = wx.GridSizer(2, 2, 2, 2)
btn1 = wx.Button(panel, label='Tetris')
btn2 = wx.Button(panel, label='Pong')
btn3 = wx.Button(panel, label='Brik')
sizer.AddMany([btn1, btn2, btn3])
hbox.Add(sizer, 150, wx.ALL, 200)
panel.SetSizer(hbox)
btn1.Bind(wx.EVT_BUTTON, self.Mensaje1)
btn2.Bind(wx.EVT_BUTTON, self.Mensaje2)
btn3.Bind(wx.EVT_BUTTON, self.Mensaje3)
self.SetSize((600, 500))
self.SetTitle('Messages')
self.Centre()
frame.Show(True)
def Mensaje1(self, event):
[stuffs...]
def Mensaje2(self, event):
[stuffs...]
def Mensaje3(self, event):
[stuffs...]
print "Hell, World"
def main():
ex = wx.App()
Example(None)
ex.MainLoop()
if __name__ == '__main__':
main()
Thanks again.
Use panel as a StaticBitmap's parent.
I removed some element to run this code.
import wx
class Example(wx.Frame):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(parent=None, *args, **kwargs)
self.InitUI()
def InitUI(self):
panel = wx.Panel(self)
try:
self.image_filename = "roses.jpg"
self.image = wx.Image(self.image_filename, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
# parent: panel
self.bitmap = wx.StaticBitmap(panel, -1, self.image, (0, 0))
except IOError:
print "Image file %s not found" % self.image_filename
raise SystemExit
hbox = wx.BoxSizer()
panel.SetSizer(hbox)
btn1 = wx.Button(panel, label='Tetris')
btn2 = wx.Button(panel, label='Pong')
btn3 = wx.Button(panel, label='Brik')
sizer = wx.GridSizer(2, 2, 2, 2)
sizer.AddMany([btn1, btn2, btn3])
hbox.Add(sizer, 150, wx.ALL, 200)
self.SetSize((600, 500))
self.SetTitle('Messages')
self.Centre()
self.Show()
if __name__ == '__main__':
ex = wx.App()
Example() # I moved None to super.__init__
ex.MainLoop()

Using wxPython to get input from user

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()

Deletion in wxpython's list control

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

Notebook widgets do not display properly unless we resize the window in wxPython?

im having a little issue with NoteBook switching. When I switch notebook tabs, I will need to resize to make the wigdets display properly. I tried using self.Refresh() but that does not seem to do anything. If you have trouble understanding me, please run the following code, then switch tabs and resize, you will notice that there is problems, displaying things correctly. I do not know if this is a problem with wxPython but I think it is with my code.
IMAGE_NAME = []
IMAGE_DATA = []
IMAGEMORE_NAME=[]
IMAGEMORE_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 == "Regular Pictures":
self.images_area.Show()
self.scroll_img.Show()
self.btnTwo.Show()
else:
self.images_area.Hide()
self.scroll_img.Hide()
self.btnTwo.Hide()
if pg == "More Pictures":
self.images_area.Show()
self.scroll_img.Show()
self.imageboxersiz.Show()
else:
self.imageboxersiz.Hide()
self.Refresh()
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 objectTab(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=IMAGEMORE_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()
def newAddImage(self, evt):
IMAGEMORE_NAME.append('New image')
IMAGEMORE_DATA.append('')
self.listBox.Set(IMAGEMORE_NAME)
self.listBox.SetSelection(len(IMAGEMORE_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 != '':
IMAGEMORE_NAME.pop(sel)
IMAGEMORE_NAME.insert(sel,renamed)
self.listBox.Set(IMAGEMORE_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)
self.notebook.AddPage(self.tab_images, "Regular Pictures", select=True)
self.tab_imagesmore = objectTab(self.notebook, self)
self.notebook.AddPage(self.tab_imagesmore, "More Pictures")
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(wx.VERTICAL) #change to horizontal for side by side
self.btnTwo = wx.Button(self, label="Load File", size=(200, 40))
self.bmp = None
self.bitmap = None
self.imageboxersiz=wx.ComboBox(self, -1, "None Selected!",(0, 0), (190,20),IMAGE_NAME, wx.CB_DROPDOWN)
btnSizer.Add(self.imageboxersiz, 0, wx.TOP, 15)
btnSizer.Add(self.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()
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()
Thank you very much.
Just change the self.Refresh() to self.Layout(). Worked for me on Windows 7 anyway.

Categories

Resources