Threading not working - python

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

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

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

Python PBI.PyBusyInfo stays on top

I have the code below which opens Quicktime via applescript and does stuff to files, which take a little while and mustn't be disturbed. I want a dialog window to open on top of everything else, not matter what it must stay on top, which just says "Processing files, please wait". This code works, but as soon as quicktime opens via applescript the PBI.PyBusyInfo disappears. Any idea how I could do this please?
import wx
import os
import os.path
import wx.lib.agw.pybusyinfo as PBI
from subprocess import Popen, PIPE
class ScrolledWindow(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(510, 370), style=wx.STAY_ON_TOP | wx.DEFAULT_FRAME_STYLE & ~ (wx.RESIZE_BORDER |
wx.RESIZE_BOX |
wx.MAXIMIZE_BOX))
self.tabbed = wx.Notebook(self, -1, style=(wx.NB_TOP))
run_params = {}
run_params["dropList1"] = ['HD 1920x1080', 'PAL 4x3', 'PAL 16x9', 'NTSC 4x3', 'NTSC 16x9']
run_params["dropList2"] = ['Progressive', 'Interlaced']
self.CreateStatusBar()
menuBar = wx.MenuBar()
menu = wx.Menu()
self.SetMenuBar(menuBar)
panel = wx.Panel(self, -1)
self.Centre()
self.Show()
self.filePrep = PrepFile(self.tabbed, run_params)
self.tabbed.AddPage(self.filePrep, "File Prep")
class PrepFile(wx.Panel):
def __init__(self, parent, run_params):
wx.Panel.__init__(self, parent)
self.run_params = run_params
self.fieldChoice = 'Progressive'
self.formatOption = 'HD 1920x1080'
outputOption = '''Format'''
wx.StaticText(self, -1, outputOption, (33, 22), style=wx.ALIGN_CENTRE)
self.choice1 = wx.Choice(self, pos=(35, 40), choices=self.run_params["dropList1"])
self.choice1.SetSelection(0)
self.choice1.SetFocus()
self.choice1.Bind(wx.EVT_CHOICE, self.selectOption)
fieldSetText = '''Fields'''
wx.StaticText(self, -1, fieldSetText, (33, 82), style=wx.ALIGN_CENTRE)
self.choice2 = wx.Choice(self, pos=(35, 100), choices=self.run_params["dropList2"])
self.choice2.SetSelection(0)
self.choice2.SetFocus()
self.choice2.Bind(wx.EVT_CHOICE, self.fieldSet)
self.buttonClose = wx.Button(self, -1, "Quit", pos=(195, 250))
self.buttonClose.Bind(wx.EVT_BUTTON, self.OnClose)
greyBox = wx.StaticBox(self, -1, '', pos=(20, 15), size=(235, 130))
outputtxt3 = '''Drag and Drop Quicktimes'''
wx.StaticText(self, -1, outputtxt3, pos=(35, 170), style=wx.ALIGN_CENTRE)
self.drop_target = MyFileDropTarget(self)
self.SetDropTarget(self.drop_target)
self.tc_files = wx.TextCtrl(self, wx.ID_ANY, pos=(38, 190), size=(200, 25))
self.buttonSubmit = wx.Button(self, -1, "Submit", pos=(250,190))
self.buttonSubmit.Bind(wx.EVT_BUTTON, self.submit)
def EvtRadioBox(self, event):
self.mode = (event.GetString())
def selectOption(self, e):
self.formatOption = self.choice1.GetStringSelection()
def fieldSet(self, e):
self.fieldChoice = self.choice2.GetStringSelection()
def setSubmissionDrop(self, dropFiles):
"""Called by the FileDropTarget when files are dropped"""
self.tc_files.SetValue(','.join(dropFiles))
self.selectedFiles = dropFiles
print self.selectedFiles
def submit(self, event):
event.Skip()
message = "Please wait..."
busy = PBI.PyBusyInfo(message, parent=self, title="Processing Files")
wx.Yield()
for item in self.selectedFiles:
if os.path.isdir(item):
print "It is a folder!"
for root, dirs, files in os.walk(item):
for file1 in files:
if file1.endswith(".mov"):
currentFile = os.path.join(root, file1)
self.jesFile(currentFile)
print "Finished"
del busy
def doSomething(self):
print "Open Quicktime and process files via applescript"
def OnClose(self, e):
CloseApp()
class MyFileDropTarget(wx.FileDropTarget):
""""""
def __init__(self, window):
wx.FileDropTarget.__init__(self)
self.window = window
def OnDropFiles(self, x, y, filenames):
self.window.setSubmissionDrop(filenames)
app = wx.App()
ScrolledWindow(None, -1, 'App')
app.MainLoop()
Your question seems to be missing the call where you start the AppleScript (it looks like you forgot to add the jesFile method to your example code). I can only speculate so much on your code that isn't in the posted example... but my guess is your call to do processing isn't a blocking call, so you either need to make it a blocking call, or save busy to a class variable that you delete when the non-blocking process call returns (hopefully you can bind a callback function for when that happens).

Why is my second label not updating?

For some reason, I can't update the second label. I tested it by asking for input and print out strings and it worked so I apparently did not write the label update function correctly. Any ideas?
import wx
import time
class LeftPanel(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id, style=wx.BORDER_SUNKEN)
self.text = parent.GetParent().rightPanel.text
self.text_2 = parent.GetParent().rightPanel.text_2
button1 = wx.Button(self, -1, 'Count', (10, 10))
button2 = wx.Button(self, -1, 'Countdown', (10, 60))
button3 = wx.Button(self, -1, 'Action', (10, 110))
self.Bind(wx.EVT_BUTTON, self.OnPlus, id=button1.GetId())
self.Bind(wx.EVT_BUTTON, self.OnMinus, id=button2.GetId())
self.Bind(wx.EVT_BUTTON, self.button_Pressed, id=button3.GetId())
self.timed_Out = 1
def OnPlus(self, event):
value = 1
for t in range(50):
value = value + 1
time.sleep(1)
wx.Yield()
self.text.SetLabel(str(value))
def OnMinus(self, event):
value = 60
for t in range(value):
value = value - 1
time.sleep(1)
wx.Yield()
self.text.SetLabel(str(value/60) + ':' + str(value%60))
self.timed_Out = 0
self.text_2.SetLabel(str('End o\'line.'))
def button_Pressed(self, event):
if self.timed_Out == 1:
if self.text_2 == 'First':
self.text_2.SetLabel('Second')
elif self.text_2 == 'Second':
self.text_2.SetLabel('First')
class RightPanel(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id, style=wx.BORDER_SUNKEN)
self.text = wx.StaticText(self, -1, '0', (10,60))
self.text_2 = wx.StaticText(self,-1,'First',(10, 120))
class Communicate(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(600, 200))
panel = wx.Panel(self, -1)
self.rightPanel = RightPanel(panel, -1)
leftPanel = LeftPanel(panel, -1)
hbox = wx.BoxSizer()
hbox.Add(leftPanel, 1, wx.EXPAND | wx.ALL, 4)
hbox.Add(self.rightPanel, 1, wx.EXPAND | wx.ALL, 5)
panel.SetSizer(hbox)
self.Centre()
self.Show(True)
app = wx.App()
Communicate(None, -1, 'widgets communicate')
app.MainLoop()
In button_Pressed, you're testing self.text_2 for equality with "First" or "Second", but text_2 is a StaticText object, not a string, so the test doesn't work. Try this:
if self.text_2.GetLabelText() == 'First':

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