Vertically center text inside label in Gtk using python - label

I'm trying to vertically center the text of a label inside a box. This is do code I'm trying:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
win = Gtk.Window()
win.set_default_size(200, 100)
box = Gtk.Box()
win.add(box)
lbl = Gtk.Label("FOO")
lbl.set_vexpand(True)
lbl.set_valign(Gtk.Align.CENTER)
# Set the background to make the problem visible
lbl.override_background_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(red=1, green=0, blue=0))
box.add(lbl)
win.show_all()
win.connect('destroy', Gtk.main_quit)
Gtk.main()
As you can see, the label itself is centered perfectly fine inside the box, but the text inside the label is shifted slightly towards the top end of the label:
I'm not able to find anything about this. Programmatic as well as CSS-based solutions are highly appreciated.

I believe the problem is that you and the font creator have different opinions of what "centered" in the vertical sense means. Also think what will happen to the visual impression if you have characters like y and g. This will get even more confusing if you add international characters to the mix like Å or Ö.
Anyway, this answer uses CSS to create a configurable offset (padding-top), and will also give you freedom to change font. The 20 px value is obviously too much, but will give a clear visible evidence that it works.
style.css:
#custom_label {
padding-top: 20px;
background-color: red;
font: Vera 20px;
}
test.py:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
win = Gtk.Window()
win.set_default_size(200, 100)
box = Gtk.Box()
win.add(box)
lbl = Gtk.Label(u'FOO')
lbl.set_name('custom_label')
box.add(lbl)
style_provider = Gtk.CssProvider()
Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(), style_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
with open("style.css") as f:
style_provider.load_from_data(f.read())
win.show_all()
win.connect('destroy', Gtk.main_quit)
Gtk.main()
As a bonus, if you start your program with:
GTK_DEBUG=interactive python test.py
(assuming Linux, not sure how to do this in Windows), you will have an interactive way to change the CSS and other widget properties.

Related

Changing the font of atom label in rdkit

I'm trying to change the font type of the atom labels in RDKIT.
The default font type is 'sans'. I ran this code on colab:
from rdkit import Chem
from rdkit.Chem.Draw import IPythonConsole
from rdkit.Chem import Draw
smiles = 'C1=CC=C(C=C1)I'
mol = [Chem.MolFromSmiles(smiles)]
IPythonConsole.drawOptions.atomLabelFontFace = 'Times New Roman'
pic = Draw.MolsToGridImage(mol, returnPNG=False, subImgSize= (256, 256), molsPerRow=1)
display (pic)
But it seems that the font type doesn't change, neither when I tried other font types. What's wrong?
Use IPythonConsole.drawOptions.fontFile to choose the font.
Times New Roman is not installed in Colab, so you have to install it or you can use the existing serif font (LiberationSerif-Regular.ttf).
from rdkit import Chem
from rdkit.Chem import rdDepictor, Draw
rdDepictor.SetPreferCoordGen(True)
from rdkit.Chem.Draw import IPythonConsole
IPythonConsole.molSize = (600, 300)
IPythonConsole.drawOptions.fontFile = r'/usr/share/fonts/truetype/liberation/LiberationSerif-Regular.ttf'
IPythonConsole.drawOptions.explicitMethyl = True
m = Chem.MolFromSmiles('CC(F)CC(Cl)C(C(=O)O)C(P)CC(S)CC(Br)CN') # Dummy molecule
rdDepictor.Compute2DCoords(m)
m
On your local computer you also need to set the path to the font:
IPythonConsole.drawOptions.fontFile = r'C:\Windows\Fonts\times.ttf'

Matploitlib image displaying at place of button

I have troubles using Matploitlib. My aim is to create program, which will be displaying image, with buttons allowing it's edition.
I started from the button allowing to pick an image, and I already have meet a problem. I wish image to load at the center of the window, but it loads at the place of button.
How to create a figure with fixed position, and how to choice it to display the image?
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
import tkinter.filedialog as dialog
class Index(object):
def load(self, event):
filename = dialog.askopenfilename()
img = plt.imread(filename)
plt.imshow(img)
callback = Index()
axload = plt.axes([0.59, 0.05, 0.1, 0.075])
bload = Button(axload, 'Load')
bload.on_clicked(callback.load)
plt.show()
plt.close()
Okay, I found an answer by myself.
To create new axis
ax=plt.subplot(111)
and then, to use it instead of existing one, simply
ax.imshow(img)
instead of
plt.imshow(img)

Can't make image_url work in Bokeh (Python)

I tried to reproduce the solution from: How do I work with images in Bokeh (Python) , but it doesn't work. For that, I find an image on the net and put it in place of the 'url' field but the plot is just blank! From the original solution bokeh ask me to add up w and h params which I suppose are the width and height of the pic. Also I dropped x_range and y_range within figure() to wipe out the horizontal and vertical lines of the plot.
from bokeh.plotting import figure, show, output_notebook
output_notebook()
p = figure()
p.image_url( url=[ "http://pngimg.com/uploads/palm_tree/palm_tree_PNG2504.png"],
x=1, y=1, w=253, h=409)
show( p)
Anyone could tell me what's going on ?
Bokeh can't auto-range ImageURL it seems. So if there are no other glyphs, you need to provide explicit ranges. Additionlly, the default anchor is upper_left IIRC so it may be that our image is rendering off-canvas and you don't realize it. The code below works with Bokeh 0.12.5:
from bokeh.plotting import figure, show, output_file
output_file("foo.html")
p = figure(x_range=(0,500), y_range=(0,500))
p.image_url( url=[ "http://pngimg.com/uploads/palm_tree/palm_tree_PNG2504.png"],
x=1, y=1, w=253, h=409, anchor="bottom_left")
show(p)
Without the anchor set, the image plots blow the plot region (have to pan to see it)

How to set the background color of a tab widget in Qt Creator?

I’m using Qt Creator. In my GUI I use a tab widget. This widget should have the same grey background color as the main window (picture A). I accomplished this by editing the Style Sheet in Qt Designer with:
background-colour: rgb(240, 240, 240);
But now I have two new problems I can’t solve:
The buttons (--> Send) are not rounded anymore.
The edit boxes’ background color has changed to grey, too.
Befor I changed the Style Sheet the GUI looked like in Picture B.
I also tried
QPalette pal = m_pUi->tabWidget->palette();
pal.setColor(m_pUi->tabWidget->backgroundRole(), Qt::blue);
m_pUi->tabWidget->setPalette(pal);
but this only changes the color behind the tabs, not the entire color of the whole "tab-window-surface".
Do I have to make additional style descriptions or is there an more simple solution?
Picture A - with Style Sheet
Picture B - without Style Sheet
I had the same problem and I discovered that you need to set this attribute to each one of your tabs:
ui->tab->setAutoFillBackground(true);
I'm not sure, but I think that also is necessary set that attribute to the QTabWidget as such.
I hope this help.
The "things" you want to access are called QTabBars. Keeping that in mind you can write a stylesheet like this:
QTabBar::tab
{
background: #48555E;
color: white;
border-color: #48555E;
}
QTabBar::tab:selected,
QTabBar::tab:hover
{
border-top-color: #1D2A32;
border-color: #40494E;
color: black;
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #C1D8E8, stop: 1 #F0F5F8);
}
Also you might find this question and this official documentation insightful.

TransformGestureEvent zoom on image but keep image size Flex

I'm having problems finding the exact answer I'm looking for. I'm building an application that currently allows pinch zooming on an image. This is being built in Flash Builder 4.5. What I want specifically is to keep the image the same dimensions while zooming in and out. So the image still increases and decreases but stays within an invisible box.
Does that explanation make sense?
Right now I can zoom in and out but it changes the image size and covers the other content. Eventually other features will be added including pan and rotate. The image will become a 3D model of a mouth.
If tutorials or sources are supplied that would be great.
Here's my code for the zoom gesture which is using the TransformGestureEvent:
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.TransformGestureEvent;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
private function init():void
{
img.addEventListener(TransformGestureEvent.GESTURE_ZOOM, onZoom);
}
private function onZoom(event:TransformGestureEvent):void
{
var mySprite:Sprite = img as Sprite;
if(mySprite.scaleY.valueOf() >= 1)
{
mySprite.scaleX *= event.scaleX;
mySprite.scaleY *= event.scaleY;
}
else
{
mySprite.scaleX= 1;
mySprite.scaleY = 1;
}
}
If i understand you question correctly.
I think you may want to look at scrollRect
http://gskinner.com/blog/archives/2006/11/understanding_d.html
DisplayObject.scrollRect lets you specify a Rectangle object that defines the region of the DisplayObject (Sprite, MovieClip, etc) to display. The masked region is always displayed at the origin of the display object, which lets you scroll the region by changing the x and y properties of the rectangle.

Resources