How can wxThumbnailCtrl in wxpython be used? - python

I think this is a new widget and theres no examples online about this. im not sure how i should start using it or how it can be used.

Here is simple example to get you started. self.button selects directory with images; on doubleclick on any of thumbnails a messagebox is shown with info on selected thumb.
import wx
from wx.lib.agw import thumbnailctrl as tn
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
wx.Frame.__init__(self, *args, style=wx.DEFAULT_FRAME_STYLE)
self.button = wx.Button(self, -1, "Select dir")
self.Bind(wx.EVT_BUTTON, self.ButtonPress, self.button)
self.tn = tn.ThumbnailCtrl(self)
self.tn.Bind(tn.EVT_THUMBNAILS_DCLICK, self.TnClick)
box = wx.BoxSizer(wx.VERTICAL)
box.Add(self.tn, 1, wx.EXPAND, 0)
box.Add(self.button, 0, wx.ADJUST_MINSIZE, 0)
self.SetSizer(box)
box.Fit(self)
self.Layout()
def ButtonPress(self, evt):
dlg = wx.DirDialog(self, 'Get dir')
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
dlg.Destroy()
self.tn.ShowDir(path)
def TnClick(self, evt):
sel = self.tn.GetSelection()
wx.MessageBox(self.tn.GetThumbInfo(sel))
if __name__ == "__main__":
app = wx.PySimpleApp(0)
frame = MyFrame(None, -1, "")
frame.Show()
app.MainLoop()

Related

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

How to delete an animated gif from a frame with wxPython?

I can't for the life of me figure out how to destroy or hide a gif from a wxPython frame.
Here is the example code:
import wx
import wx.animate
########################################################################
class MyForm(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "GIF frame")
panel = wx.Panel(self, wx.ID_ANY)
btn1 = wx.Button(self, -1, "Start GIF",(50,10))
btn1.Bind(wx.EVT_BUTTON, self.onButton1)
btn2 = wx.Button(self, -1, "Stop GIF",(50,40))
btn2.Bind(wx.EVT_BUTTON, self.onButton2)
#----------------------------------------------------------------------
def onButton1(self, event):
self.animateGIF()
#----------------------------------------------------------------------
def onButton2(self, event):
self.animateGIF(False)
#----------------------------------------------------------------------
def animateGIF(self, state=True):
gif_fname = "test.gif"
gif = wx.animate.GIFAnimationCtrl(self, -1, gif_fname,pos=(50,70),size=(10,10))
gif.GetPlayer()
if state:
gif.Play()
else:
gif.Stop()
#gif.Destroy(), gif.Hide() have no effect besides cancelling the Stop() function.
#----------------------------------------------------------------------
# Run the program
app = wx.App()
frame = MyForm().Show()
app.MainLoop()
So, how do I go about deleting this gif from my frame ?
Thank you! I hope the code is clear enough.
I believe you are loading a new GIF every time you call animateGIF. I suggest the following, though I am sure this can be improved:
import wx
import wx.animate
########################################################################
class MyForm(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "GIF frame")
# panel not used in this example
#panel = wx.Panel(self, wx.ID_ANY)
self.btn1 = wx.Button(self, -1, "Start GIF",(50,10))
self.btn1.Bind(wx.EVT_BUTTON, self.onButton1)
self.btn2 = wx.Button(self, -1, "Stop GIF",(50,40))
self.btn2.Bind(wx.EVT_BUTTON, self.onButton2)
self.gif = None
#----------------------------------------------------------------------
def onButton1(self, event):
self.animateGIF()
#----------------------------------------------------------------------
def onButton2(self, event):
self.animateGIF(False)
#----------------------------------------------------------------------
def animateGIF(self, state=True):
if self.gif == None:
gif_fname = "test.gif"
self.gif = wx.animate.GIFAnimationCtrl(self, -1, gif_fname,pos=(50,70),size=(10,10))
# call to GetPlayer was unnecessary
#gif.GetPlayer()
if state:
self.gif.Play()
else:
self.gif.Stop()
self.gif.Destroy()
self.gif = None
#----------------------------------------------------------------------
# Run the program
app = wx.PySimpleApp()
frame = MyForm().Show()
app.MainLoop()

pop up a window with text in it and automatically close it after certain time

This is the code that i have written. It does close the window but doesnt display the text in it. I need the text displayed and then automatic close of the window.
What changes should i make for it to work
Thanks
Here is the code
import wx
from time import sleep
class Frame(wx.Frame):
def __init__(self, title):
wx.Frame.__init__(self, None, title=title, size=(300,200))
self.panel = wx.Panel(self)
box = wx.BoxSizer(wx.VERTICAL)
m_text = wx.StaticText(self.panel, -1, 'File Uploaded!')
m_text.SetSize(m_text.GetBestSize())
box.Add(m_text, 0, wx.ALL, 10)
self.panel.SetSizer(box)
self.panel.Layout()
self.Bind(wx.EVT_ACTIVATE, self.onClose)
def onClose(self, event):
sleep(5)
self.Destroy()
app = wx.App(redirect=True)
top = Frame('test')
top.Show()
app.MainLoop()
I would recommend using a wx.Timer. If you use time.sleep(), you will block wxPython's main loop which makes your application unresponsive. Here is your code modified to use the timer:
import wx
class Frame(wx.Frame):
def __init__(self, title):
wx.Frame.__init__(self, None, title=title, size=(300,200))
self.panel = wx.Panel(self)
box = wx.BoxSizer(wx.VERTICAL)
m_text = wx.StaticText(self.panel, -1, 'File Uploaded!')
m_text.SetSize(m_text.GetBestSize())
box.Add(m_text, 0, wx.ALL, 10)
self.panel.SetSizer(box)
self.panel.Layout()
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.onClose, self.timer)
self.timer.Start(5000)
def onClose(self, event):
self.Close()
app = wx.App(redirect=True)
top = Frame('test')
top.Show()
app.MainLoop()
You can read more about timers in this article:
http://www.blog.pythonlibrary.org/2009/08/25/wxpython-using-wx-timers/
>>> import wx
>>> import time
>>> app = wx.App()
>>> b = wx.BusyInfo('Upload Finished!')
>>> time.sleep(5)
>>> del b
>>>

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

AuiNotebook, where did the event happend

How can I find out from which AuiNotebook page an event occurred?
EDIT: Sorry about that. Here are a code example. How do I find the notebook page
from witch the mouse was clicked in?
#!/usr/bin/python
#12_aui_notebook1.py
import wx
import wx.lib.inspection
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
wx.Frame.__init__(self, *args, **kwds)
self.nb = wx.aui.AuiNotebook(self)
self.new_panel('Pane 1')
self.new_panel('Pane 2')
self.new_panel('Pane 3')
def new_panel(self, nm):
pnl = wx.Panel(self)
self.nb.AddPage(pnl, nm)
self.sizer = wx.BoxSizer()
self.sizer.Add(self.nb, 1, wx.EXPAND)
self.SetSizer(self.sizer)
pnl.Bind(wx.EVT_LEFT_DOWN, self.click)
def click(self, event):
print 'Mouse click'
#How can I find out from witch page this click came from?
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, '12_aui_notebook1.py')
frame.Show()
self.SetTopWindow(frame)
return 1
if __name__ == "__main__":
app = MyApp(0)
# wx.lib.inspection.InspectionTool().Show()
app.MainLoop()
Oerjan Pettersen
For a mouse click you can assume the current selected page is the one that got the click. I added a few lines to your code. See comments
def new_panel(self, nm):
pnl = wx.Panel(self)
# just to debug, I added a string attribute to the panel
# don't you love dynamic languages? :)
pnl.identifierTag = nm
self.nb.AddPage(pnl, nm)
self.sizer = wx.BoxSizer()
self.sizer.Add(self.nb, 1, wx.EXPAND)
self.SetSizer(self.sizer)
pnl.Bind(wx.EVT_LEFT_DOWN, self.click)
def click(self, event):
print 'Mouse click'
# get the current selected page
page = self.nb.GetPage(self.nb.GetSelection())
# notice that it is the panel that you created in new_panel
print page.identifierTag

Categories

Resources