Insert an image in a labelFrame title with tkinter - python-3.x

I wonder if it's possible to insert an image or icon in the title of a Labelframe in tkinter ?
Maybe something like this lf = Labelframe(root, image='my_img'), the same as we can do with a button.
It would be very useful.

You can insert any widget as labelwidget option of LabelFrame.
If you want an image instead of text displayed in LabelFrame:
import tkinter as tk
from PIL import Image, ImageTk
w = tk.Tk()
w.geometry("400x500")
img_open = Image.open("example_1.png")
img = ImageTk.PhotoImage(img_open)
label_title = tk.Label(w, image=img) #important Do Not grid/pack/place it!
label_frame = tk.LabelFrame(w, labelwidget=label_title)
label_frame.grid(row=0, column=0)
#fill LabelFrame with something to make it visible
bt = tk.Button(label_frame, text="Press")
bt.grid(padx=20, pady=20)
w.mainloop()
Output:
Or like I said before you can use any widget.
An example label with image and a label with text.
import tkinter as tk
from PIL import Image, ImageTk
w = tk.Tk()
w.geometry("400x500")
img_open = Image.open("example_1.png")
img = ImageTk.PhotoImage(img_open)
frame_labelwidget = tk.Frame(w) #important Do Not grid/pack/place it!
label_image = tk.Label(frame_labelwidget, image=img)
label_image.grid(row=0, column=0)
label_text = tk.Label(frame_labelwidget, text="Teamwork")
label_text.grid(row=0, column=1)
label_frame = tk.LabelFrame(w, labelwidget=frame_labelwidget)
label_frame.grid(row=0, column=0)
#fill LabelFrame with something to make it visible
bt = tk.Button(label_frame, text="Press")
bt.grid(padx=20, pady=20)
w.mainloop()
Output:
Of course that makes less sense because you can have text and an image combined in tk.Label by using compound option.
I added it only for demonstration purpose, you can add any widget you want. Instead of label_text you could add an Entry, Canvas...

Related

Python: TypeError: 'Label' object is not callable

from PIL import Image, ImageTk
from tkinter import *
from tkinter import Label
def open_window():
menu = Toplevel(root)
menu.geometry("800x800")
menu.title("my game's menu")
menu.resizable(False, False)
menu.geometry("800x800")
lbl = Label(menu, text ="Hello!").pack
menu.mainloop()
root = Tk()
root.geometry("400x300")
Label = Label(root, text="Are you ready?")
Label.pack()
root.title("quick question")
btn = Button(root, text="Yes", command= open_window)
btn.pack(padx=20, pady = 20)
root.mainloop()
I got this error while I was working on previously seen code: File "C:\Users\User\Desktop\naujas zaidimas\scratch.py", line 11, in open_window
lbl = Label(menu, text ="Hello!").pack
TypeError: 'Label' object is not callable
Does anyone know why/how to fix it?
The code has a number of fairly obvious errors and one insidious error
This import plays no role in current code
from PIL import Image, ImageTk
Not a good or preferred way to import tkinter
This will cause problems later on
from tkinter import *
This is unnecessary with the current inport method
from tkinter import Label
def open_window():
The rule for Python functions is: Names created in functions stay in functions.
It will require a global instruction to make 'menu' available elsewhere in your code
menu = Toplevel(root)
menu.geometry("800x800")
menu.title("my game's menu")
menu.resizable(False, False)
Duplicated geometry instruction
menu.geometry("800x800")
This object has already been defined as a Label object so trying to name it throws a TypeError
lbl = Label(menu, text ="Hello!").pack()
This is unnecessary since root.mainloop() has already been executed
menu.mainloop()
root = Tk()
root.geometry("400x300")
Here is another naming problem caused by the import method chosen
Label = Label(root, text="Are you ready?")
Label.pack()
root.title("quick question")
This button will enable you to create MANY Toplevel windows
The problem is, ALL of them will be called 'menu'!?
This is the insidious error
btn = Button(root, text="Yes", command= open_window)
btn.pack(padx=20, pady = 20)
root.mainloop()
# This solution avoids all the previous problems
import tkinter as tk
root = tk.Tk()
root.title("quick question")
root.geometry("400x300")
tk.Label(root, text = "Are you ready?").pack()
# This will give tkinter time to process the given instructions.
root.update()
menu = tk.Toplevel(root)
# withdraw will make menu temporarily invisible
menu.withdraw()
menu.title("my game's menu")
menu.geometry("800x800")
menu.resizable(False, False)
tk.Label(menu, text = "Hello!").pack()
# command will now make menu window visible
btn = tk.Button(root, text = "Yes", command = menu.deiconify)
btn.pack(padx = 20, pady = 20)
root.mainloop()
from PIL import Image, ImageTk
from tkinter import *
def open_window():
menu = Toplevel(root)
menu.geometry("800x800")
menu.title("my game's menu")
menu.resizable(False, False)
menu.geometry("800x800")
lbl = Label(menu, text ="Hello!").pack()
menu.mainloop()
root = Tk()
root.geometry("400x300")
lbl1 = Label(root, text="Are you ready?").pack()
root.title("quick question")
btn = Button(root, text="Yes", command= open_window)
btn.pack(padx=20, pady = 20)
root.mainloop()
I Played around both our codes and now somewhy it works.

button to take frame screenshot

can someone help me take a screnshot of a specific frame?
been playing with these, but cant seem to specify just the frame
import pyautogui
#im1 = pyautogui.screenshot()
#im1.save('my_screenshot.png')
#im2 = pyautogui.screenshot('my_screenshot2.png')
from tkinter import *
import time
from PIL import ImageTk, Image
import pyautogui as pg
# Create an instance of tkinter frame or window
win = Tk()
# Set the size of the window
win.geometry("700x350")
frm2shoot = Frame(win)
frm2shoot.grid(column=0, row=0)
lbl = Label(frm2shoot, width=16, text="testing testing:", justify=LEFT, anchor="w").grid(row=0, column=0, sticky=W, pady=2)
# Define a function for taking screenshot
def screenshot():
random = int(time.time())
filename = "/Users/ricardosimoes/Desktop/"+ str(random) + ".jpg"
ss = pg.screenshot(filename)
ss.show()
frm2shoot.deiconify()
# Create a Button to take the screenshots
button = Button(win, text="Take Screenshot", font=('Aerial 11 bold'), background="#aa7bb1", foreground="white", command=screenshot)
button.grid(column=5, row=0)
win.mainloop()
Anyone have any idea how to do this??

Treeview Image not displaying

having problems displaying an image using treeview. Found some other mentions on the net with a similar problem but the answers do not seem to work for me. I am using Win10 if that makes a difference
import ttkthemes
import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showinfo
from tkinter import PhotoImage
from PIL import Image, ImageTk
self = tk.Tk()
self.title('Tkinter PhotoImage Demo')
#self.image = Image.open("A4.bmp")
#self.python_image = ImageTk.PhotoImage(self.image)
#print(self.python_image)
#ttk.Label(self, image=self.python_image).pack()
# columns
columns = ('#1', '#2')
tree = ttk.Treeview(self, columns=columns, show='headings', height=20)
tree.tag_configure('oddrow', background='#ece0cf')
tree.tag_configure('evenrow', background='#e0e0e0')
style = ttk.Style()
style.theme_use('clam')
style.configure("Treeview",font=(None,12))
style.configure("Treeview.Heading", font=(None, 12))
# define headings
tree.heading('#1', text='Date')
tree.column("#1", minwidth=0, width=160)
tree.heading('#2', text='Logo')
tree.column("#2", minwidth=0, width=80)
# add a scrollbar
scrollbar = ttk.Scrollbar(self, orient=tk.VERTICAL, command=tree.yview)
tree.configure(yscroll=scrollbar.set)
scrollbar.grid(row=0, column=1, sticky='ns')
tree.grid(row=0, column=0, sticky='nsew')
logo="A4.bmp"
rowcol='oddrow'
im = Image.open(logo)
ph = ImageTk.PhotoImage(im)
print(ph)
tree.insert('', -1, values=("2021-03-25", logo), image=ph, tags=(rowcol,))
tree.grid(row=0, column=0, sticky='nsew')
self.update_idletasks()
self.update()
If it helps the value of ph is: pyimage1
The commented out code at the start displays the image just fine, so the issue seems to be on adding the image itself into the tree.insert.... bit of coding
Whilst I have your attention and as related but very minor questions, is there also a way to add a second image into treeview? Plus is there a way to display some values, then an image, then some more values, then another image and finally more values, or do the images have to be at the start or end of the rows?

Python tkinter how can I copy information from a Text widget to a Canvas widget?

To put it simple, i want to write stuff in the text widget and then have it copied onto a canvas widget on the same coordinates (in the code below the Text widget size is the same as the canvas one)
here is an image of the code
Here is the written code:
from tkinter import *
#root configuration
root = Tk()
root.config(bg='black')
#function that "replaces" text with canvas
def replace_TextWidget_with_CanvasWidget():
global text_editor
global written_text
#gets text from the text widget and destroys it
written_text = text_editor.get('0.0', '100.0')
text_editor.destroy()
#i want the copied text from the text widget to be inserted on the canvas
text_editor = Canvas(root, height=851, width=601, bg='white')
text_editor.pack()
text_editor.insert()
#button
button = Button(root, command=replace_TextWidget_with_CanvasWidget)
button.pack()
#initial text widget
text_editor = Text(root, height=50, width=75)
text_editor.pack()
text_editor.insert('1.0', 'HERE I WRITE SOMETHING I WANT TO BE COPIED ON THE CANVAS LATER')
root.mainloop()
It looks like you're trying to show some text to display only. If that's the case, set it's state property to disabled after updating it.
text_editor.configure(state='disabled')
However, if you really want to replace it with a canvas with the same information, here is your modified code. Please note that canvas widget doesn't have any insert method. Instead, it has a create_text(x,y,text='my text') method.
from tkinter import * #Not a good programming approach, use import tkinter as tk instead
#function that "replaces" text with canvas
def replace_TextWidget_with_CanvasWidget():
global text_editor
#gets text from the text widget and destroys it
written_text = text_editor.get('0.0', '100.0')
text_editor.destroy()
#i want the copied text from the text widget to be inserted on the canvas
new_canvas = Canvas(root, height=200, width=400, bg='white')
new_canvas.pack()
root.update() #Necessary if you want to get the current coordinates of the canvas
# print(new_canvas.winfo_x(),new_canvas.winfo_y()) #print canvas x,y pos
new_canvas.create_text(new_canvas.winfo_x()+200,new_canvas.winfo_y(), text=written_text) #Create text at x,y coords
#root configuration
root = Tk()
root.config(bg='black')
#button
button = Button(root, width=10, height=2, text="OK", command=replace_TextWidget_with_CanvasWidget)
button.pack(padx=10, pady=10)
#initial text widget
text_editor = Text(root, height=10, width=50)
text_editor.pack()
text_editor.insert('1.0', 'HERE I WRITE SOMETHING I WANT TO BE COPIED ON THE CANVAS LATER')
root.mainloop()
You may have trouble positioning the text at the correct x,y co-ordinates and with text wrapping. If you want text wrapping, create_text has a width property. You can specify it to a specific dimension like width=100
😉
To add text to Canvas
text_id = text_editor.create_text(0, 0, text=written_text, anchor='nw')
# from tkinter import * # PEP8: `import *` is not preferred
import tkinter as tk
# --- functions ---
def replace_TextWidget_with_CanvasWidget():
global text_editor
global written_text
written_text = text_editor.get('1.0', 'end')
text_editor.destroy()
text_editor = tk.Canvas(root, height=851, width=601, bg='white')
text_editor.pack()
text_id = text_editor.create_text(0, 0, text=written_text, anchor='nw')
#text_editor.insert(text_id, 'end', written_text)
# --- main ---
root = tk.Tk()
root.config(bg='black')
button = tk.Button(root, text="OK", command=replace_TextWidget_with_CanvasWidget)
button.pack()
text_editor = tk.Text(root, height=50, width=75)
text_editor.pack()
text_editor.insert('1.0', 'HERE I WRITE SOMETHING I WANT TO BE COPIED ON THE CANVAS LATER')
root.mainloop()

tkinter GUI- label image not showing but is still there (kinda)

I'm trying to show a die at random to a tkinter GUI, but it does not work as expected.
from tkinter import *
from random import choice
def change_pic():
die1 = PhotoImage(file=("dice-1.png"))
die2 = PhotoImage(file=("dice-2.png"))
die3 = PhotoImage(file=("dice-3.png"))
die4 = PhotoImage(file=("dice-4.png"))
die5 = PhotoImage(file=("dice-5.png"))
die6 = PhotoImage(file=("dice-6.png"))
faces=[die1, die2, die3, die4, die5, die6]
label.config(image=choice(faces))
label.grid(row=1, column=1)
root = Tk()
label = Label(root)
label.grid(row=1, column=1)
change_button = Button(root, text="change", command =change_pic)
change_button.grid(row=1, column=2)
root.mainloop()
this is my code
instead of showing the die image, it just show the place where it should be, and its size.
I tried a lot of things but I cannot fix it. please help.
You choose the image for the label inside a function which puts the image in the function namespace. When the function ends the reference to the image is garbage collected.
You can fix this by saving a reference to the image in the label widget:
faces=[die1, die2, die3, die4, die5, die6]
img = choice(faces)
label.config(image=img)
label.image = img # Save a reference to the image
label.grid(row=1, column=1)

Resources