How can I insert an image on a Gtk.Label. Is this possible? Or do I need to find another solution? - gtk

I have a Database with this fields: Firstname, Lastname, picture.
I have a gtk.Label named infoLabel.
I would like to update the label a way like this:
Label.set_markup("<img src='pix/1.png'>\nName" + Firstname + " " + Lastname)
But it isn't works, because img isn't allowed in Pango markup. How I can do this?

Here is an example of a "Home" label.
label_with_icon = gtk.HBox()
icon = gtk.image_new_from_stock(gtk.STOCK_HOME, gtk.ICON_SIZE_MENU)
label_with_icon.pack_start(icon)
label_with_icon.pack_start(gtk.Label('Home'))
label_with_icon.show_all()

I wouldn't expect to be able to have an image in label, no.
But you should be able to replace any label with a container holding a GtkImage and the label, and solve it that way.
Of course you will have to have the image in local memory, first.

This is based on John La Rooy's answer, but I had to make some adjustments. Don't know why, maybe due to a different version of the library or something.
label_with_icon = gtk.HBox()
icon = Gtk.Image.new_from_file("/path/to/file.png") # not .image_new_from_file
label_with_icon.pack_start(icon, False, False, 0) # this method takes five args
label_with_icon.pack_start(gtk.Label('a caption'))
label_with_icon.show_all()

Related

Cocoa/Swift: NSStatusBarItem's text gets trimmed when using a picture

When creating an NSStatusBar.systemStatusBar related application for MacOS, I stumbled upon a problem that seemed strange to begin with. The problem is: when using an image and a text for the NSStatusBar, the text was being clipped unless you manually specified a sufficient length, which would be hardcoded and causes problems with alternating lengths. How can this be solved?
// The -1 is supposed to mean "variable length"
let myStatusItem = NSStatusBar.systemStatusBar().statusItemWithLength(CGFloat(-1))
myStatusItem.image = NSImage(named: "customImage")
myStatusItem.title = "Some special information"
This would be the normal case where this problem will occur.
After playing around with some ridiculous variations, I realized that the problem can be fixed when using BOTH .title and .attributedTitle parameters of the NSStatusBar item.
Also make sure that you declare the .image BEFORE you declare the titles. If you want to use the .attributedTitle, define it after .title - if you want to use the plain .title, just define it after the .attributedTitle.
// The -1 is supposed to mean "variable length"
let myStatusItem = NSStatusBar.systemStatusBar().statusItemWithLength(CGFloat(-1))
myStatusItem.image = NSImage(named: "customImage")
myStatusItem.title = "Some special information"
myStatusItem.attributedTitle = NSAttributedString(string: "Some special information")

How to place widgets like labels & buttons with `.pack()`?

Hello i am asking for help on a PyDev Eclipse problem i cold not find a solution on at this forum. My example below show that i have a window with a couple of labels and buttons etc. But the ting is that when i searched earlier i found posts on how to place widgets using .grid() and .place() but not any for .pack() which i am using. Therefor i want to know what would be the best way for me to place these widgets for example in the center of the parent window.
Thanks in advance. :D
window.title("PTF Pydev Eclipse")
window.geometry("500x500")
window.config(bg="blue")
frame = tk.Frame(window, bg="blue")
lbl = tk.Label(frame, text= "secret")
lbl.config(background="blue", width="500")
ent = tk.Entry(frame)
lbl1 = tk.Label(frame, text= "secret", bg="blue")
ent1 = tk.Entry(frame)
lbl2 = tk.Label(frame, text= "secret", bg="blue")
btn = tk.Button(frame, text="secret", command= lambda: validation())
btn2 = tk.Button(frame, text="secret", command=lambda: page1())
lbl3 = tk.Label(frame, bg="blue", text="secret")
ent2 = tk.Entry(frame) #these 3 are packed in a function (not included).
lbl.pack()
ent.pack()
lbl1.pack()
ent1.pack()
lbl2.pack()
btn.pack()
frame.pack()
window.mainloop()
Do not use pack if you want to control the exact position of each widget.
Your window is 500x500, right ?
So for example if you want to place a widget at the top left of your window, you could to
Yourwidget.place(x=10, y=10)
Hope It helps, good luck.

GWT Dynamically Changing an Image

I want to change dynamically an image by using the ID and a Ressource Bundle.
Element changedImage;
/* imageID is the Id for the previous image */
changedImage = Document.get().getElementById("imageId");
/* And myImageBundle is the resource Bundle and icon the new image*/
changedImage.setInnerHTML(myImageBundle.icon().getHTML());
But nothing happens , it's the same image . Did i miss something ?
Thanks a lot for answer(s) or suggestions(s).
-------- Solution ---------------
I've found a solution , it works properly, but somehow i think it's not the best one . Everything is on the next function :
private void switchImage( AbstractImagePrototype imageBundled) {
String newStyleFromImageBundle, value;
value = "style=";
newStyleFromImageBundle = extractStyle(value ,imageBundled.getHTML());
Element e = Document.get().getElementById("imageHeaderLogo");
// removing the current style
e.removeAttribute("style");
// setting the new style with the previous one retrieved from the image bundled's html
e.setAttribute("style",newStyleFromImageBundle );
}
Hope it helps, feel free to tell if there's a better way to do it or this the worst you 've ever seen .. :-) .
I think this is specifically what applyTo is for. Why don't you just do the following:
/*assuming that the element with id "imageId" is a real image
element and is not undefined:*/
myImageBundle.icon().applyTo(Image.wrap(Document.get().getElementById("imageId")));
//editted to use Image.wrap
The above should apply the image you have defined in icon() to the image with id "imageId"

Label on HeaderIcon in HeaderControls

I want to put a label next to an icon so user can better understand what is this clickable icon for.
This icon appear in the HeaderControl of a com.smartgwt.client.widgets.Window
String promptNewThing = "Add some thing";
String imgNewThingText = "../servlet/servletOperation?cmd=Icon&time=" + System.currentTimeMillis() + "&text=" + promptNewThing;
HeaderIcon iconNewThing = new HeaderIcon("buttons/new.png");
HeaderIcon iconNewThingLabel = new HeaderIcon(imgNewThingText);
newThing = new HeaderControl(iconNewThing, clickHandlerNewThing);
newThing.setPrompt(promptNewThing);
newThingLabel = new HeaderControl(iconNewThingLabel, clickHandlerNewThing);
newThingLabel.setPrompt(promptNewThing);
setHeaderControls(HeaderControls.HEADER_LABEL, NewThing, NewThingLabel, HeaderControls.MINIMIZE_BUTTON, HeaderControls.MAXIMIZE_BUTTON, HeaderControls.CLOSE_BUTTON);
For information : In this code, Servlet URL return an image made from the text promptNewThing
But iconNewThingLabel is an Icon which has the width of an Icon, too small.
The result of this code :
The small white square we can see between first Icon is newThingLabel.
Is there a way to add a label at the right of an HeaderIcon ?
For example I would to have something like :
Hello Manu I have done the same thing as you have done. The only change is instead of adding HeaderIcon (iconNewThingLabel in your case - if it's representing the text), I have used a com.smartgwt.client.widgets.Label & added that as a headerControl. Code snippet is as follows:
Label label = new Label("test header");
setHeaderControls(HeaderControls.HEADER_LABEL, NewThing, label, HeaderControls.MINIMIZE_BUTTON, HeaderControls.MAXIMIZE_BUTTON, HeaderControls.CLOSE_BUTTON);
Let me know if this helps you.

Image alignment in text?

Using iTextSharp i'm trying to align an image so that it gets embedded in a paragraph. I can do it like this:
iTextSharp.text.Image image;
image.Alignment = Image.ALIGN_RIGHT | Image.TEXTWRAP;
document.Add(image);
document.Add(new Paragraph("Large string of text goes here"));
But the image comes out on the top right with the text surrounding it (kind of like an L)
What I want is the text to be a few paragraphs then the image with text below it (kind of like a C). Does anyone know how I would do this VIA iTextSharp?
Edit:
I also tried
iTextSharp.text.Image image;
image.Alignment = Image.ALIGN_RIGHT | Image.TEXTWRAP | Image.ALIGN_MIDDLE;
document.Add(image);
document.Add(new Paragraph("Large string of text goes here"));
But it was displayed with the image at the top and the text below it. There was no textwrap in effect.
The Phrase and the Paragraph objects do behave differently. Try changing to:
image.Alignment = 6;
document.Add(image);
document.Add(new Phrase("Large string of text goes here"));
This worked for me in VB. ( I had to change the image alignment to the sum of the integer values for ALIGN_RIGHT and TEXTWRAP to get this to work properly).
ALIGN_RIGHT = 2
TEXTWRAP = 4
Your image was displayed at the top of the page because it was the first thing added to the document, and the text was added after it.
You can move the image down by either setting its absolute position, or by adding some of your text to the document, then adding the image, then adding the rest of your text.
The easiest way to embed image into paragraph is to use InlineImage from PDFFlow library.
InlineImage was created exactly for this purpose.
Example of adding an inline image with margins:
var imagePath = "imageFile.png";
DocumentBuilder.New()
.AddSection()
.AddParagraph()
.AddTextToParagraph("Inline image")
.AddInlineImage(imagePath, new XSize(16, 16), ScalingMode.UserDefined)
.SetMarginLeft(8)
.SetMarginRight(8)
.ToParagraph()
.AddText("in paragraph.")
.ToDocument()
.Build("Result.pdf");
The above code will generate the following:
Here are business documents examples with source code: Examples.
Hope, this will help.