Is it possible to style a tkinter Text widget? - python

I've been using the 'ttkthemes' module to add some additional user-selectable themes to my application via an OptionMenu, and all my Text widgets stand out a bit in their standard style.
I would like to know if it is at all possible to style a Tkinter Text widget with ttk themes.
I know you can't actually style the standard Tkinter widget (you instead must use the respective ttk version).
My issue is that there is no ttk.Text only the entry and I need multiple lines for a text editor.
One possibility would be to change background colour based on the selected theme, but this could be very long since there many themes to choose from.
Would anyone know of any possible way to get a stylable version of the Tkinter Text widget, so that it doesn't stand out as much?

Would anyone know of any possible way to get a stylable version of the Tkinter Text widget, so that it doesn't stand out as much?
You can create your own custom Text widget that listens to the <<ThemeChanged>> event, and then reconfigures itself based on the settings from the current theme or other widgets. For example, you could query the current theme to get the background color of an Entry widget and use that to set the background of the Text widget.

Related

Using ttk theme azure-dark and Changing background color of ttk combobox widget

I am using the ttk azure theme dark Azure-ttk-theme. It sets the background color to nice modern looking UI. However, as seen in the images the background color of the tk window, Text widget and combobox widget is set as same. This looks bad when we are using all these together with some text in area(as seen on the right side of screenshot below).
I tried to modify the background of my combobox as below(pardon the full imports- just for replication):
from tkinter import *
from tkinter.ttk import *
root = Tk()
style = ttk.Style(root)
root.tk.call("source", SYSTEM_DIR / "azure.tcl")
root.tk.call("set_theme", "dark")
style.configure("TCombobox", fieldbackground= "orange", background= "white")
g_combo = Combobox(root, style="TCombobox")
But, the above code does not have any effect on my interface. If someone has any idea on how I can handle this. I might have to modify the tcl file for this theme. Since, I have limited knowledge on tcl I am looking for answers/suggestions here.
Ahh, well, I am answering this question because I also tried using this theme yesterday. My idea was too something similar to yours. I wanted to change the default colors and stuff to get a better UI. Before getting into the solution, I have to tell you that this theme overrides the default values and designs to a custom-created one as set by the author. So here's what you can do to achieve what you want.
As far as I know, the only way to do this is to edit the TCL file, which manages the colors of the widgets. It would help if you had some idea about TCL language before you get into it so deep. So I will tell you step by step:
Go to dark.tcl file, and then find the widget's code where you want to change the background color. Say you want to change the combo box's background color. So if you are on windows, you can search "Combobox" by pressing ctrl+f after opening that file (I suggest using Notepad to edit it, it's more accessible).
Ok, so here are the codes where you want to change. As you can see in the third part of the code (ttk::style element create Combobox.field), it is used to create the combo box widget. You can keep trying changing different statements and customize the whole Combobox and its effects (If you know how to). But now lets focus on background color
So in there, you see that the default background is set to an image known as box-basic.
So now you have to go to Azure-ttk-theme/theme/dark folder and find out where that box-basic file is. Once you find it, you have to edit the colors of that image with respecting the same size and then replace the old one and make sure you set the same name. I suggest you to use figma to replicate another image. Its easy. Change it to any color you wish.
And you are done!
With some other theme fieldbackground would work, but not with Azure (or with any of my themes).
Tk handles PNG images containing transparency very badly, especially on Microsoft Windows.
So to reduce lagging, I removed the transparent areas of the image elements, and therefore where you'd expect fieldbackground to be, there's actually the images' background, which looks like as if it were fieldbg.
So the solution is to edit the images, as TechieGeeke suggested.

Python change listbox colors in real time

Win7, Python 2.7, Tkinter
I have several list boxes on the screen at once, and I am setting up a way to let the user change the colors (background, text color, border width, border color, etc...) There are more then just list boxes, there are label frames, progress bars, window frames, to name a few.
The listbox (and other widgets) labels are declared in a globals file, thuis:
globs.lb1 = tk.Listbox(root, ...
I can change the attributes easily enough, but what is the best method to update all the widgets?
Currently, in my Settings.py file, I am manually setting each list box, (and other widgets) but, or course, if I add a widget later, I must remember to change the Settings.py file.
I would like to use ttk, I think ttk allows me to change the style, then ttk will remember which widgets use that style, but there is no Listbox in ttk.
If there were an event or something of that nature I could setup for each list box...
Thanks, Mark.
Option 1: define a subclass of Listbox that registers each listbox you create so that when a user changes settings, you can iterater through the set/list of listboxes.
Option 2: use a ttk.Combobox or a ttk.Treeview (with only top-level items and not expansion for subitems). I think one could think of a treeview as a supercharged listbox, with multiple columns and possible hierarchical relationships.

Is it possible to use pango markup text on a Gtk+ 3 toolbutton label using Glade and PyDev?

I have put together a Gtk+ interface in Glade and part of the UI is a tool palette with several toolbuttons using utf-8 characters as labels. They work fine in the default font, but I would like to change font details using pango markup. This is straightforward when dealing with a label as such, as one can apply
label.set_markup(pangoMarkupString)
but the label in a toolbutton can not, as far as I can tell, be addressed directly in this way. A naive
button.label.set_markup(pangoMarkupString)
naturally doesn't work and returns an error saying that toolbuttons do not have the label property. Is there any way to use pango parsed text in a toolbutton, and what depth of python trickery would be required at the application end?
To save any others from hours of fruitless head-scratching and searching, and to open the eyes of other newbies to the powers of Gtk+ 3 and Glade, I present the solution I found.
Right click on your tool palette in the outliner and select edit
Choose the hierarchy tab in the editor
Select your button in the hierarchy outliner
Under Label properties choose widget rather than text
Click on the selector icon at the right of the widget entry box
Add a new widget
Leave the tool palette editor
Select label1, the new widget you created in the outliner
You can now edit its id, label text and attributes
I don't need it yet, but I wonder how to do this with a regular button...

Scroll a group of widgets in Tkinter

I want to make a whole column of various widgets scrollable in a Tkinter GUI, like so:
Tkinter can only attach scrollbars to certain widgets, of which, frames are not included. Making a scollable column is a common practice in interfaces, and there should be a simple solution, but so far, all I have been able to find is this hacky example of a scrollable frame, using a canvas widget. A similar hacky solution was used in a similar stack overflow question.
Is there a commonly accepted way in Tkinter to make a column, or a group of widgets, that is scrollable?
The solution using the canvas is the commonly accepted way to solve this problem. It's really not all that hacky, and the end result can be indistinguishable from having a native scrolling container widget.
If you're making a single column, another option is to use a text widget, and use the widget's ability to embed other widgets. Insert a widget, then insert a newline, insert another widget, etc. You then get the scrolling ability for free. The only thing you need to worry about is configuring the width of the embedded windows, which isn't too hard to do.

Horizontally oriented listbox using Python and Tkinter

I want to provide a listbox where the user can select (multiple) characters (usually close to 15). Quite often some of these will be sequenced, so a listbox is easier than a validated text entry field.
Since the character combination has a meaning to the user, it is user friendly to orient the listbox horizontally.
Is there an easy way e.g. a theme, setting or subclass of the Tkinter listbox so I do not have to build my own?
No, there is no setting, subclass or theme that will let you do that with a listbox.
What you might want to use instead is a set of check buttons with the indicator turned off so they each appear as a button with a single letter. You can then pack them all horizontally in a frame. With the indicator off, the button will appear sunken when selected, or normal otherwise.

Categories