I want to start playing a .wmv file as soon as it is loaded in wxPython. I wrote a small code for it. The code does not give any error, but it does not show any video as well, but the sound does play in the background. It just shows a gray screen. Following is the code I wrote for my program.
import wx
import wx.media
class TestPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, -1, style=wx.TAB_TRAVERSAL|wx.CLIP_CHILDREN)
# Create some controls
try:
self.mc = wx.media.MediaCtrl(self, style=wx.SIMPLE_BORDER)
except NotImplementedError:
self.Destroy()
raise
self.mc.Load(r"C:\Documents and Settings\N1002401B\Desktop\test1.wmv")
#self.slider.SetRange(0, self.mc.Length())
#folder, filename = os.path.split("C:\Documents and Settings\N1002401B\Desktop\test1.wmv")
self.Bind(wx.media.EVT_MEDIA_LOADED, self.OnPlay)
def OnPlay(self,evt):
self.mc.Play()
app = wx.App(0)
frame = wx.Frame(None)
panel = TestPanel(frame)
frame.Show()
app.MainLoop()
I am using Python 2.7 and windows XP.
Can anyone please help me on this one. I would be really grateful for your help.
Related
I try to make a download interface via tkinter. The following code works well in Pycharm. But when I try to make it an exe file using pyinstaller, it opens a new window every time I click on button1(the download button), which is undesired. Could you please tell me how to fix it? I'm working on Win10. Thanks in advance.
import tkinter as tk
from download import download_yv as dyv
import multiprocessing
class DownloadPage(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.grid()
self.download_flag=True
self.current_row=0
self.current_rowspan=0
self.label1=tk.Label(self,text='Url:',)
self.label1.grid(row=self.current_row,column=0,)
self.text1=tk.Entry(self,width=45)
self.text1.grid(row=self.current_row,column=1,padx=0,pady=0)
self.current_row+=1
self.label2=tk.Label(self,text='Save_Dir:',wraplength=200,)
self.label2.grid(row=self.current_row,column=0)
self.text2=tk.Entry(self,width=45)
self.text2.grid(row=self.current_row,column=1,padx=0,pady=0,)
self.current_row+=1
self.label3=tk.Label(self,text='Overwrite:',)
self.label3.grid(row=self.current_row,column=0)
self.var1=tk.BooleanVar(self)
self.var1.set('False')
self.menu1=tk.OptionMenu(self,self.var1,'True','False')
self.current_rowspan=2
self.menu1.grid(row=self.current_row,column=1,columnspan=2,rowspan=self.current_rowspan,sticky='news')
self.current_row+=self.current_rowspan
self.label4=tk.Label(self,text='Categorize:')
self.label4.grid(row=self.current_row,column=0)
self.var2 = tk.BooleanVar(self)
self.var2.set(True)
self.menu2=tk.OptionMenu(self,self.var2,'True','False')
self.menu2.grid(row=self.current_row,column=1,columnspan=2,rowspan=self.current_rowspan,sticky='news')
self.current_row+=self.current_rowspan
self.current_rowspan=2
self.button1=tk.Button(self,text='download',command=self.download)
self.button1.grid(row=self.current_row,column=1,columnspan=2,rowspan=self.current_rowspan,sticky='news')
self.current_row+=self.current_rowspan
self.button2=tk.Button(self,text='Stop Downloading',command=self.stop_download)
self.button2.grid(row=self.current_row,column=1,columnspan=1,)
def download(self):
var1=self.var1.get()
var2=self.var2.get()
# print(var1,type(var1),var2,type(var2))
self.download_process=multiprocessing.Process(target=dyv,args=(self.text1.get(),self.text2.get(),var1,var2))
self.download_process.start()
def stop_download(self):
if self.download_process and self.download_process.is_alive():
self.download_process.terminate()
if __name__ == '__main__':
root=tk.Tk()
app=DownloadPage(root)
app.mainloop()
I am following this example https://github.com/cztomczak/cefpython/blob/master/examples/wxpython.py for cefpython3 and wxpython. However I am looking to disable the windows border so the program can no longer be adjusted size wise and their is no title bar. But I would like to keep it so the program can be moved around the screen. I have done some research but not come across a way to successfully do this.
The following appears to do the job on Linux.
Your mileage may vary.
import wx
class MyFrame(wx.Frame):
def __init__(self, parent, id=wx.ID_ANY, title="", size=(360,100)):
super(MyFrame, self).__init__(parent, id, title, size, style = wx.DEFAULT_FRAME_STYLE & ~(wx.RESIZE_BORDER | wx.MAXIMIZE_BOX))
self.panel = wx.Panel(self)
self.panel.SetBackgroundColour('palegreen')
self.Show()
if __name__ == "__main__":
app = wx.App()
frame = MyFrame(None,title="Non-Resizeable Frame")
app.MainLoop()
Database Loading Panel Screen StaticText is Blurred Out when Shown
Hello,
I have an odd issue with a loading screen. The problem is this: When I start with a main screen, then when you click on the 'next' button,
it goes to the database loading screen, when it is done, it prints on the screen that the Database Loading is complete.
The problem is that the loading screen text is blurred out for some reason. Also, something I discovered is that if you pop up a message dialog
box, the loading text is shown...
In this example, I am not actually loading into an actual database. Instead, I am just sleeping for seven seconds.
If you are confused as to what I am saying, just run the example, and you will see that the loading screen is all messed up...
Here is the code:
import wx
import time
class TextPanel(wx.Panel):
def __init__(self, parent, label):
self.__myParent = parent
wx.Panel.__init__(self, self.__myParent, size = (800, 800))
staticText = wx.StaticText(self, label = label)
class MainPanel(wx.Panel):
def __init__(self, parent):
self.__myParent = parent
wx.Panel.__init__(self, self.__myParent, size = (800, 800))
nextButton = wx.Button(self, label = 'Next')
nextButton.Bind(wx.EVT_BUTTON, self.__onNext)
def __onNext(self, event):
self.__myParent.onNextScreen()
class MainFrame(wx.Frame):
def __init__(self):
# Base contsructor.
wx.Frame.__init__(self, None, id = -1, title = 'Testing...', size = (800, 800))
self.__myMainPanel = MainPanel(self)
self.__myMainPanel.Show()
self.__myDatabase = TextPanel(self, 'Loading Data...')
self.__myDatabase.Hide()
self.__myFinalPanel = TextPanel(self, 'Database Loading Complete!')
self.__myFinalPanel.Hide()
def onNextScreen(self):
self.__myMainPanel.Hide()
self.__myDatabase.Show()
self.doDatabaseLoad()
self.__myDatabase.Hide()
self.__myFinalPanel.Show()
def doDatabaseLoad(self):
time.sleep(7) # before, this method would load data into a database...
if __name__ == '__main__':
app = wx.App()
frame = MainFrame()
frame.Show(True)
app.MainLoop()
print 'Exiting...'
Thanks all for your help,
I found an easy solution to this issue.
I first found out that wx.Panel.Update() gets called to repaint the screen when ever the event handler method returns. So, the loading panel seems to look like it needed to be repainted, as the static text box is all grayed out. I then just called self.__myDatabase.Update() after self.__myDatabase.Show().
Here is the new version of onNextScreen(self):
def onNextScreen(self):
self.__myMainPanel.Hide()
self.__myDatabase.Show()
self.__myDatabase.Update() # Fixes the bug...
self.doDatabaseLoad()
self.__myDatabase.Hide()
self.__myFinalPanel.Show()
Also, another way to fix it is to call SetLabel on the static text, but the above solution is better. It just seems that Update was not being called automatically, because it was in the middle of an event...
Am trying to display image in text control but it display only binary characters.
But is their any way I can archive this or its impossible dream to do it in wxpython
Please help I will need this agently.
Thank you advance
Here are the source codes that I have so far
import wx
class MainFrame(wx.Frame):
def __init__(self,*args,**kwargs):
super(MainFrame,self).__init__(*args,**kwargs)
self.main_panel = MainPanel(self,-1)
class MainPanel(wx.Panel):
def __init__(self,*args,**kwargs):
super(MainPanel,self).__init__(*args,**kwargs)
img1 = wx.Image("coins.png", wx.BITMAP_TYPE_ANY)
w = img1.GetWidth()
h = img1.GetHeight()
img1 = img1.Scale(w/2, h/2)
sb1 = wx.StaticBitmap(self, -1, wx.BitmapFromImage(img1))
self.txtctrl = wx.TextCtrl(self,-1,"display image here",size=(500,300),pos=(20,10))
class App(wx.App):
def OnInit(self):
mainframe = MainFrame(None,-1,title="Display image in txt ctrl",size=(600,400))
mainframe.Show()
mainframe.Center()
return True
if __name__ == "__main__":
app = App();
app.MainLoop()
You cannot put an image directly inside a regular wx.TextCtrl widget. That is currently impossible as they just don't support that. However, you can put an image into a RichTextCtrl widget. If you haven't downloaded it yet, be sure to get the wxPython demo from the project's website as it has a good example. Here are a couple links:
http://wxpython.org/Phoenix/docs/html/richtext.RichTextCtrl.html
http://play.pixelblaster.ro/blog/archive/2008/10/08/richtext-control-with-wxpython-saving-and-loading
If you just want to put an image in your application, then wx.Image is your friend (as John already mentioned).
You can do it with wx.Image read this
you can also see a more complex example here
I am new to python. I am trying to write a motion detection app. Currently, I am trying to get the webcam video to display on the screen. Current code right now has no flicker at first, but after any resizing, the flicker will come back. Any clue? Also, why doesn't it work without self.Refresh() in the timer event, isn't paint event always happening unless the frame is minimized? Thanks in advance.
import wx
import cv
class LiveFrame(wx.Frame):
fps = 30
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, title="Live Camera Feed")
self.SetDoubleBuffered(True)
self.capture = None
self.bmp = None
#self.displayPanel = wx.Panel(self,-1)
#set up camaera init
self.capture = cv.CaptureFromCAM(0)
frame = cv.QueryFrame(self.capture)
if frame:
cv.CvtColor(frame,frame,cv.CV_BGR2RGB)
self.bmp = wx.BitmapFromBuffer(frame.width,frame.height,frame.tostring())
self.SetSize((frame.width,frame.height))
self.displayPanel = wx.Panel(self,-1)
self.fpstimer = wx.Timer(self)
self.fpstimer.Start(1000/self.fps)
self.Bind(wx.EVT_TIMER, self.onNextFrame, self.fpstimer)
self.Bind(wx.EVT_PAINT, self.onPaint)
self.Show(True)
def updateVideo(self):
frame = cv.QueryFrame(self.capture)
if frame:
cv.CvtColor(frame,frame,cv.CV_BGR2RGB)
self.bmp.CopyFromBuffer(frame.tostring())
self.Refresh()
def onNextFrame(self,evt):
self.updateVideo()
#self.Refresh()
evt.Skip()
def onPaint(self,evt):
#if self.bmp:
wx.BufferedPaintDC(self.displayPanel, self.bmp)
evt.Skip()
if __name__=="__main__":
app = wx.App()
app.RestoreStdio()
LiveFrame(None)
app.MainLoop()
I have found the solution to this problem. The flickering came from the panel clearing its background. I had to create a panel instance and have its EVT_ERASE_BACKGROUND bypass. Another thing is that I had to put the webcam routine inside that panel and have BufferPaintedDC drawing on the panel itself. For some reason, flicker still persist if wx.BufferedPaintedDC is drawing from the frame to self.displaypanel .
When you're drawing, you just have to call Refresh. It's a requirement. I don't remember why. To get rid of flicker, you'll probably want to read up on DoubleBuffering: http://wiki.wxpython.org/DoubleBufferedDrawing
Or maybe you could use the mplayer control. There's an example here: http://www.blog.pythonlibrary.org/2010/07/24/wxpython-creating-a-simple-media-player/