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

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

Related

Updating Static label in wxpython on event trigger

I am building an application that will show basic info about a motorcycle such as RPM and the gear it is in. Say we change gear with a "keyboard button press", how could I get it to update the label. Suppose the equivalent key would be the UP key to go up, so far this is what I came up with but the label wont update when the event is being triggered. What may I be doing wrong?
import wx
import sys
var =int(sys.argv[1])
gr = "N"
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.SetSize((400, 300))
self.gauge_1 = wx.Gauge(self, wx.ID_ANY, 10000, style=wx.GA_HORIZONTAL | wx.GA_SMOOTH)
self.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
self.__set_properties()
self.__do_layout()
def __set_properties(self):
self.SetTitle("Test")
self.gauge_1.SetBackgroundColour(wx.Colour(216, 216, 191))
self.gauge_1.SetForegroundColour(wx.Colour(128, 0, 206))
self.gauge_1.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, "Ubuntu"))
self.gauge_1.SetValue(var)
def __do_layout(self):
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_1.Add(self.gauge_1, 0, wx.EXPAND, 0)
label_1 = wx.StaticText(self, wx.ID_ANY, "GEAR")
label_1.SetMinSize((100, 50))
label_1.SetForegroundColour(wx.Colour(0, 137, 215))
label_1.SetFont(wx.Font(25, wx.FONTFAMILY_MODERN, wx.FONTSTYLE_ITALIC, wx.FONTWEIGHT_NORMAL, 0, ""))
sizer_1.Add(label_1, 0, wx.ALL, 3)
Gearind = wx.StaticText(self, wx.ID_ANY, gr, style=wx.ALIGN_CENTER)
Gearind.SetMinSize((50, 43))
Gearind.SetForegroundColour(wx.Colour(122, 0, 7))
Gearind.SetFont(wx.Font(32, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_SLANT, wx.FONTWEIGHT_NORMAL, 0, ""))
sizer_1.Add(Gearind, 0, 0, 0)
self.SetSizer(sizer_1)
self.Layout()
def OnKeyUp(self, evt):
code = evt.GetKeyCode()
if code == wx.WXK_UP:
gr = "1"
self.Gearind.SetLabel(gr)
elif code == wx.WXK_DOWN:
evt.Skip()
class MyApp(wx.App):
def OnInit(self):
self.Test = MyFrame(None, wx.ID_ANY, "")
self.SetTopWindow(self.Test)
self.Test.Show()
return True
if __name__ == "__main__":
app = MyApp(0)
app.MainLoop()
I don't believe a Frame accepts characters.
Use a wx.Window with style wx.WANTS_CHARS or a wx.Panel
It won't help that you want to update self.Gearind but you defined it as a local i.e. Gearind.
Below, I've added a panel and a few other tweaks (note the gauge value).
You might want to investigate the wxpython SpeedMeter which may add some Vroooom! to your program.
import wx
import sys
try:
var =int(sys.argv[1])
except Exception:
var = 0
gr = "N"
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, wx.ID_ANY)
self.SetSize((400, 300))
self.panel = wx.Panel(self, wx.ID_ANY)
self.gauge_1 = wx.Gauge(self.panel, wx.ID_ANY, 6, style=wx.GA_HORIZONTAL | wx.GA_SMOOTH)
self.panel.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
self.__set_properties()
self.__do_layout()
def __set_properties(self):
self.SetTitle("Test")
#self.gauge_1.SetBackgroundColour(wx.Colour(216, 216, 191))
#self.gauge_1.SetForegroundColour(wx.Colour(128, 0, 206))
#self.gauge_1.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, "Ubuntu"))
self.gauge_1.SetValue(var)
def __do_layout(self):
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_1.Add(self.gauge_1, 0, wx.EXPAND, 0)
label_1 = wx.StaticText(self.panel, wx.ID_ANY, "GEAR")
label_1.SetMinSize((100, 50))
label_1.SetForegroundColour(wx.Colour(0, 137, 215))
label_1.SetFont(wx.Font(25, wx.FONTFAMILY_MODERN, wx.FONTSTYLE_ITALIC, wx.FONTWEIGHT_NORMAL, 0, ""))
sizer_1.Add(label_1, 0, wx.ALL, 3)
self.Gearind = wx.StaticText(self.panel, wx.ID_ANY, gr, style=wx.ALIGN_CENTER)
self.Gearind.SetMinSize((50, 43))
self.Gearind.SetForegroundColour(wx.Colour(122, 0, 7))
self.Gearind.SetFont(wx.Font(32, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_SLANT, wx.FONTWEIGHT_NORMAL, 0, ""))
sizer_1.Add(self.Gearind, 0, 0, 0)
self.panel.SetSizer(sizer_1)
self.Layout()
def OnKeyUp(self, evt):
code = evt.GetKeyCode()
gr = self.Gearind.GetLabel()
if gr.isnumeric():
gr_up = int(gr) + 1
gr_down = int(gr) - 1
else:
gr_up = 1
gr_down = 0
if gr_up > 6:
gr_up = 6
if gr_down < 1:
gr_down = "N"
if code == wx.WXK_UP:
self.Gearind.SetLabel(str(gr_up))
elif code == wx.WXK_DOWN:
self.Gearind.SetLabel(str(gr_down))
gr = self.Gearind.GetLabel()
if gr == "N":
var = 0
else:
var = int(gr)
self.gauge_1.SetValue(var)
evt.Skip()
class MyApp(wx.App):
def OnInit(self):
self.Test = MyFrame(None)
self.SetTopWindow(self.Test)
self.Test.Show()
return True
if __name__ == "__main__":
app = MyApp(0)
app.MainLoop()

Show a limited time Message wxPython

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

Threading not working

I am trying to get this little file copy application working which shows a progress bar, but I am not understanding why this won't work, as it locks up the gui whilst updating the gauge.
import shutil
import os
import threading
import wx
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title)
self.source = os.path.expanduser("~/Desktop/FolderToCopy")
self.destination = os.path.expanduser("~/Desktop/BackupFolder/Temp")
panel = wx.Panel(self, -1)
vbox = wx.BoxSizer(wx.VERTICAL)
hbox1 = wx.BoxSizer(wx.HORIZONTAL)
hbox2 = wx.BoxSizer(wx.HORIZONTAL)
hbox3 = wx.BoxSizer(wx.HORIZONTAL)
hbox4 = wx.BoxSizer(wx.HORIZONTAL)
self.getSourceSize = self.get_size(self.source)
self.gauge = wx.Gauge(panel, -1, self.getSourceSize, size=(150, 25))
self.btn1 = wx.Button(panel, wx.ID_OK)
self.abortButton = wx.Button(panel, label="Abort")
self.Bind(wx.EVT_BUTTON, self.OnButtonSelect, self.btn1)
self.abortButton.Bind(wx.EVT_BUTTON, self.OnAbortButton, self.abortButton)
hbox1.Add(self.gauge, 1, wx.ALIGN_CENTRE)
hbox2.Add(self.btn1, 1, wx.RIGHT, 10)
hbox4.Add(self.abortButton, 1, wx.RIGHT, 20)
vbox.Add((0, 50), 0)
vbox.Add(hbox1, 0, wx.ALIGN_CENTRE)
vbox.Add((0, 30), 0)
vbox.Add(hbox2, 1, wx.ALIGN_CENTRE)
vbox.Add(hbox4, 1, wx.ALIGN_CENTRE)
panel.SetSizer(vbox)
self.Centre()
def OnAbortButton(self, e):
self.shouldAbort = True
def get_size(self, start_path):
total_size = 0
for dirpath, dirnames, filenames in os.walk(start_path):
for f in filenames:
fp = os.path.join(dirpath, f)
total_size += os.path.getsize(fp)
total_size = total_size / 50
return total_size
def OnButtonSelect(self, event):
thread1 = threading.Thread(target=shutil.copytree, args=(self.source, self.destination))
thread1.start()
self.thread1 = threading.Thread(target=self.OnGo(self))
self.thread1.start()
def OnCopy(self):
shutil.copytree(self.source, self.destination)
def OnGo(self, event):
self.shouldAbort = False
getDestinationSize = 0
get_size = self.get_size
while getDestinationSize < self.getSourceSize:
getDestinationSize = get_size(self.destination)
self.gauge.SetValue(getDestinationSize)
if self.shouldAbort:
break
app = wx.App(0)
frame = MyFrame(None, -1, 'gauge.py')
frame.Show(True)
app.MainLoop()
self.thread1 = threading.Thread(target=self.OnGo(self))
self.thread1.start()
You're executing self.onGo on this line, before the thread even starts. Arguments to the target should be passed in via args. Since OnGo never actually uses its event parameter, you can just pass in None.
self.thread1 = threading.Thread(target=self.OnGo, args=(None,))
self.thread1.start()

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

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