Is it possible to apply partial borders (e.g. a border on only the right-hand edge, or on only the top and the bottom) to an element in Python Tkinter?
EDIT: This is the layout I'm trying to achieve - if anyone can see a better way of achieving this than partial borders, please feel free to point it out - I'm not very experienced with Tkinter!
The answer to your question depends on what you mean by "borders". Individual widgets have borders (buttons, etc), and for those you can't change just part of the border.
If instead you're asking about how to layout your widgets so that there are dividing lines between them, you can do this with the padx and pady options of grid and pack. For those you can have different amounts of padding on each edge.
There is no way to this built in to tkinter, but why do you need this behaviour? There is probably a better way to accomplish what you need. See this this resource for more help with your problem.
Related
So I am curren]tly working on an app for image processing. I am using PyQt5 for this purpose. You can see how it looks like now on the picture below. My question is, what is the best way to make it responsive, meaning, if I resize the window, all the widgets will rescale as well. Right now I am using some groupboxes, Hlayouts, VLayouts.
Only thing that comes to my mind is to create a gridlayout, though I would like to know, if there is by any chance any other possibility how to easily do it, since there is quite a lot of widgets.
Also, I would like to ask about GroubBox widget. Down left I have four buttons which are in one groupbox whose border is set to none by self.tableButtonsGroupBox.setStyleSheet("tableButtonsGroupBox {border: none}"), but there is still visible the upper line. Is there way to get rid of it?
Thank you for suggestions in advance!
I've been working on a project where I use Separators and I've just can't find out how I make them thicker!
this is the code for it I've got at the moment:
tkinter.ttk.Separator(master, orient=VERTICAL).grid(column=1, row=1, rowspan=4, sticky='ns')
so how do I make the Separator thicker/wider?
ps. The code is only a small part of my project
The ttk separator widget isn't designed to be modified in that way. Part of the point of the themed (ttk) widgets is that they adhere to a common theme and thus can't be customized.
You can use a an empty frame widget, which allows you to set the width, height, border style, color, etc.
Even though it apparently goes against the "theme" of tkinter, as pointed out by Bryan, I wanted to do this too.
The solution I found is simply stack multiple separators. If you're using grid geometry manager, you'll need to provide extra rows or columns to fit as many separators as necessary. For me, three separators did the trick, and aesthetically, I didn't find it offensive in any way:
ttk.Separator(master=root,orient='vertical').grid(row=0,column=11,rowspan=2,sticky='ns')
ttk.Separator(master=root,orient='vertical').grid(row=0,column=12,rowspan=2,sticky='ns')
ttk.Separator(master=root,orient='vertical').grid(row=0,column=13,rowspan=2,sticky='ns')
But as Bryan suggests, you can use an empty Frame widget. Example code for that can be found here:
https://stackoverflow.com/a/68179426/7759839
Does anyone know if it is possible to have a single scale widget with two "sliders" from the tkinter module? Or anyway to fake it?
Such as (edited with GIMP):
I am thinking placing one scale on top of another scale, but with a transparent background?
No, you cannot do this with the scale widget. You can't give it a transparent background and stack them.
If I had to implement this functionality, I would use a canvas widget and draw my own sliders.
It's not very hard to do, but obviously it's going to be more work than an off-the-shelf widget.
Here is a custom widget using a canvas, made by MenxLi.
I think this might work: Range Slider Module
You can see how it looks,
Is there any way to customize the border color of a Tkinter/ttk widgets or frames?
Yes, you can create your own style and apply it to the widget or configure the attributes of an existing style.
Some documentation and examples are in the official docs, specifically the section about ttk.Style.
Additional information on using styles (for several languages) can be found at tkdocs.com.
Regarding a border on ttk.Frames: in lieu of styles, the best I can think of is to insert a ttk.Frame within a ttk.Frame. The outer frame would use a standard "background" property with the border color of choice, and a padding equal to the desired border thickness. The inner frame then obscures the middle, so you have a line only around the edges. Works, but seems a little bit hacky (and a bit less wieldy if you're creating many widgets/borders).
I've searched quite a bit and haven't found or figured out a way to use a style to change a ttk.Frame's border color. Using something like style.configure( 'SeletectedItem.TFrame', highlightcolor='#78F', highlightthickness=2 ) or style.configure( 'SeletectedItem.TFrame', highlightbackground='#78F', highlightcolor='#78F', highlightthickness=2, borderwidth=2 ) (config options that work on a regular Tk.Frame) doesn't seem to do anything. With style.layout() I find just [('Frame.border', {'sticky': 'nswe'})], and from style.element_options('TFrame.border') there is just ('-relief',). I don't know if I'm using the wrong option names or syntax (or what options are available for .configure() for different widget classes), or if this just isn't possible with styles. I'm interested if anyone has extra info or knows if this can be done.
I want to put a Canvas with an image in my window, and then I want to pack widgets on top of it, so the Canvas acts as a background.
Is it possible to have two states for the pack manager: one for one set of widgets and another for another set?
The answer to your specific question is no. You can't have two states or otherwise use pack two different ways in the same parent.
However, what I think you want to accomplish is simple. Use the built-in features of the canvas to create an image item that is part of the canvas, then pack things into the canvas as if it were a frame.
You can accomplish a similar thing by creating a label widget with an image, then pack your other widgets into the label.
One advantage to using a canvas is you can easily tile an image to fill the whole canvas with a repeating background image so as the window grows the image will continue to fill the window (of course you can just use a sufficiently large original image...)
I believe that Bryan's answer is probably the best general solution. However, you may also want to look at the place geometry manager. The place geometry manager lets you specify the exact size and position of the widget... which can get tedious quickly, but will get the job done.
... turned out to be unworkable because I wanted to add labels and more canvases to it, but I can't find any way to make their backgrounds transparent
If it is acceptable to load an additional extension, take a look at Tkzinc. From the web site,
Tkzinc (historically called Zinc) widget is very similar to the Tk Canvas in that they both support structured graphics. Like the Canvas, Tkzinc implements items used to display graphical entities. Those items can be manipulated and bindings can be associated with them to implement interaction behaviors. But unlike the Canvas, Tkzinc can structure the items in a hierarchy, has support for scaling and rotation, clipping can be set for sub-trees of the item hierarchy, supports muti-contour curves. It also provides advanced rendering with the help of OpenGL, such as color gradient, antialiasing, transparencies and a triangles item.
I'm currently using it on a tcl project and am quite pleased with the results. Extensions for tcl, perl, and python are available.
Not without swapping widget trees in and out, which I don't think can be done cleanly with Tk. Other toolkits can do this a little more elegantly.
COM/VB/MFC can do this with an ActiveX control - you can hide/show multiple ActiveX controls in the same region. Any of the containers will let you do this by changing the child around. If you're doing a windows-specific program you may be able to accomplish it this way.
QT will also let you do this in a similar manner.
GTK is slightly harder.