I am making a small pop up to a text game I am making. I just want a small frame to pop up with some text and a picture to the far left but in a custom size and position. How would I do this? How do I insert text and change the image position and size, this is my current code, it works as in there are no errors and the image shows.
from tkinter import *
import time, sys
root = Tk()
root.configure(background='#16e116')
root.title('Pop Up')
root.geometry('300x200')
photo = PhotoImage(file='file.gif')
w = Label(root, image=photo)
w.pack()
root.mainloop()
If you want your image to be a different size (without it being deformed by resizing the widget) you have to make it the desired size in your image editor.
If you want to change the image position then use grid() where pack() has very limited placement options.
If you want text on a label, then use: text = Label(root, text="Hello world!"). This also comes with various options and styles such as font and it's size.
If you want the text over the image then use grid() and place it in the middle of the image by using rows and columns.
import tkinter
from tkinter import *
root = tkinter.Tk()
root.configure(background='#16e116')
root.title('Pop Up')
root.geometry('300x200')
photo = PhotoImage(file='file.gif')
w = Label(root, image=photo)
text = Label(root, text="Hello world!")
w.grid(row=3, column=3)
text.grid(row=3, column=3)
root.mainloop()
Although, the label will still have the background around it but you change the background colour using widgetname.config(bg="Colour")to make it suit your standards.
Now of course, I can't be sure the above will work because I don't have file.gif but you should also read tutorials before jumping straight into code to avoid confusion.
Related
I am so new to python. I am trying to implement a program which changes a button's image property with an "on click" operation. I have tried png, jpeg, gif and bmp formats. I don't want to use PIL, I just want to handle it with tkinter. When I click on the button a small square appears on button but there is no actual image that can be seen. I have tried many things, search StackOverflow and the Internet, and couldn't come up with a solution. Is the problem caused by the version problem or something?
My code is below, thank you so much for your support:
from tkinter import *
def insertRedDisc(buttonParam):
image = PhotoImage(file=r"C:\Connect4\red.png")
buttonParam['image'] = image
window = Tk()
window.title("Button State App")
window.geometry('1000x1000')
window['bg'] = "blue"
for i in range(6):
for j in range(7):
frame = Frame(master=window, relief=FLAT, borderwidth=5, bg="blue")
frame.grid(row=i, column=j, padx=1, pady=1)
myButton = Button(master=frame, width=5, height=5)
myButton['command'] = lambda theButton=myButton: insertRedDisc(theButton)
myButton.pack()
window.mainloop()
App currect image
I've made a GUI using Tkinter. For the time being, I set the geometry to '962x652' and made it so the user can't resize it by using .resizable(0, 0). I'm now looking for a way to make it so when the GUI is resized, all the elements change size as well along side it, preferably with the aspect ratio locked.
Is there a way to achieve this whilst making it so only the element change size visibly without changing actually changing size? For example, if my GUI contains a scrolledtext box, if the user was to resize the GUI, the scrolledtext will also increase in size, but so will the text and each line of text in it will stay in the same line. I hope that makes sense.
I've add some code down below to show what I'm working with. This should have a similar effect to what I currently have:
import tkinter as tk
from tkinter import *
from tkinter import scrolledtext
TrialGUI = Tk()
TrialGUI.title('Resize GUI')
TrialGUI.geometry('962x652+0+0')
#Remove the following so you can resize the GUI
TrialGUI.resizable(0, 0)
#These are the two frames. I've changed their colour so they are visible when they are resized.
ABC1b = Frame(TrialGUI, bg='lightpink', bd=20, width=900, height=600)
ABC1b.grid(row=0, column=0)
ABC2 = Frame(TrialGUI, bg='lightblue', bd=20, width=452, height=600)
ABC2.grid(row=0, column=1)
#This is the text box
txtQuestion = scrolledtext.ScrolledText(ABC1b, wrap=tk.WORD, width=42, height=10, font=(14))
txtQuestion.grid(row=0, column=0, columnspan=4, pady=3)
#This inserts text into it. I made it insert which line the text should be in so it is easier to check if it is still in the same line when it is resized.
txtQuestion.insert(tk.INSERT, 'This should be in the first line.----------------------------------- This should be in the second line.------------------------------ This should be in the third line.----------------------------------')
TrialGUI.mainloop()
If anyone decides to play around with this code, remember to remove TrialGUI.resizable(0, 0) so it can be resized.
I want to make a text widget in pixel size. But always set on font size.
Here is my code:
import tkinter as tk
root = tk.Tk()
test_btn = tk.Button(root, text="test", height=10, width=10)
test_btn.pack()
root.mainloop()
I want to use pixel, but i can't so, I am using "place" method, so my code looks dirty.
I want to change them to "pixel size" and pack.
The width attribute is in units of characters if the button has text, but is in units of pixels if the button has an image. So, you can add a small transparent image to the button and set the compound option to allow both text and image at the same time. When you do that, width will be interpreted as pixels.
Example:
image = tk.PhotoImage(width=1, height=1)
button = tk.Button(root, image=image, text="Hello, world", compound="center",width=300)
I am trying to display an image on my desktop without any boarder or window, like an image floating in the desktop. I also want to be able to control its position once it has been created, with the arrow keys or with a line of code that changes its position with some coordinate system of some sorts. I haven't found any method after some reaserch (not in Python, at least).
If that's not possible, please recommend another programming language that can.
You can use Tkinter for this.
I made a little example:
Python3
from tkinter import Toplevel, Tk, Label, PhotoImage
win = Tk()
win.attributes('-alpha', 0.0)
win.iconify()
window = Toplevel(win)
window.geometry("500x500+100+100")
window.overrideredirect(1)
photo = PhotoImage(file="test.png")
label = Label(window, image=photo)
label.pack()
win.mainloop()
Python2
from Tkinter import Toplevel, Tk, Label
import ImageTk
win = Tk()
win.attributes('-alpha', 0.0)
win.iconify()
window = Toplevel(win)
window.geometry("500x500+100+100") # create an window 500x500 pixel, 100 pixels from the upper left corner
window.overrideredirect(1) # Take the border away
photo = ImageTk.PhotoImage(file="test.png")
label = Label(window, image=photo)
label.pack()
win.mainloop()
I am creating an Arduino interface on Sublime using python Tkinter..
I need to show a text over an image. Located in the middle of the screen (512, 200). I don't know how to do it using this library
import Tkinter as tk
from Tkinter import *
root = tk.Tk()
root.geometry("1024x574")
root.title("window")
photo = tk.PhotoImage(file= r"hi.gif")
cv = tk.Canvas()
cv.pack(side='top', fill='both', expand='yes')
cv.create_image(0, 0, image=photo, anchor='nw')
text=['my text']
root.mainloop()
Any suggestions?
You need to create a tk label widget and add your text to it. Then you need to use the tk label option compound=.
Taken directly from http://effbot.org/tkinterbook/label.htm:
"compound=
Controls how to combine text and image in the label. By default, if an image or bitmap is given, it is drawn instead of the text. If this option is set to CENTER, the text is drawn on top of the image. If this option is set to one of BOTTOM, LEFT, RIGHT, or TOP, the image is drawn besides the text (use BOTTOM to draw the image under the text, etc.). Default is NONE."
The following is a minimal but working example that accomplishes what you asked for:
import tkinter as tk
from PIL import Image, ImageTk
root = tk.Tk()
image = Image.open('hi.gif')
tk_image = ImageTk.PhotoImage(image)
label = tk.Label(root, text='Some Plain Text', image=tk_image, compound='center')
label.pack()
root.mainloop()