Python GUI Tk : Label
Label width and height
from Tkinter import * root = Tk() labelfont = ('times', 20, 'bold') widget = Label(root, text='Hello config world') widget.config(height=3, width=20) widget.pack(expand=YES, fill=BOTH) root.mainloop()
A label and a button
import sys, Tkinter Tkinter.Label(text="Welcome!").pack() Tkinter.Button(text="Exit", command=sys.exit).pack() Tkinter.mainloop()
Basic coding styles with labels
from Tkinter import Label widget = Label(None, text='Hello GUI world!') widget.pack() widget.mainloop()
Set side to top
from Tkinter import * root = Tk() Label(root, text='Hello GUI world!').pack(side=TOP) root.mainloop()
Add a label to a frame
from Tkinter import * root = Tk() root.title("Labeler") root.geometry("200x500") app = Frame(root) app.grid() lbl = Label(app, text = "I'm a label!") lbl.grid() root.mainloop()
Load image to label
from Tkinter import * root = Tk() #root.option_readfile('optionDB') root.title('Labels') Label(root, text="Text ", wraplength=300, justify=LEFT).pack(pady=10) f1=Frame(root) Label(f1, text="Text", relief=RAISED).pack(side=LEFT, padx=5) Label(f1, text="Text", relief=SUNKEN).pack(side=LEFT, padx=5) f1.pack() f2=Frame(root) for bitmap,rlf in [('woman', RAISED),('mensetmanus',SOLID),('terminal',SUNKEN), ('escherknot',FLAT),('calculator',GROOVE),('letters',RIDGE)]: Label(f2, bitmap='@bitmaps/%s' % bitmap, relief=rlf).pack(side=LEFT, padx=5) f2.pack() root.mainloop()
Label wraplength
from Tkinter import * root = Tk() #root.option_readfile('optionDB') root.title('Labels') Label(root, text="Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text ", wraplength=300, justify=LEFT).pack(pady=10) f1=Frame(root) Label(f1, text="Text", relief=RAISED).pack(side=LEFT, padx=5) Label(f1, text="Text", relief=SUNKEN).pack(side=LEFT, padx=5) f1.pack() f2=Frame(root) for bitmap,rlf in [('woman', RAISED),('mensetmanus',SOLID),('terminal',SUNKEN), ('escherknot',FLAT),('calculator',GROOVE),('letters',RIDGE)]: Label(f2, bitmap='@bitmaps/%s' % bitmap, relief=rlf).pack(side=LEFT, padx=5) f2.pack() root.mainloop()
Set text and font for Label
from Tkinter import * import math root = Tk() top = Frame(root) top.pack(side='top') hwframe = Frame(top) hwframe.pack(side='top', anchor='w') font = 'times 18 bold' hwtext = Label(hwframe, text='Hello, World!', font=font) hwtext.pack(side='top', pady=20) rframe = Frame(top) rframe.pack(side='top', padx=10, pady=20, anchor='w') r_label = Label(rframe, text='The sine of') r_label.pack(side='left') r = StringVar() r.set('1.2') r_entry = Entry(rframe, width=6, textvariable=r) r_entry.pack(side='left') s = StringVar() def comp_s(event): global s s.set('%g' % math.sin(float(r.get()))) # construct string r_entry.bind('<Return>', comp_s) compute = Label(rframe, text=' equals ') compute.pack(side='left') s_label = Label(rframe, textvariable=s, width=12) s_label.pack(side='left') def quit(event=None): root.destroy() quit_button = Button(top, text='Goodbye, GUI World!', command=quit, background='yellow', foreground='blue') quit_button.pack(side='top', pady=5, ipadx=30, ipady=30, anchor='w') root.bind('<q>', quit) root.mainloop()
Label style
from Tkinter import * class GUI: def __init__(self): self.root = Tk() self.root.title('GUI Design 9') self.base = 'gray60' self.fout=Frame(self.root, borderwidth=0) self.f1=Frame(self.fout, borderwidth=2, relief=RAISED) Label(self.f1, text='RAISED', width=10).pack(side=LEFT) self.f1.pack(pady=5) self.f2=Frame(self.fout, borderwidth=2, relief=SUNKEN) Label(self.f2, text='SUNKEN', width=10).pack(side=LEFT) self.f2.pack(pady=5) self.f3=Frame(self.fout, borderwidth=2, relief=FLAT) Label(self.f3, text='FLAT', width=10).pack(side=LEFT) self.f3.pack(pady=5) self.f4=Frame(self.fout, borderwidth=3, relief=RIDGE) Label(self.f4, text='RIDGE', width=10).pack(side=LEFT) self.f4.pack(pady=5) self.f5=Frame(self.fout, borderwidth=2, relief=GROOVE) Label(self.f5, text='GROOVE', width=10).pack(side=LEFT) self.f5.pack(pady=5) self.f6=Frame(self.fout, borderwidth=2, relief=SOLID) Label(self.f6, text='SOLID', width=10).pack(side=LEFT) self.f6.pack(pady=5) self.fout.pack() myGUI = GUI() myGUI.root.mainloop()
Label on frame
from Tkinter import * root = Tk() #root.option_readfile('optionDB') root.title('Canvas') canvas = Canvas(root, width =400, height=400) img = PhotoImage(file='./logo.gif') frm = Frame(canvas, relief=GROOVE, borderwidth=2) Label(frm, text="Label").pack() canvas.create_window(285, 280, window=frm, anchor=CENTER) canvas.pack() root.mainloop()
Label border: RAISED, SUNKEN, FLAT, RIDGE, GROOVE, SOLID
from Tkinter import * root = Tk() root.title('Frames') for relief in [RAISED, SUNKEN, FLAT, RIDGE, GROOVE, SOLID]: f = Frame(root, borderwidth=2, relief=relief) Label(f, text=relief, width=10).pack(side=LEFT) f.pack(side=LEFT, padx=5, pady=5) root.mainloop()
Our First Tkinter Program: a label and a window
from Tkinter import * root = Tk() w = Label(root, text="Hello, world!") w.pack() root.mainloop()
Simplest Label
from Tkinter import * root = Tk() labelfont = ('times', 20, 'bold') # family, size, style widget = Label(root, text='Hello config world') widget.pack(expand=YES, fill=BOTH) root.mainloop()
Label background and foreground
from Tkinter import * root = Tk() labelfont = ('times', 20, 'bold') widget = Label(root, text='Hello config world') widget.config(bg='black', fg='yellow') widget.pack(expand=YES, fill=BOTH) root.mainloop()
StringVar and Label variable
from Tkinter import * import math root = Tk() top = Frame(root) top.pack(side='top') hwframe = Frame(top) hwframe.pack(side='top') hwtext = Label(hwframe, text='Hello, World!') hwtext.pack(side='top') rframe = Frame(top) rframe.pack(side='top') r_label = Label(rframe, text='The sine of') r_label.pack(side='left') r = StringVar() r.set('1.2') r_entry = Entry(rframe, width=6, textvariable=r) r_entry.pack(side='left') s = StringVar() def comp_s(event): global s s.set('%g' % math.sin(float(r.get()))) # construct string r_entry.bind('<Return>', comp_s) compute = Label(rframe, text=' equals ') compute.pack(side='left') s_label = Label(rframe, textvariable=s, width=12) s_label.pack(side='left') def quit(event=None): root.destroy() quit_button = Button(top, text='Goodbye, GUI World!', command=quit) quit_button.pack(side='top') root.bind('<q>', quit) root.mainloop()
Bind variable to Label
#!/usr/bin/env python from Tkinter import * import math root = Tk() top = Frame(root) top.pack(side='top') hwtext = Label(top, text='Hello, World! The sine of') hwtext.pack(side='top') r = StringVar() r.set('1.2') r_entry = Entry(top, width=6, textvariable=r) r_entry.pack(side='top') s = StringVar() def comp_s(event): global s s.set('%g' % math.sin(float(r.get()))) # construct string r_entry.bind('<Return>', comp_s) compute = Label(top, text=' equals ') compute.pack(side='top') s_label = Label(top, textvariable=s, width=18) s_label.pack(side='top') import tkMessageBox def quit(event): if tkMessageBox.askokcancel('Quit','Do you really want to quit?'): root.destroy() root.bind('<q>', quit) root.mainloop()