wxPython Slider background colour incomplete change - python

wxPython 2.8.10, Python 2.6.2, Windows 7 x64
So I am having a bit of a wxPython issue I am hoping someone can say where I am going wrong. In my application I have Sliders where the background colour changes as you move the slider around. I do this via SetBackgroundColour from within a EVT_SLIDER handler. It mostly works, but I end up with a border of unchanged colour around the outer edge of the widget.
I have tried Refresh up the chain, RefreshRect on the top level Frame around the widget (and a few pixels further out), various combinations of Update and Layout, but nothing seems to erase it. The best I have managed is if I use something like:
frame = wx.GetTopLevelParent(slider)
rect = slider.GetRect()
slider.ClearBackground()
slider.SetBackgroundColour(wx.Colour(red,green,blue))
frame.RefreshRect(rect)
It will correctly draw the background box with the new colour without the border issue, but it will not repaint the slider itself.
However, if I click away so the focus is elsewhere, the border snaps to the new colour.
I could probably use an overlay or custom paint function but I am hoping there is something I am missing here that will get me the desired results.
Any suggestions?
(edited to add)
Looks like resizing the window manually corrects the issue, but Layout() does not.
It also functions correctly if you do the client size trick:
w,h = slider.GetClientSize()
slider.SetClientSize( (w+1,h) )
slider.SetClientSize( (w,h) )

Related

How to move canvas drawn shapes to have a fixed position on screen?

I have a GUI that I've made in tkinter using a canvas that has a bar along the left side and a bar along the top containing information. In the remaining area various rectangles are drawn corresponding to the information in those bars I described. The issue is, when you scroll away on the canvas, those events will leave as well.
Basically, I want to create a visual effect like position: fixed is in CSS where these bars on the side and the top stay in place relative to the rest of the canvas so that they don't move from their relative position while scrolling.
I tried making use of the scrollbar commands, but have had trouble making my own function there. I also tried to see if there was an event I could bind to the canvas to track the movement so that I could move the bars myself but I could not find anything.
There is no way to do this directly in the canvas unless you write the code to move the items in the top and left whenever the user scrolls. It would probably require a fair amount of work.
However, it's trivial to have separate canvases for the side and top that do not scroll, and a third canvas that has all of the scrolled data.
I was able to end up solving my own issue by adapting the solution from: How can I add Unscrollable Image to a frame/Canvas?
Simply put, I made my own yview and xview functions that would scroll my desired "bars" with the window by moving the objects.
Special thanks to #jasonharper for his help.

Black boxes around slider in wxpython GUI

I imagine this is a simple question and was hoping it someone would be able to help me figure it out. I have multiple wxpython sliders in my GUI but some of them have black boxes around.
The sliders are placed inside a StaticBox which is placed inside ScrolledPanel. It seems that those that are on top (i.e. are shown without the need to scroll the panel) look normal, as for the 'Annotation font size' but the ones that were hidden have black background behind it. Anyone has any ideas?
I thought it was because I was not calling Layout() but it doesn't make any difference.
I managed to fix this problem on my side. Maybe it helps you too. In my case, I have a lot of text boxes and sliders on a scrolled panel (wx.lib.scrolledpanel.ScrolledPanel):
Black boxes around sliders:
I am fixing it by setting the background color of the panel after I have finished populating it with graphic elements:
self.SetBackgroundColour('WHITE')
Here is the result:
No more black boxes around sliders:

Is there a simple way to add a border to Kivy Labels, Buttons, Widgets etc. with-out images?

I'm trying to add a border to Kivy Buttons but it doesn't work as expected.
For labels my implementation seems to be OK but for buttons it overrides/clears the standard look of the button.
How can I draw the border above the button with-out changing normal behavior? I'd like to implement it like the ButtonBehavior so I can add a border to every Kivy object with a canvas. I've called it BorderBehavior.
Styling dashed, dotted works only for line width of 1 because there is a bug in Kivy (see https://github.com/kivy/kivy/issues/2037) (Need to figure out what's wrong here later.)
I know that drawing a border is possible with a BorderImage but I'd like to add simple borders with-out an image.
Here is how it looks at the moment:
You can find my source code here (the labels can be dragged just for testing purposes to see that the border is always correctly positioned):
https://gist.github.com/AWolf81/c6796dc2049d9872b2df
OK, I've found the fix. It was a naming conflict.
In the console log I saw that there is a problem at unpacking the border tuple in BorderImage of the button.
Of course, that's not working because my border is implemented differently. Maybe I can add the list (top, right, bottom, left) to my border implementation so I can keep the same name. But I haven't checked that yet.
Changing the naming of my border to borders in python and in kv fixed the problem:
class BorderBehavior(Widget):
borders = ObjectProperty(None)
Now it looks like I want it:
OK, now I'll check if it's working for other classes too (e.g. Scatter, Widget,...). If that's working I'm doing a pull request to Kivy.

python tkinter canvas 'transparent'

I think transparent is the right word I'm looking for. Hopefully my description will explain it properly no matter what.
I have a graph of time based data going back decades. Pretty much simply setup to show one day per x-pixel, unless zoomed in closer. I would like to have a box, maybe along the lines of 5x100, appear on top of the graph so when I move the mouse over the graph the box will move and keep pace with the mouse. Anotherwords showing what was happening in the 5 furthest days 'x number of days prior'. Anotherwords when computing an average going forward what are the next values to be falling off as new data arrives. Naturally I want the underlying graphed data to be displayed with the transparent box on top of it outlining the days in question. This may get crazy enough to be a much wider box with two areas that are colored light grey or something like that to show the areas in question but the colored areas are separated by numerous days(could be multiple transparent windows that are tracked together as well. Is this feasible with tkinter? From the research I've been doing it's questionable if using
root.attributes('alpha', .30)
would work or not. It doesn't sound like I could do something like as it would end up making the graph transparent to whatever is underneath it.
self.Graph.create_line()
self.Box.attributes('alpha', .30)
If I understand correctly I have to use
attributes
right at the root level versus the individual 'window' level so the above (severely chopped down) code wouldn't work...or would it. I haven't had a chance to try anything out yet to see what happens...that will be later on this evening. Kinda hoping to save myself a little time by asking now and you never know who else may need the help sometime.
If I understand what you're trying to do, it's going to be pretty hard.
Setting window attributes works just fine to make a window transparent. You've got a minor problem in the code—attributes start with a hyphen—but otherwise you've got it right:
self.Box.attributes("-alpha", .30)
However, it sounds like you want that Box to be an embedded part of the graph window, not its own top-level window that can be dragged around by the user, etc. Unfortunately, Tkinter does not have any notion of child windows, and it doesn't expose nearly enough of the native stuff you'd need to fake them by creating an immobile window and manually moving it to track the movements of another window. So, you don't have a window, which means you don't have window transparency.
The obvious thing for Box to be is some kind of widget, like a Frame or Canvas. But widgets don't have transparency.
Box could instead be just a collection of elements drawn onto the same Canvas as the Graph. That seems promising… but none of the Canvas methods handle alpha transparency. (Some of them do handle all-or-nothing transparency, but that doesn't help.)
One thing that does handle transparency is PhotoImage. So, if you draw Box off-screen, get the resulting contents as a PhotoImage, add the alpha (e.g., via PIL), then create_image the result… close, but no cigar. Depending on the settings of the underlying Tk library, Tkinter may just draw the pixmap with 1-bit transparency or ignore transparency completely. (Experiment with loading alpha-transparent PNG files in PIL and drawing them on a Canvas.) So, unless you want an app that looks right on some systems, doesn't draw the Box at all on others, and draws it opaque on others, this is a dead end.
So, what's left? Only manual compositing: Draw the Graph and the Box on separate off-screen windows, get the pixmaps, use PIL to compose them, and create_image the result.
At which point you're probably better off just using something like PIL's ImageDraw or a more powerful library to construct the pixmap in the first place. Or, of course, using a more powerful GUI library than Tk, like Qt or wx.
Maybe this can give you some ideas to play with:
from tkinter import *
root = Tk()
c = Canvas(root, width=640, height=480, bd=0, highlightthickness=0)
c.create_line(0,240,640,240, fill='blue')
c.pack()
#pil image with transparency
try:
from PIL import Image, ImageTk
except ImportError:
pass
else:
pim = Image.new('RGBA', (5,100), (0,255,0,64))
photo = ImageTk.PhotoImage(pim)
c.create_image(200,200, image=photo, anchor='nw')
#blank standard photoimage with red vertical borders
im = PhotoImage(width=7, height=480)
dat = ('red',)*480
im.put(dat, to=(0,0))
im.put(dat, to=(6,0))
box = c.create_image(0, 0, image=im, anchor='nw')
def on_motion(event):
left,top = c.coords(box)
dx = event.x - (left+7)
c.move(box, dx, 0)
c.bind('<Motion>', on_motion)
root.mainloop()

Is it possible to create a Tkinter Window with no frame, and no background?

I've looked through the documentation, and searched quite a bit now, and couldn't find an answer for this problem. Before I give up, I'll ask here.
I want to create a window that has no frame (as in, no titlebar, no border around the window), and maybe a transparent background. I've found how to set the background to a certain colour, but not to transparent, and couldn't find anything about whether I can have the frame not display itself.
So I ask, is it possible to create a Tkinter window that has no border, and a transparent background?
No border is possible. Try something like wm transient or wm overrideredirect.
Transparent depends, have a look at the documentation for wm attributes:
http://www.tcl.tk/man/tcl/TkCmd/wm.htm#M8 On windows you can set a transparentcolor.
Otherwise you might need a platform specific extension to create oddly shaped and transparent windows, for example: http://www.cs.man.ac.uk/~fellowsd/tcl/shapeidx.html

Categories

Resources