hello I've this python code:

import wx 
class MyFrame(wx.Frame): 
	def __init__(self, parent=None, id=-1, title=None): 
		wx.Frame.__init__(self, parent, id, title)
		self.panel_1 = wx.Panel(self, -1)
		self.panel_1.Bind(wx.EVT_PAINT, self.on_paint) 
		self.image = wx.Image('foto.jpg')
		# Menu Bar
		self.frame_1_menubar = wx.MenuBar()
		self.File = wx.Menu()
		self.Test = wx.MenuItem(self.File, wx.NewId(), "Test", "", wx.ITEM_NORMAL)
		self.File.AppendItem(self.Test)
		self.frame_1_menubar.Append(self.File, "File")
		self.SetMenuBar(self.frame_1_menubar)
		# Menu Bar end
		self.frame_1_statusbar = self.CreateStatusBar(1, 0)
		self.panel_1.Bind(wx.EVT_LEFT_UP, self.OnMove)
		self.__set_properties()
		self.__do_layout()
		# end wxGlade

	def __set_properties(self):
		# begin wxGlade: MyFrame.__set_properties
		#self.SetTitle("frame_1")
		self.panel_1.SetMinSize((480, 272))
		self.panel_1.SetMaxSize((480, 272))
		self.panel_1.SetBackgroundColour(wx.Colour(0, 0, 0))
		self.frame_1_statusbar.SetStatusWidths([-1])
		# statusbar fields
		frame_1_statusbar_fields = ["frame_1_statusbar"]
		for i in range(len(frame_1_statusbar_fields)):
			self.frame_1_statusbar.SetStatusText(frame_1_statusbar_fields[i], i)
		# end wxGlade

	def __do_layout(self):
		# begin wxGlade: MyFrame.__do_layout
		sizer_1 = wx.BoxSizer(wx.VERTICAL)
		sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
		sizer_3 = wx.BoxSizer(wx.VERTICAL)
		sizer_2.Add(self.panel_1, 1, wx.ALIGN_CENTER_VERTICAL, 0)
		sizer_2.Add(sizer_3, 1, wx.EXPAND, 0)
		sizer_1.Add(sizer_2, 1, 0, 0)
		#self.linea = wx.StaticLine(self.panel_1, pos=(0, 5), size=(0,wx.EXPAND))
		#self.panel_1.Add(self.static_line_1, 0, wx.EXPAND, 0)
		#self.linea.SetBackgroundColour(wx.Colour(255, 0, 0))
		self.SetSizer(sizer_1)
		sizer_1.Fit(self)
		self.Layout()
		# end wxGlade
		
	def on_paint(self, event):
		self.dc = wx.PaintDC(self.panel_1)
		self.dc.SetBackground(wx.Brush('black'))
		self.lineaorizzontale(34)
		self.lineaverticale(34)
		bitmap = wx.BitmapFromImage(self.image)
		self.dc.DrawBitmap(bitmap, 0,0)
		event.Skip()
		
	def lineaorizzontale(self, y):
		self.dc.SetPen(wx.Pen('blue', 1))
		self.dc.DrawLine(0, y, 200, y)

	def lineaverticale(self, x):
		self.dc.SetPen(wx.Pen('blue', 1))
		self.dc.DrawLine(x, 0, x, 200)

	def OnMove(self, event):
		self.dc.Clear()
		bitmap = wx.BitmapFromImage(self.image)
		self.dc.DrawBitmap(bitmap, 0,0)
		pos = event.GetPosition()
		self.lineaorizzontale(pos.y)
		self.lineaverticale(pos.x)
		print "%s, %s" % (pos.x, pos.y)
		event.Skip()
		
# test it ...
app = wx.PySimpleApp() 
frame1 = MyFrame(title='rounded-rectangle & circle') 
frame1.Center() 
frame1.Show() 
app.MainLoop()

but I don't know why it doesn't not work properly.
Can you help me?

sorry for my english

Recommended Answers

All 8 Replies

What does not work properly?
What do you expect to happen?

What does not work properly?
What do you expect to happen?

It wasn't drawing properly the line, but I made an error:

def lineaorizzontale(self, y):
	self.dc.SetPen(wx.Pen('blue', 1))
	self.dc.DrawLine(0, y, 200, y)
def lineaverticale(self, x):
	self.dc.SetPen(wx.Pen('blue', 1))
	self.dc.DrawLine(x, 0, x, 200)

I've inserted the wrong height and width :)

def lineaorizzontale(self, y):
	self.dc.SetPen(wx.Pen('blue', 1))
	self.dc.DrawLine(0, y, 480, y)
def lineaverticale(self, x):
	self.dc.SetPen(wx.Pen('blue', 1))
	self.dc.DrawLine(x, 0, x, 272)

But now I have another problem:
The program doesn't work when the window is resized (doesn't, too, draw the line properly)
I attach some screenshoot ;)

When you draw your crosshairs, you have to reference the image height and width:

def lineaorizzontale(self, y):
        self.dc.SetPen(wx.Pen('blue', 1))
        w = self.image.GetWidth()
        #print w
        self.dc.DrawLine(0, y, w, y)

    def lineaverticale(self, x):
        self.dc.SetPen(wx.Pen('blue', 1))
        h = self.image.GetHeight()
        #print h
        self.dc.DrawLine(x, 0, x, h)

When you draw your crosshairs, you have to reference the image height and width:

def lineaorizzontale(self, y):
        self.dc.SetPen(wx.Pen('blue', 1))
        w = self.image.GetWidth()
        #print w
        self.dc.DrawLine(0, y, w, y)

    def lineaverticale(self, x):
        self.dc.SetPen(wx.Pen('blue', 1))
        h = self.image.GetHeight()
        #print h
        self.dc.DrawLine(x, 0, x, h)

Thanks for the reply :) but it doesn't not work :(

When I resize the frame it works just fine.

It might be these two lines that give you problems with a larger picture:

self.panel_1.SetMinSize((480, 272))
		self.panel_1.SetMaxSize((480, 272))

When I resize the frame it works just fine.

It might be these two lines that give you problems with a larger picture:

self.panel_1.SetMinSize((480, 272))
		self.panel_1.SetMaxSize((480, 272))

No :(
If I remove the image the program doesn't work properly :(

Edit:

If I add this function on evt_size It does work when I enlarge the window but not when I reduce it (or when I open a menu) :(

def OnResize(self, event):
	self.dc.Clear()

Ps did you try the script?

I am a little confused here. What are your resizing, the frame, the panel, the image?

I changed your code so I could see the meat and used it with a larger image ...

import wx 

class MyFrame(wx.Frame): 
    def __init__(self, parent=None, id=-1, title=None): 
        wx.Frame.__init__(self, parent, id)
        self.panel_1 = wx.Panel(self, -1)
        self.panel_1.Bind(wx.EVT_PAINT, self.on_paint)
        # use an image file you have
        self.filename = 'lake.gif'    #'foto.jpg' 
        self.image = wx.Image(self.filename)
        self.w = self.image.GetWidth()
        self.h = self.image.GetHeight()
        self.title = "%s  w=%d h=%d" % (self.filename, self.w, self.h)
        self.SetTitle(self.title)
        self.frame_1_statusbar = self.CreateStatusBar(1, 0)
        self.panel_1.Bind(wx.EVT_LEFT_UP, self.OnMove)
        self.__set_properties()
        self.__do_layout()

    def __set_properties(self):
        # modified so the image would fit
        self.panel_1.SetMinSize((self.w, self.h))
        self.panel_1.SetMaxSize((self.w, self.h))
        self.panel_1.SetBackgroundColour(wx.Colour(0, 0, 0))
        self.frame_1_statusbar.SetStatusWidths([-1])
        # statusbar fields
        frame_1_statusbar_fields = ["click on image"]
        for i in range(len(frame_1_statusbar_fields)):
            self.frame_1_statusbar.SetStatusText(frame_1_statusbar_fields[i], i)

    def __do_layout(self):
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_3 = wx.BoxSizer(wx.VERTICAL)
        sizer_2.Add(self.panel_1, 1, wx.ALIGN_CENTER_VERTICAL, 0)
        sizer_2.Add(sizer_3, 1, wx.EXPAND, 0)
        sizer_1.Add(sizer_2, 1, 0, 0)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        self.Layout()
    
    def on_paint(self, event):
        self.dc = wx.PaintDC(self.panel_1)
        #self.dc.Clear()
        self.dc.SetBackground(wx.Brush('black'))
        self.line_horizontal(0)
        self.line_vertical(0)
        bitmap = wx.BitmapFromImage(self.image)
        self.dc.DrawBitmap(bitmap, 0,0)
        event.Skip()
    
    def line_horizontal(self, y):
        self.dc.SetPen(wx.Pen('blue', 1))
        self.dc.DrawLine(0, y, self.w, y)

    def line_vertical(self, x):
        self.dc.SetPen(wx.Pen('blue', 1))
        self.dc.DrawLine(x, 0, x, self.h)

    def OnMove(self, event):
        self.dc.Clear()
        bitmap = wx.BitmapFromImage(self.image)
        self.dc.DrawBitmap(bitmap, 0,0)
        pos = event.GetPosition()
        self.line_horizontal(pos.y)
        self.line_vertical(pos.x)
        position = "   x=%s  y=%s" % (pos.x, pos.y)
        new_title = self.title + position
        self.SetTitle(new_title)


# test it ...
app = wx.PySimpleApp() 
frame1 = MyFrame()
frame1.Center() 
frame1.Show() 
app.MainLoop()

Seems to work just fine on my Windows XP machine. What OS do you have?

I am a little confused here. What are your resizing, the frame, the panel, the image?

I'm resizing the frame :)

I changed your code so I could see the meat and used it with a larger image ...

import wx 

class MyFrame(wx.Frame): 
    def __init__(self, parent=None, id=-1, title=None): 
        wx.Frame.__init__(self, parent, id)
        self.panel_1 = wx.Panel(self, -1)
        self.panel_1.Bind(wx.EVT_PAINT, self.on_paint)
        # use an image file you have
        self.filename = 'lake.gif'    #'foto.jpg' 
        self.image = wx.Image(self.filename)
        self.w = self.image.GetWidth()
        self.h = self.image.GetHeight()
        self.title = "%s  w=%d h=%d" % (self.filename, self.w, self.h)
        self.SetTitle(self.title)
        self.frame_1_statusbar = self.CreateStatusBar(1, 0)
        self.panel_1.Bind(wx.EVT_LEFT_UP, self.OnMove)
        self.__set_properties()
        self.__do_layout()

    def __set_properties(self):
        # modified so the image would fit
        self.panel_1.SetMinSize((self.w, self.h))
        self.panel_1.SetMaxSize((self.w, self.h))
        self.panel_1.SetBackgroundColour(wx.Colour(0, 0, 0))
        self.frame_1_statusbar.SetStatusWidths([-1])
        # statusbar fields
        frame_1_statusbar_fields = ["click on image"]
        for i in range(len(frame_1_statusbar_fields)):
            self.frame_1_statusbar.SetStatusText(frame_1_statusbar_fields[i], i)

    def __do_layout(self):
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_3 = wx.BoxSizer(wx.VERTICAL)
        sizer_2.Add(self.panel_1, 1, wx.ALIGN_CENTER_VERTICAL, 0)
        sizer_2.Add(sizer_3, 1, wx.EXPAND, 0)
        sizer_1.Add(sizer_2, 1, 0, 0)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        self.Layout()
    
    def on_paint(self, event):
        self.dc = wx.PaintDC(self.panel_1)
        #self.dc.Clear()
        self.dc.SetBackground(wx.Brush('black'))
        self.line_horizontal(0)
        self.line_vertical(0)
        bitmap = wx.BitmapFromImage(self.image)
        self.dc.DrawBitmap(bitmap, 0,0)
        event.Skip()
    
    def line_horizontal(self, y):
        self.dc.SetPen(wx.Pen('blue', 1))
        self.dc.DrawLine(0, y, self.w, y)

    def line_vertical(self, x):
        self.dc.SetPen(wx.Pen('blue', 1))
        self.dc.DrawLine(x, 0, x, self.h)

    def OnMove(self, event):
        self.dc.Clear()
        bitmap = wx.BitmapFromImage(self.image)
        self.dc.DrawBitmap(bitmap, 0,0)
        pos = event.GetPosition()
        self.line_horizontal(pos.y)
        self.line_vertical(pos.x)
        position = "   x=%s  y=%s" % (pos.x, pos.y)
        new_title = self.title + position
        self.SetTitle(new_title)


# test it ...
app = wx.PySimpleApp() 
frame1 = MyFrame()
frame1.Center() 
frame1.Show() 
app.MainLoop()

Seems to work just fine on my Windows XP machine. What OS do you have?

I tried it but It doesn't work :O Now I think that is a problem of my pc :(
I'm on debian sid with gnome

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.