I am trying to design 650pxx100px button in an application.
The rectangle contains an icon on the left and a text on the right.
In order to make everything act like a button I was considering adding a transparent button on top of the text and icon but SetTransparent has no effect and adding a mask raises an exception as SetMask does not exists for BitmapButton.
bitmap = wx.EmptyBitmap(650, 100)
button = wx.BitmapButton(panel, -1, bitmap=bitmap, pos=(100, 0), size=(650, 100), style=0)
Has anyone done something like this successfully or can suggest a course of action that would help my case?
I have worked with GTK in the past and where there is an EventBox that can contain anything and have events bind to it.
wxPython doesn't really support that. Transparency is an all or nothing affair where either everything in your app is transparent or none of it is. You can do gradients if you use the PlateButton or the AquaButton though. Or you might be able to do something if you drew the button yourself.
Related
I am making a GUI using tkinter, I have some Button icons on my screen, that when user clicks, they execute their specific function. for example this one:
def screenshot():
# root.iconify()
myScreentshot=pyautogui.screenshot()
file_path=filedialog.asksaveasfilename(defaultextension='.png')
myScreentshot.save(file_path)
screenshot_image = tk.PhotoImage(file='images/app6.png')
screenshot = tk.Button(root,image=screenshot_image,bg='#0000CD',command=screenshot)
screenshot.place(x=640,y=500)
the problem is icons image. when I use the image as a button icon on my page, they always have a square or a rectangle on their background, although I use bg to set its color like my main page but I can still see it has a background.
How can I simply display the shape of the image without the background of the shape?
The square or rectangle is called the "relief". It is one of the visual cues that this is a button that can be pressed.
If you don't want that, set it to "flat":
screenshot = tk.Button(
root,
image=screenshot_image,
relief="flat",
bg='#0000CD',
command=screenshot
)
I would however advise you not to do this. The relief is there so that buttons are recognizable as such. Removing that in some places makes for an inconsistant user interface which can confuse new users.
I want to create buttons dynamically
self.ctset = wx.BitmapButton(panel, -1, self.pic1, pos=(10,10), size=(50,50))
self.ctset.Bind(wx.EVT_BUTTON, self.add_ct)
self.ctset.SetDefault()
and the add_ct binding function
def add_ct(self, event):
pos=(10,self.yct)
self.yct+=65
self.new = wx.BitmapButton(self, -1, self.pic1, pos=pos,size=(50,50))
self.new.SetDefault()
print "Cutset"
I don't know where I am going wrong but my dynamically created buttons always seem disabled!
I want to bind a function to the dynamically created buttons that allows me to drag them around. Any ideas would be of great help!
I am pretty new to python and wxpython.
I don't see any code that captures the mouse's coordinates or even any drag-and-drop code. You need to download the wxPython demo package from the wxPython website and look at the ShapedWindow example for catching mouse coordinates. See also this old thread: http://wxpython-users.1045709.n5.nabble.com/Drag-Button-around-a-Panel-td3358640.html
In it you will find someone who is doing something very similar to what you want. I also found the following links which you might find helpful:
Drag button between panels in wxPython
How to move items smoothly in wxPython?
I'd like to do the following: Create a fullscreen, always on top pygtk window with a webkit widget displaying some html, but with a box that is completely transparent, so that the windows below are visible. (This seems to be possible: Is it possible to render web content over a clear background using WebKit?)
What I'd like is to (sometimes) pass all mouse events that occur in the transparent box down to the windows below my application's window, so that I can interact with them normally. So not just visually transparent, but also transparent to mouse events.
Theoretically, I suppose I could catch all events I am interested in with a pygtk Eventbox, find the window directly below mine with wnck, and pass this event to it with python-xlib.
This doesn't exactly seem like the most elegant solution; is there a better way?
Forwarding the events won't work well as you guessed; it creates a lot of race conditions, and some apps will ignore stuff from XSendEvent anyway.
What you can do is set the input shape mask. See http://www.x.org/releases/current/doc/xextproto/shape.html and then look at XFixesSetWindowShapeRegion() in /usr/include/X11/extensions/Xfixes.h which lets you specify a shape "kind" (here you want ShapeInput).
Something like:
XRectangle rect;
XserverRegion region = XFixesCreateRegion(display, &rect, 1);
XFixesSetWindowShapeRegion(display, window, ShapeInput, 0, 0, region);
XFixesDestroyRegion(display, region);
The ability to set ShapeInput is "only" 5-6 years old so if you care about really crappy old versions of X11, you might be hosed.
I am trying to rebuild the functionality of the desktop's "highlight to select" feature so that I can use it in my own app. When I say "highlight to select" I mean the selection box that shows up if you click and drag on your desktop (native to all main-stream OS).
I've been working for hours trying to recreate it, and simply can't find a way. I've tried PyGTK, Xlib for python, and a couple other weird hacks. All of which have their own problems that won't allow me to move forward.
I generally don't ask for straight up example code without providing some sort of starting point, but in this project I don't even know where to start. How would you do this?
Here's the requirements:
Must draw on the root window (or a transparent layer that "appears" to be the root)
Must return the coordinates of the selection (x, y, height width)
Update: Forgot some details.
I am using Ubuntu 10.10
I have dual monitors (though, I don't think that should matter)
I don't mind downloading any extra libraries that are necessary
I don't know if this is what you're looking for, but what if you created another window in your module, and have your code show it when you release drag? You could fetch the cursor's current position, and have it draw the window there.
This should help you get the mouse position on the root window.
So, your code may look a little like this (this is untested code!) I'm only showing the relevant portions of what goes inside __ init __.
def __init__(self):
...
#Some of your code here.
...
win = gtk.Window(gtk.WINDOW_TOPLEVEL)
#Note that I am creating a popup window separately.
popwin = gtk.Window(gtk.WINDOW_POPUP)
#I am setting "decorated" to False, so it will have no titlebar or window controls.
#Be sure to compensate for this by having another means of closing it.
popwin.set_decorated(False)
def ShowPopup():
#You may need to put additional arguments in above if this is to be an event.
#For sake of example, I'm leaving this open ended.
#Get the cursor position.
rootwin = widget.get_screen().get_root_window()
curx, cury, mods = rootwin.get_pointer()
#Set the popup window position.
popwin.move(curx, cury)
popwin.show()
def HidePopup():
#This is just an example for how to hide the popup when you're done with it.
popwin.hide()
...
#More of your code here.
...
#Of course, here is the code showing your program's main window automatically.
win.show()
A very simplistic approach, but it should give the appearance of what you're wanting.
I would like to create a button that I can control the look of the button using pyGTK. How would I go about doing this?
I would like to be able to point to a new image for each 'state' the button is in (i.e. Pressed, mouse over, normal...etc.)
As described in the API documentation for gtk.Image, "If you want to receive events on the image, such as button clicks, place the image inside a gtk.EventBox, then connect to the event signals on the event box.".
You probably want to use a gtk.Image rather than a gtk.Button, since buttons require more knowledge of how the theming engine works. If you really want to use a button, you'll need to read up on gtk rc files and the APIs for manipulating them.
Here's an easy way to use an image on a button. Note that there is no text given when you initialize the button (self.button1 = gtk.Button()). Adding text there would display the text instead of the image.
self.image1 = gtk.Image()
self.image1.set_from_file('images/home.png')
self.image1.show()
self.button1 = gtk.Button()
self.button1.add(self.image1)
self.button1.show()
self.backupHBox.pack_start(self.button1, True, True)
self.button1.connect("clicked", self.quit)