How to position text ontop of an Image Label - python-3.x

I have been tinkering with Tkinter the last couple of days to create a little game.
I have searched for how to display text ontop of an image label but there is a little downside, it won't display exactly at the spot i am looking(in this case the top left side of the label).
I tried anchor and compound together in the same label but it doesn't seem to do anything new
the text still remains in the middle.
The code looks like this:
'''Adjusting the information frame'''
self.image_info = PhotoImage(file=locate_images("_info_panel", ""))
self.label_text_info = "• Welcome to Agony,\nto play or read the rules please go to File"
self.label_info = Label(self.info, text=self.label_text_info, font=10, image=self.image_info, compound=CENTER, anchor=NW)
self.label_info.grid(row=0, column=0)
Edit:
If I try to add anything else other than compound=CENTER the text is displayed outside the boundingbox of the image on the specified side.

Moved from an edit to the question by the OP to an answer.
Use a Canvas:
'''Adjusting the information frame'''
self.image_info = PhotoImage(file=locate_images("_info_panel", ""))
self.label_text_info = "• Welcome to Agony,\nto start the game or read the rules please go to File"
self.canvas_info = Canvas(self.info, width=300, height=600)
self.canvas_info.create_image((0, 0), image=self.image_info, anchor=NW)
self.canvas_info.create_text((140, 25), text=self.label_text_info)
self.canvas_info.grid(row=0, column=0)
I was approaching it wrong from the start, I am fearly new to tkinter
and I had to use a canvas to display the items inside of.

Related

Tkinter Button not working when inserting an image

I am trying to add buttons with custom bg or image so like 3d color. But it gives a white unclickable button as result. I tried many other ways but failed, the code i gave below its a code which lets us to select the weight and height we want for the button that's why i choosed it otherwise if other things like the .zoom or making a function to make the image fit the button were working i would be using them, they are all not working and i have no idea, i know i must be doing something wrong but wellas you see i am new in python, i learned everything myself so really i am 0 x). I Resized the image aswell, it has the same weight height with the button:
root.geometry(geo)
image = ImageTk.PhotoImage(file='background.png')
label = Label(root, image=image)
label.pack(fill='both', expand=True)
label.image = image
myWidth = 150
myHeight = 82
img4=Image.open('button.png')
image4=img4.resize((myWidth,myHeight))
useImg4=ImageTk.PhotoImage(image4)
quitBtn = Button(label, image=useImg4,command=lambda: root.destroy())
quitBtn.pack()
do you know why its not working?
The result:

Had an issue with tkinter displaying my label on a top level window. A syntax error fixed it?

I am working on a color matching learning game. When you click the colored square, a pop up window will show you the name of the color, typed in the corresponding color, as well as pronounce the color.
During development, I ran into an issue where tkinter would not display my .gif image (which is the typed color) on the top level window, which opens after a color is selected.
I started adding additional widgets to the top level window to attempt to figure out the issue.
However, I accidentally left of the parentheses at the end of pack on the second label I added.
Sure enough, my image showed.
When I went back to add the missing parentheses, my image stopped showing??
def callback(event):
print ("clicked at", event.x, event.y)
if 30 < event.x < 120:
print("Red")
top = tk.Toplevel()
diagrams = tk.PhotoImage(file='Red.gif')
logolbl= tk.Label(top, text = "Red", image = diagrams).pack()
btn = tk.Button(top, text="Back").pack()
##### this is the label I am referring to #####
tk.Label(top, text = "why does this fix it?").pack
if 150 < event.x <240 :
print("Green")
if 270 < event.x < 370:
print("Blue")
This is a screen shot of it displaying the image, with the missing parentheses
This is a screen shot of the second label commented out, and my image not showing
Has anyone experienced this?
I mean technically I can work with this, however, I understand it is incorrect.
I'd like to have an understanding of what I am doing wrong, and what is happening "behind the scenes"

how to change the position of textinput in python/turtle

#welcome Player 1 and print the players name
player1 = sc.textinput("Name of Player X", "Whats your name? ")
movingTurtle.penup()
movingTurtle.goto(-275, 240)
movingTurtle.pendown()
sc.wel_Player1 = movingTurtle.write("Welcome Player X: "+player1,
font=FONT2)
this code works fine , a little "message-box" pop´s up and asks the 1st player for his name -- the problem is that i cant change the position of that TEXTINPUT-BOX -- i dont even know, if this is possible
I've had the same question and I've looked everywhere for an answer!
Unfortunately, from what I've found, there doesn't seem to be any way to modify the position or general look of a textinput box.
Have a look at -> This similar post
This might be what you're looking for.
If anyone can actually propose a different way, that would be much appreciated however my conclusion is that a textinput dialog box cannot be modified sadly.
What I've done is commit to the look by adding a black backdrop-type box behind it and making it a bit more obvious and professional that way.
#the black backdrop
useroptionboxback = turtle.Turtle()
useroptionboxback.speed(0)
useroptionboxback.shapesize(stretch_wid=6.8,stretch_len=10)
useroptionboxback.shape('square')
useroptionboxback.color('black')
useroptionboxback.penup()
useroptionboxback.goto(-215,255)
useroptionboxback.direction = 'stop'
#the tetinput box
title1 = 'Menu'
prompt1 = 'Please type a number.'
useroptionbox = turtle.textinput(title1, prompt1)
wn.update()
Example Screenshot
I would recommend doing something like this if you find what the guy on the link is saying too confusing.

Display number in rectangle

Note that I am using Python3 and Phoenix.
I would like to display a number (double, but that does not matter now) formatted in some way (again, no matter what that way is) within a rectangle: almost a wx.StaticText but not editable by the user. This is to display some data coming from some hardware, such as a temperature.
Is there such a widget?
I tried with using the default wx.StaticText with a style but I must have done something wrong:
hbox = wx.BoxSizer(wx.HORIZONTAL)
title = wx.StaticText(parent, label=label)
title.SetLabelMarkup("<b>{}</b>".format(label))
hbox.Add(title, border=5)
value = wx.StaticText(parent, label="3.141592", style=wx.BORDER_RAISED)
value.SetWindowStyle(wx.BORDER_SIMPLE)
hbox.Add(value, border=5)
title = wx.StaticText(parent, label="\u2103")
hbox.Add(title, border=5)
Shows this on Linux (Fedora 24, GTK):
Wouldn't using a wx.TextCtrl set to read only do the job?
Temp = wx.TextCtrl(panel1, value="3.141592", style=wx.TE_READONLY)
Temp.SetBackgroundColour('green')
The simplest solution is to just use wxStaticText with a border style (e.g. wxBORDER_SIMPLE, ...). If you don't like the appearance this results in, it's pretty simple to make your own widget drawing whatever border you desire: just create a window, define its wxEVT_PAINT handler and draw the (presumably centered) text in it and a border outside of it.

Manipulate pixels in QGraphcisPixmapItem

I am using PyQt to build a small application for viewing images. When I click on the image I would like to alter the color of the pixels I have clicked:
Schematically my current code looks like this:
scene = QtGui.QGraphicsScene()
view = QtGui.QGraphicsView( scene )
image = QtGui.QImage( "image.png" )
pixmap = QtGui.QGraphicsPixmapItem( QtGui.QPixMap.fromImage( image ))
scene.addItem( pixmap )
...
...
def mousePressEvent(self , event):
print "Click on pixmap recorded - setting Pixel to red"
image.setPixel( event.pos() , RED.rgb())
The code 'works' in the sense that the mousePressEvent() method is called, and the image.setPixel() method does not give any errors, but nothing happens on the screen. Any tips on how to get the updated pixels to be displayed?
Joakim
To make changes appear, you need to reload image
self.image.setPixel(event.pos(), RED.rgb())
self.pixmap.setPixmap(QtGui.QPixmap.fromImage(self.image))
But I'm not sure that it's a good way. If you don't need to save modified image, I'd add some circles (e.g. addEllipse) instead modifying pixels.
Also, don't forget to map window coordinates to image.

Resources