Python GUI Tk : Frame
Create additional windows: use the Toplevel widget.
from Tkinter import *
root = Tk()
top = Toplevel()
root.mainloop()Change Frame title
import Tkinter root = Tkinter.Tk() var = Tkinter.StringVar() entry = Tkinter.Entry(root, textvariable=var) entry.focus_set() entry.pack() var.set(root.title()) def changeTitle(): root.title(var.get()) Tkinter.Button(root, text="Change Title", command=changeTitle).pack() Tkinter.mainloop()
Frame action: Iconify
import Tkinter root = Tkinter.Tk() var = Tkinter.StringVar() entry = Tkinter.Entry(root, textvariable=var) entry.focus_set() entry.pack() var.set(root.title()) def changeTitle(): root.title(var.get()) Tkinter.Button(root, text="Iconify", command=root.iconify).pack() Tkinter.mainloop()
Frame action: close
import Tkinter root = Tkinter.Tk() var = Tkinter.StringVar() entry = Tkinter.Entry(root, textvariable=var) entry.focus_set() entry.pack() var.set(root.title()) def changeTitle(): root.title(var.get()) Tkinter.Button(root, text="Close", command=root.destroy).pack() Tkinter.mainloop()
Creating a window
from Tkinter import * # create the root window root = Tk() # modify the window root.title("Create a window") root.geometry("200x200") # Start the window's event-loop root.mainloop()
Default top level root window
from Tkinter import * root = Tk() root.title('Toplevel') Label(root, text='This is the main (default) Toplevel').pack(pady=10) root.mainloop()
A child of root window
from Tkinter import * root = Tk() t1 = Toplevel(root) Label(t1, text='This is a child of root').pack(padx=10, pady=10) root.mainloop()
Transient window of root
from Tkinter import * root = Tk() t2 = Toplevel(root) Label(t2, text='This is a transient window of root').pack(padx=10, pady=10) t2.transient(root) root.mainloop()
Frame without decorations
from Tkinter import * root = Tk() t3 = Toplevel(root, borderwidth=5, bg='blue') Label(t3, text='No wm decorations', bg='blue', fg='white').pack(padx=10, pady=10) t3.overrideredirect(1) t3.geometry('200x70+150+150') root.mainloop()
Using Tk option database to configure Tk widgets
title = 'Using Tk option database to configure Tk widgets' # Import Pmw from this directory tree. import sys sys.path[:0] = ['../../..'] import string import Tkinter import Pmw info = """ The Tk widgets contained in this simple megawidget have been configured using the Tk option database. *DemoClass*Listbox.cursor is 'heart' *DemoClass*Entry.cursor is 'hand1' *DemoClass*background is 'pink' *DemoClass*highlightBackground is 'green' *DemoClass*foreground is 'blue' """ class DemoClass(Pmw.MegaWidget): # Demo Pmw megawidget. def __init__(self, parent = None, **kw): # Define the megawidget options. optiondefs = () self.defineoptions(kw, optiondefs) # Initialise the base class (after defining the options). Pmw.MegaWidget.__init__(self, parent) interior = self.interior() listbox = Tkinter.Listbox(interior, height = 12, width = 40) listbox.pack(fill='both', expand='yes') for line in string.split(info, '\n'): listbox.insert('end', line) entry = Tkinter.Entry(interior) entry.pack(fill='y') entry.insert(0, 'Hello, World!') # Check keywords and initialise options. self.initialiseoptions() class Demo: def __init__(self, parent): # Test Tk option database settings. parent.option_add('*DemoClass*Listbox.cursor', 'heart') parent.option_add('*DemoClass*Entry.cursor', 'hand1') parent.option_add('*DemoClass*background', 'pink') parent.option_add('*DemoClass*highlightBackground', 'green') parent.option_add('*DemoClass*foreground', 'blue') # Create and pack the megawidget. demo = DemoClass(parent) demo.pack(fill = 'both', expand = 1) ###################################################################### # Create demo in root window for testing. if __name__ == '__main__': root = Tkinter.Tk() Pmw.initialise(root) root.title(title) exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy) exitButton.pack(side = 'bottom') widget = Demo(root)
root.mainloop()
Set Frame background color
from Tkinter import * class App: def __init__(self, root): fm = Frame(root, width=300, height=200, bg="blue") fm.pack(side=TOP, expand=NO, fill=NONE) root = Tk() display = App(root) root.mainloop()