How to scroll contents in label? - python-3.x

I want to scroll contents in a label. I'm a begginer. Please help me.
I did it like below.But it doesn't working. I found this from the internet. But it doesn't working. I take some data from a .txt file and i need to display in a lable. The lable is not enough to display them all. So I need to scroll the contents in the label.
from tkinter import ttk
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk
window=tk.Tk()
im = Image.open("landscape2.png")
tkimage = ImageTk.PhotoImage(im)
tab_control = ttk.Notebook(window)
tab5 = ttk.Frame(tab_control)
tab_control.add(tab5, text='History')
tab_control.pack(expand=1, fill='both')
his_lbl = tk.Label(tab5, image=tkimage)
his_lbl.place(relwidth = 1, relheight = 1)
his_frame = tk.Frame(tab5, bg='#80c1ff',bd=5)
his_frame.place(relx = 0.3, rely = 0.1, relheight=0.1, relwidth=0.50,
anchor= 'n')
button = tk.Button(his_frame, bg = 'white', command = lambda:
get_weather(his_entry.get()))
button.place(relx = 0.7, relheight = 1, relwidth = 0.3)
his_entry = tk.Entry(his_frame, font =('Courier', 18))
his_entry.place(relheight = 1, relwidth = 0.65)
canvas = Canvas(tab5, bg="white")
canvas.place(relx = 0.3, rely = 0.25, relheight = 0.6, relwidth = 0.50,
anchor='n')
lst = []
y = 0
label = Label(canvas,anchor='w', font=("Courier", 20),
compound=RIGHT,bg='white',bd=4, justify="left")
label.place(relwidth=1,relheight=1)
canvas.create_window(0, y, window=label, anchor=NW)
y += 60
scrollbar = Scrollbar(canvas, orient=VERTICAL, command=canvas.yview)
scrollbar.place(relx=1, rely=0, relheight=1, anchor=NE)
canvas.config(yscrollcommand=scrollbar.set, scrollregion=(0, 0, 0, y))
def get_weather(history):
file=open((history+".txt"),("r"))
a=(file.read())
label['text'] = a
window.mainloop()

What you are trying to do with a Canvas just to make a Label scrollable is an overkill in my opinion. You could use a Text widget instead and set its state to disabled to prevent the user from editing its content.
import tkinter as tk
root = tk.Tk()
# create text widget
text = tk.Text(root, background=root.cget('background'), relief='flat', height=10)
# insert text
content = "Long text to display\n" * 20
text.insert('1.0', content)
# disable text widget to prevent editing
text.configure(state='disabled')
# scrolling
scroll = tk.Scrollbar(root, orient='vertical', command=text.yview)
text.configure(yscrollcommand=scroll.set)
scroll.pack(side='right', fill='y')
text.pack(side='left', fill='both', expand=True)
root.mainloop()

Related

How can I scroll multiple frames in canvas?

I want to create a list of frames with further features like a button, label e.g.. but my issues are the size of the LabelFrame. If I put the LabelFrame in container it fits like I want to but it isn't scrollable any more. Any ideas?
from tkinter import ttk
import tkinter as tk
root = tk.Tk()
container = ttk.Frame(root)
canvas = tk.Canvas(container)
scrollbar = ttk.Scrollbar(container, orient="vertical", command=canvas.yview)
scrollable_frame = ttk.Frame(canvas)
scrollable_frame.bind(
"<Configure>",
lambda e: canvas.configure(
scrollregion=canvas.bbox("all")
)
)
canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
canvas.configure(yscrollcommand=scrollbar.set)
for i in range(50):
lf = ttk.Frame(scrollable_frame, text=i).grid(column=1, row=i)
frame_ip = tk.LabelFrame(lf, bg="white", text=i)
frame_ip.place(relwidth=0.95, relheight=0.2, relx=0.025, rely=0)
button_scroll1 = tk.Button(frame_ip, text="Start", bg="grey")
button_scroll1.place(relwidth=0.15, relx=0.025, relheight=0.15, rely=0.1)
container.pack()
canvas.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")
root.mainloop()
Here is your updated code with a Button, Canvas and Scrollbar inserted into each LabelFrame with grid manager.
I've also made the container resizable with row|column configure.
Seems to work fine.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.rowconfigure(0, weight = 1)
root.columnconfigure(0, weight = 1)
root.geometry("341x448")
container = ttk.Frame(root)
container.rowconfigure(0, weight = 1)
container.columnconfigure(0, weight = 1)
canvas = tk.Canvas(container)
scrollbar = ttk.Scrollbar(container, orient = tk.VERTICAL, command = canvas.yview)
scrollable_frame = ttk.Frame(canvas)
scrollable_frame.bind(
"<Configure>",
lambda e: canvas.configure(
scrollregion=canvas.bbox("all")))
canvas.create_window((0, 0), window = scrollable_frame, anchor = tk.NW)
canvas.configure(yscrollcommand = scrollbar.set)
for i in range(15):
L = ttk.LabelFrame(scrollable_frame, text = "Sample scrolling label")
L.grid(row = i, column = 0, sticky = tk.NSEW)
B = ttk.Button( L, text = f"Button {i}")
B.grid(row = 0, column = 0, sticky = tk.NW)
K = tk.Canvas(
L, width = 300, height = 100,
scrollregion = "0 0 400 400", background = "#ffffff")
K.grid(row = 1, column = 0, sticky = tk.NSEW)
S = ttk.Scrollbar( L, command = K.yview)
S.grid(column = 1, row = 1, sticky = tk.NSEW)
K["yscrollcommand"] = S.set
container.grid(row = 0, column = 0, sticky = tk.NSEW)
canvas.grid(row = 0, column = 0, sticky = tk.NSEW)
scrollbar.grid(row = 0, column = 1, sticky = tk.NS)
root.mainloop()

Tkinter: How to make a rounded corner text widget?

Question
How to create a rounded corner text widget? I think I have an idea of creating a rounded canvas and fill the entire canvas with the text box with no border.
Problem
The code to create a rounded border canvas is not working when I try to extend the height and width of the canvas. The line breaks and becomes discontinuous.
The code to create rounded corner canvas is taken from How to make a tkinter canvas rectangle with rounded corners?
My code with custom width and height
def rounded_rect(canvas, x, y, w, h, c):
canvas.create_arc(x, y, x+2*c, y+2*c, start= 90, extent=90, style="arc")
canvas.create_arc(x+w-2*c, y+h-2*c, x+w, y+h, start=270, extent=90, style="arc")
canvas.create_arc(x+w-2*c, y, x+w, y+2*c, start= 0, extent=90, style="arc")
canvas.create_arc(x, y+h-2*c, x+2*c, y+h, start=180, extent=90, style="arc")
canvas.create_line(x+c, y, x+w-c, y )
canvas.create_line(x+c, y+h, x+w-c, y+h )
canvas.create_line(x, y+c, x, y+h-c)
canvas.create_line(x+w, y+c, x+w, y+h-c)
import tkinter
root = tkinter.Tk()
canvas = tkinter.Canvas(root)
canvas.grid(row=0,column=0)
# The width and height has been changed from the original solution
rounded_rect(canvas, 10, 10, 330, 290, 10)
root.mainloop()
Output
One solution is to use a ttk frame with a custom style that uses an image for the corners.
The following example is adapted from a tcl/tk solution I wrote back in 2007. It uses an odd color for the focus, and requires a white background, but you could create your own image to replace the one used in this solution (or edit the image in this example1)
# adapted from http://wiki.tcl.tk/%0920152
import tkinter as tk
from tkinter import ttk
focusBorderImageData = '''
R0lGODlhQABAAPcAAHx+fMTCxKSipOTi5JSSlNTS1LSytPTy9IyKjMzKzKyq
rOzq7JyanNza3Ly6vPz6/ISChMTGxKSmpOTm5JSWlNTW1LS2tPT29IyOjMzO
zKyurOzu7JyenNze3Ly+vPz+/OkAKOUA5IEAEnwAAACuQACUAAFBAAB+AFYd
QAC0AABBAAB+AIjMAuEEABINAAAAAHMgAQAAAAAAAAAAAKjSxOIEJBIIpQAA
sRgBMO4AAJAAAHwCAHAAAAUAAJEAAHwAAP+eEP8CZ/8Aif8AAG0BDAUAAJEA
AHwAAIXYAOfxAIESAHwAAABAMQAbMBZGMAAAIEggJQMAIAAAAAAAfqgaXESI
5BdBEgB+AGgALGEAABYAAAAAAACsNwAEAAAMLwAAAH61MQBIAABCM8B+AAAU
AAAAAAAApQAAsf8Brv8AlP8AQf8Afv8AzP8A1P8AQf8AfgAArAAABAAADAAA
AACQDADjAAASAAAAAACAAADVABZBAAB+ALjMwOIEhxINUAAAANIgAOYAAIEA
AHwAAGjSAGEEABYIAAAAAEoBB+MAAIEAAHwCACABAJsAAFAAAAAAAGjJAGGL
AAFBFgB+AGmIAAAQAABHAAB+APQoAOE/ABIAAAAAAADQAADjAAASAAAAAPiF
APcrABKDAAB8ABgAGO4AAJAAqXwAAHAAAAUAAJEAAHwAAP8AAP8AAP8AAP8A
AG0pIwW3AJGSAHx8AEocI/QAAICpAHwAAAA0SABk6xaDEgB8AAD//wD//wD/
/wD//2gAAGEAABYAAAAAAAC0/AHj5AASEgAAAAA01gBkWACDTAB8AFf43PT3
5IASEnwAAOAYd+PuMBKQTwB8AGgAEGG35RaSEgB8AOj/NOL/ZBL/gwD/fMkc
q4sA5UGpEn4AAIg02xBk/0eD/358fx/4iADk5QASEgAAAALnHABkAACDqQB8
AMyINARkZA2DgwB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TqcqswvyxzzDS/HDMHEiiggQMLDxCZXh8k
BnEBCQTggAUGGKCB0ktr0PTTTEfttNRQT22ABR4EkEABDXgnGUEn31ZABglE
EEAAWaeN9tpqt832221HEEECW6M3wc+Hga3SBgtMODBABw00UEEBgxdO+OGG
J4744oZzXUEDHQxwN7F5G7QRdXxPoPkAnHfu+eeghw665n1vIKhJBQUEADs=
'''
root = tk.Tk()
style = ttk.Style()
borderImage = tk.PhotoImage("borderImage", data=borderImageData)
focusBorderImage = tk.PhotoImage("focusBorderImage", data=focusBorderImageData)
style.element_create("RoundedFrame",
"image", borderImage,
("focus", focusBorderImage),
border=16, sticky="nsew")
style.layout("RoundedFrame",
[("RoundedFrame", {"sticky": "nsew"})])
frame1 = ttk.Frame(style="RoundedFrame", padding=10)
text1 = tk.Text(frame1, borderwidth=0, highlightthickness=0, wrap="word",
width=40, height=4)
text1.pack(fill="both", expand=True)
text1.bind("<FocusIn>", lambda event: frame1.state(["focus"]))
text1.bind("<FocusOut>", lambda event: frame1.state(["!focus"]))
text1.insert("end", "This widget has the focus")
frame2 = ttk.Frame(style="RoundedFrame", padding=10)
text2 = tk.Text(frame2, borderwidth=0, highlightthickness=0, wrap="word",
width=40, height=4)
text2.pack(fill="both", expand=True)
text2.bind("<FocusIn>", lambda event: frame2.state(["focus"]))
text2.bind("<FocusOut>", lambda event: frame2.state(["!focus"]))
text2.insert("end", "This widget does not have the focus")
root.configure(background="white")
frame1.pack(side="top", fill="both", expand=True, padx=20, pady=20)
frame2.pack(side="top", fill="both", expand=True, padx=20, pady=20)
frame1.focus_set()
root.mainloop()
1The image data is a base64-encoded gif. If you decode the data and save it to a .gif file, you can edit it to use any colors you want.
Your canvas simply isn't large enough. Make it larger using
canvas = tkinter.Canvas(root, width=500, height=500)
Or make it resize with the root window using
canvas = tkinter.Canvas(root)
canvas.grid(row=0,column=0, sticky='NSEW')
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
I ran into this issue as well except for the main window. I was making a sticky notes app for someone and they wanted the window's corners to be rounded.
I had 4 canvases in the 4 corners with the image of an arc based on the corners. Then I made the color of those images transparent and it worked surprisingly well so I wanted to share it.
Here is the code:
from tkinter import *
from PIL import Image,ImageTk
def drag(event,win):
x = win.winfo_pointerx() - win.offsetx
y = win.winfo_pointery() - win.offsety
win.geometry('+{x}+{y}'.format(x=x,y=y))
def click(event,win):
win.offsetx = event.x
win.offsety = event.y
root = Tk()
root.resizable(False,False)
root.geometry("610x260")
img = ImageTk.PhotoImage( Image.open("src/ARC.png").resize((150,150)), Image.ANTIALIAS )
img2 = ImageTk.PhotoImage( Image.open("src/ARC 2.png").resize((150,150)), Image.ANTIALIAS )
img3 = ImageTk.PhotoImage( Image.open("src/ARC 3.png").resize((150,150)), Image.ANTIALIAS )
img4 = ImageTk.PhotoImage( Image.open("src/ARC 4.png").resize((150,150)), Image.ANTIALIAS )
fr = Frame(root, bg = "yellow", bd = 0)
fr.pack( expand = True, fill = 'both')
canvas = Canvas(fr, bg = "yellow", width = 60, height = 60, bd=0, highlightthickness=0, relief='ridge')
canvas.create_image(-2,-2, image = img, anchor = "nw")
canvas2 = Canvas(fr, bg = "yellow", width = 60, height = 60, bd=0, highlightthickness=0, relief='ridge')
canvas2.create_image(62,-2, image = img2, anchor = "ne")
canvas3 = Canvas(fr, bg = "yellow", width = 60, height = 60, bd=0, highlightthickness=0, relief='ridge')
canvas3.create_image(-2,62, image = img3, anchor = "sw")
canvas4 = Canvas(fr, bg = "yellow", width = 60, height = 60, bd=0, highlightthickness=0, relief='ridge')
canvas4.create_image(62,62, image = img4, anchor = "se")
Grid.columnconfigure(fr, 0, weight = 1)
Grid.columnconfigure(fr, 1, weight = 1)
Grid.rowconfigure(fr, 1, weight = 1)
canvas.grid( row = 0, column = 0, sticky = 'NW' )
canvas2.grid( row = 0, column = 1, sticky = 'NE' )
canvas3.grid(row = 1, column = 0, sticky = 'SW' )
canvas4.grid(row = 1, column = 1, sticky = 'SE' )
root.overrideredirect(True)
root.wm_attributes('-transparentcolor', "#222222")
root.bind('<Button-1>', lambda e, root = root: click(e,root))
root.bind('<B1-Motion>', lambda e, root = root: drag(e,root))
root.mainloop()
Now you'd probably change the rows and columns of the canvases based on what you want inside the window but this is a good start.

size of a background image in tkinter

I need to resize my window(tkinter) according to the width and height of my background image.
My code
from tkinter import *
from PIL import ImageTk
import cv2
image=cv2.imread("New_refImg.png")
width_1, height_1,channels = image.shape
canvas = Canvas(width = width_1, height = height_1, bg = 'blue')
canvas.pack(expand = YES, fill = BOTH)
img = ImageTk.PhotoImage(file = "New_refImg.png")
canvas.create_image(10, 10, image = img, anchor = NW)
mainloop()
I'm using a simple method, I call the same image twice : image=cv2.imread("New_refImg.png") and img = ImageTk.PhotoImage(file = "New_refImg.png") but is there a way change this line img = ImageTk.PhotoImage(file = "New_refImg.png") to something like that img = ImageTk.PhotoImage(image) (image already has been called in the 3rd line of the code)
Thank you
I don't know about PIL, but I can show you how to do it in tkinter:
from tkinter import *
root = Tk() # Create Tk before you can create an image
img = PhotoImage(file='pilner.png')
w, h = img.width(), img.height()
canvas = Canvas(root, width=w, height=h, bg='blue', highlightthickness=0)
canvas.pack(expand = YES, fill = BOTH)
canvas.create_image(0, 0, image=img, anchor=NW)
root.mainloop()
highlightthickness=0 rmoves the highlight border on the canvas. I'm positioning it at 0,0 so as to not show the bg.

How can I add text and image inside a frame in Python using tkinter

I am writing my first programming codes. I want to know if I can add text and image label inside a frame. I created a canvas and added two frames to it and then tried to add image and text file (to be displayed at the top of the canvas) but the text and picture do not show. When I run the program without the frames, it does show. Here is the code:
#
from tkinter import *
from tkinter import ttk
root = Tk()
root.title ('iMedic')
canvas = Canvas(root, width = 1600, height = 800)
panewindow = ttk.Panedwindow(canvas, orient = VERTICAL)
panewindow.pack(fill = BOTH, expand = True)
paitents_frame = ttk.Frame(panewindow, width = 1600, height = 400, relief = RAISED)
prescription_frame = ttk.Frame(panewindow, width = 1600, height = 300, relief = RAISED)
panewindow.add(paitents_frame, weight = 1)
panewindow.add(prescription_frame, weight = 1)
canvas.grid(row = 0, column = 0)
photo = PhotoImage(file = './logo.gif')
canvas.create_image(55, 55, image=photo)
canvas.create_text(600, 155, text = 'Welcome', font = ('Helvetica', 72, 'bold'), justify = 'center', fill='blue')
canvas.update
root.mainloop()
#
Is there a way I can fix this? I would assume another way would be to have the pic and text on top and then add frames below it but I don't know how to do it. Thanks!
It's not clear to me why you're adding frames to a canvas but going by the later statement;
I would assume another way would be to have the pic and text on top and then add frames below it but I don't know how to do it.
Here is how you could do it:
Make the panewindow child of root instead of child of canvas
The frames resize to fit their content so I added two labels in each
to make them visible, you should replace those labels with whichever
widgets you need.
I used pack for all the widget placements but you can replace them
with grid and provide the appropriate row and column values.
**
from tkinter import *
from tkinter import ttk
root = Tk()
root.title ('iMedic')
canvas = Canvas(root, width = 1600, height = 250)
canvas.pack(fill = BOTH, expand = True)
photo = PhotoImage(file = './logo.gif')
canvas.create_image(55, 55, image=photo)
canvas.create_text(600, 155, text = 'Welcome', font = ('Helvetica', 72, 'bold'), justify = 'center', fill='blue')
canvas.update
# Make panewindow child of root
panewindow = ttk.Panedwindow(root, orient = VERTICAL)
panewindow.pack(fill = BOTH, expand = True)
# paitents_frame with Labels in it
paitents_frame = ttk.Frame(panewindow, width = 1600, height = 400, relief = RAISED)
paitents_label1 = Label(paitents_frame, text="Name Label")
paitents_label1.pack()
paitents_label2 = Label(paitents_frame, text="Name Label")
paitents_label2.pack()
# prescription_frame with Labels in it
prescription_frame = ttk.Frame(panewindow, width = 1600, height = 300, relief = RAISED)
prescription_label1 = Label(prescription_frame, text="Prescription Text")
prescription_label1.pack()
prescription_label2 = Label(prescription_frame, text="Prescription Text")
prescription_label2.pack()
# Add the frames to panewindow
panewindow.add(paitents_frame, weight = 1)
panewindow.add(prescription_frame, weight = 1)
root.mainloop()
Another option is to leave out the canvas entirely and use labels to place image and text inside a frame. See this post on how to use image in labels
This code demonstrates how to place any text on any image in a Frame.
It works by creating a Label inside the Frame to hold your image and text.
Label requires the compound attribute to be set to either bottom, center, left, right, top or none
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title ('iMedic')
photo = tk.PhotoImage(file = "./logo.gif")
frame = ttk.Frame(root, relief = tk.RAISED)
frame.grid(row = 0, column = 0, sticky = tk.NSEW)
label = tk.Label(
frame, image=photo, compound = tk.CENTER,
font = "Helvetica 40 bold",
foreground = "yellow", text = "Welcome")
label.grid(row = 0, column = 0, sticky = tk.NSEW)
root.mainloop()

Tkinter Label: Retain Height when I change font to smaller size

I have an Label for a quite advanced calculator app.
I'd like to be able to change the font size when the text entered gets too long, to be able to enter more, but retain the height of the widget just for good looks.
Here's piece of code I tried:
self.biglabelfont = font.Font(family = "Arial", size = 24, weight = "bold")
self.entrylabel = Label(... font = self.biglabelfont ....)
self.entrylabel.pack(side = LEFT, fill = "x", expand = True)
self.entrylabel.propagate(0)
self.biglabelfont = font.Font(family = "Arial", size = 11, weight = "bold")
self.entrylabel.config(font = self.biglabelfont)
But it keeps shrinking the height.
Your choice to use pack_propagate is good, but you're doing it wrong. You must call it on the parent of the label widget, not on the label widget.
Here's a working example. Notice that the height of the sunken area doesn't change as the font grows and shrinks.
import Tkinter as tk
import tkFont
BG = "white"
HEIGHT = 75
def main():
global the_font, root
root = tk.Tk()
the_font = tkFont.Font(family="helvetica", size=18)
toolbar = make_toolbar(root)
subframe = make_subframe(root)
toolbar.pack(side="top", fill="x")
subframe.pack(side="top", fill="x")
root.geometry("400x400")
root.mainloop()
def make_subframe(parent):
frame = tk.Frame(parent, height=HEIGHT, borderwidth=1, relief="sunken", background=BG)
frame.pack_propagate(False)
label = tk.Label(frame, font=the_font, text="0123455.67890", background=BG)
label.pack(side="top", fill="both", expand=True)
return frame
def font_plus():
size = the_font.cget("size")
size += 2
the_font.configure(size=size)
def font_minus():
size = the_font.cget("size")
size -= 2
the_font.configure(size=size)
def make_toolbar(parent):
toolbar = tk.Frame(root)
grow = tk.Button(toolbar, text="Bigger", command=font_plus)
shrink = tk.Button(toolbar, text="Smaller", command=font_minus)
grow.pack(side="left")
shrink.pack(side="left")
return toolbar
main()

Resources