Contenu | Rechercher | Menus

Annonce

Si vous avez des soucis pour rester connecté, déconnectez-vous puis reconnectez-vous depuis ce lien en cochant la case
Me connecter automatiquement lors de mes prochaines visites.

À propos de l'équipe du forum.

#1 Le 09/02/2014, à 13:30

Gaara

[résolu] wxPython: changer position fenêtre

Bonjour,

J'ai récupéré ce script python sur le net, et actuellement l'image s'affiche en haut à gauche de l'écran, je voudrais savoir comment afficher cette image en haut à droite de l'écran.
Je précise que je ne connais rien au python, c'est juste par curiosité.
J'ai essayé cette méthode, mais rien de marche, je ne comprend rien...

#!/usr/bin/env python

import wx

# Create a .png image with something drawn on a white background
# and put the path to it here.
IMAGE_PATH = '/home/guillaume/Images/gui.png'


class ShapedFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "Shaped Window",
                style = wx.FRAME_SHAPED | wx.SIMPLE_BORDER )
        self.hasShape = False
        self.delta = wx.Point(0,0)

        # Load the image
        image = wx.Image(IMAGE_PATH, wx.BITMAP_TYPE_PNG)
        image.SetMaskColour(255,255,255)
        image.SetMask(True)            
        self.bmp = wx.BitmapFromImage(image)

        self.SetClientSize((self.bmp.GetWidth(), self.bmp.GetHeight()))
        dc = wx.ClientDC(self)
        dc.DrawBitmap(self.bmp, 0,0, True)
        self.SetWindowShape()
        self.Bind(wx.EVT_LEFT_DCLICK, self.OnDoubleClick)
        self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
        self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
        self.Bind(wx.EVT_MOTION, self.OnMouseMove)
        self.Bind(wx.EVT_RIGHT_UP, self.OnExit)
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_WINDOW_CREATE, self.SetWindowShape)


    def SetWindowShape(self, evt=None):
        r = wx.RegionFromBitmap(self.bmp)
        self.hasShape = self.SetShape(r)

    def OnDoubleClick(self, evt):
        if self.hasShape:
            self.SetShape(wx.Region())
            self.hasShape = False
        else:
            self.SetWindowShape()

    def OnPaint(self, evt):
        dc = wx.PaintDC(self)
        dc.DrawBitmap(self.bmp, 0,0, True)

    def OnExit(self, evt):
        self.Close()

    def OnLeftDown(self, evt):
        self.CaptureMouse()
        pos = self.ClientToScreen(evt.GetPosition())
        origin = self.GetPosition()
        self.delta = wx.Point(pos.x - origin.x, pos.y - origin.y)

    def OnMouseMove(self, evt):
        if evt.Dragging() and evt.LeftIsDown():
            pos = self.ClientToScreen(evt.GetPosition())
            newPos = (pos.x - self.delta.x, pos.y - self.delta.y)
            self.Move(newPos)

    def OnLeftUp(self, evt):
        if self.HasCapture():
            self.ReleaseMouse()



if __name__ == '__main__':
    app = wx.PySimpleApp()
    ShapedFrame().Show()
    app.MainLoop()

Merci !

Dernière modification par gaara92 (Le 11/02/2014, à 18:09)


Kubuntu 18.04 x64
Un terminal tactile Raspberry Pi et Odroid

                                        <code>zenity  --question --title "Alert"  --text "Microsoft Windows has been found! Would you like to remove it?"</code>

Hors ligne

#2 Le 11/02/2014, à 15:49

Gaara

Re : [résolu] wxPython: changer position fenêtre

Après quelques recherches, j'ai trouvé un autre script qui utilise gtk et non wx. Ca à l'air plus simple.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
import pygtk
pygtk.require('2.0')
import gtk

class MonObjet:
  
    def close_application(self,widget,eve,data=None):
       widget.destroy()   
       gtk.main_quit()
       while gtk.events_pending():
        gtk.main_iteration()             
      
      
    def __init__(self):
        win = gtk.Window(gtk.WINDOW_POPUP)
        win.connect("delete_event", self.close_application)
        win.set_events(win.get_events()|gtk.gdk.BUTTON_PRESS_MASK)
        win.connect("button_press_event", self.close_application)
        win.move(1600,30)
        win.show()
        
        pixmap, mask = gtk.gdk.pixmap_create_from_xpm(win.window, None, '/home/guillaume/majauto/maj.png')
        image = gtk.Image()
        image.set_from_pixmap(pixmap, mask)
            
        win.add(image)
        win.shape_combine_mask(mask, 0,53)
        win.show_all()
        
   
MonObjet()
gtk.main()

Le "win.move(1600,30)" sert à placer l'image sur l'écran.

Dernière modification par gaara92 (Le 11/02/2014, à 15:51)


Kubuntu 18.04 x64
Un terminal tactile Raspberry Pi et Odroid

                                        <code>zenity  --question --title "Alert"  --text "Microsoft Windows has been found! Would you like to remove it?"</code>

Hors ligne

#3 Le 11/02/2014, à 17:22

bipede

Re : [résolu] wxPython: changer position fenêtre

Bonjour,

Avec wxPython, il te suffit d'écrire ces lignes:

 
screenWidth, screenHeight = wx.ScreenDC().GetSize()
posX = screenWidth - self.bmp.GetWidth()
posY = 0
self.SetPosition((posX, posY))

et de les placer juste après la création de ton bmp par la ligne suivante de ta méthode __init__

self.bmp = wx.BitmapFromImage(image)

Cordialement.


Desktop: MSI - Intel® Core™ i5-3330 CPU @ 3.00GHz × 4 - RAM 8 go- Kubuntu 21.04 - Système sur SSD 64 Go - /home sur HDD 500 Go.
Laptop: DELL Inspiron-15 3567 - Intel® Core™ i5-7200 CPU @ 2.50GHz × 4 - RAM 8 go - HDD 1 To - Ubuntu 20.10 avec /home séparé.

Mon site: Les contributions du bipède

Hors ligne

#4 Le 11/02/2014, à 18:07

Gaara

Re : [résolu] wxPython: changer position fenêtre

Merci, c'est parfait.
Malgré tout, avec mes deux écrans, l'image s'est retrouvée sur le deuxième écran. J'ai donc modifié la ligne "self.SetPosition((posX, posY))" par "self.SetPosition((1600, 30))"
Je passe en résolu.


Kubuntu 18.04 x64
Un terminal tactile Raspberry Pi et Odroid

                                        <code>zenity  --question --title "Alert"  --text "Microsoft Windows has been found! Would you like to remove it?"</code>

Hors ligne