Tkinter 1 应用窗口

Reference

1 Class BaseWidget (Mics)

参考详细介绍 link, 可以被tk的实例化对象直接调用 (因为tk类继承了mics)

简介

baseWidget,获取一些基本信息,比如,获取窗口相关的一些信息,销毁窗口,剪切板的操作,事件的操作,焦点的设置和信息获取。

获取窗口相关的信息

 winfo_atomwinfo_atomnamewinfo_cellswinfo_childrenwinfo_classwinfo_colormapfull,winfo_containingwinfo_depthwinfo_existswinfo_fpixelswinfo_geometrywinfo_heightwinfo_idwinfo_interpswinfo_ismappedwinfo_managerwinfo_namewinfo_parentwinfo_pathname,winfo_pixelswinfo_pointerxwinfo_pointerxywinfo_pointerywinfo_reqheightwinfo_reqwidthwinfo_rgbwinfo_rootxwinfo_rootywinfo_screenwinfo_screencellswinfo_screendepth,winfo_screenheightwinfo_screenmmheightwinfo_screenmmwidthwinfo_screenvisualwinfo_screenwidthwinfo_serverwinfo_toplevelwinfo_viewablewinfo_visualwinfo_visualid,winfo_visualsavailablewinfo_vrootheightwinfo_vrootwidthwinfo_vrootxwinfo_vrootywinfo_widthwinfo_xwinfo_y

比如获取窗口的大小,winfo_reqwidth(), winfo_reqwidth

获取屏幕大小,winfo_screenheight, winfo_screenheight()

事件相关

 event_addevent_deleteevent_generateevent_info

bindbind_allbind_classbindtags

focusfocus_displayoffocus_forcefocus_getfocus_lastforfocus_set

tk_focusFollowsMousetk_focusNexttk_focusPrev

2 窗口管理器(Class Wm)

参考详细介绍 link, 可以被tk的实例化对象直接调用 (因为tk类继承了wm类)

简介

Wm类包含一系列窗口管理相关的操作,比如设置窗口,位置,大小,是否可调设置窗口图标,标题和属性将函数和命令绑定,等等

设置窗口位置和大小

设置窗口图标

3 应用窗口之菜单栏

可以通过Menu或Menubutton组件进行创建 (Menubutton不推荐)。Menu主要通过add_commandadd_cascadeadd_separator函数来创建

from tkinter import *

def callback():
    print "called the callback!"

root = Tk()

# create a menu
menu = Menu(root)
root.config(menu=menu)

filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=callback)
filemenu.add_command(label="Open...", command=callback)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=callback)

helpmenu = Menu(menu)
menu.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="About...", command=callback)

mainloop()

4 应用窗口之工具栏

可以通过创建Frame组件,然后在Frame上放置一些Button组件来实现工具栏

from tkinter import *

root = Tk()

def callback():
    print "called the callback!"

# create a toolbar
toolbar = Frame(root)

b = Button(toolbar, text="new", width=6, command=callback)
b.pack(side=LEFT, padx=2, pady=2)

b = Button(toolbar, text="open", width=6, command=callback)
b.pack(side=LEFT, padx=2, pady=2)

toolbar.pack(side=TOP, fill=X)

mainloop()

5 应用窗口之状态栏

一般在应用程序的底部都会有状态栏,显示一些状态信息。它可以通过一下方式实现

status = Label(master, text=s "", bd=1, relief=SUNKEN, anchor=W)
status.pack(side=BOTTOM, fill=X)
from tkinter import *

class StatusBar(Frame):

    def __init__(self, master):
        Frame.__init__(self, master)
        self.label = Label(self, bd=1, relief=SUNKEN, anchor=W)
        self.label.pack(fill=X)

    def set(self, format, *args):
        self.label.config(text=format % args)
        self.label.update_idletasks()

    def clear(self):
        self.label.config(text="")
        self.label.update_idletasks()
        
root = Tk()
status = StatusBar(root)
status.pack(side=BOTTOM, fill=X)

菜单栏工具栏状态栏

# -*- coding:utf-8 -*-
'''
Python GUI with Tkinter-11-Adding the Status Bar
'''
from Tkinter import *
 
def doNothing():
    print("ok ok I won't...")
 
root = Tk()
 
# ***** Main Menu *****
menu = Menu(root)
root.config(menu=menu)
 
subMenu = Menu(menu)
menu.add_cascade(label="File", menu=subMenu)
subMenu.add_command(label="New Project...", command=doNothing)
subMenu.add_command(label="New...", command=doNothing)
subMenu.add_separator()
subMenu.add_command(label="Exit", command=doNothing)
 
editMenu = Menu(menu)
menu.add_cascade(label="Edit", menu=editMenu)
editMenu.add_command(label="Redo", command=doNothing)
 
# ***** Toolbar *****
 
toolbar = Frame(root, bg="blue")
 
insertBtn = Button(toolbar, text="Insert Image", command=doNothing)
insertBtn.pack(side=LEFT, padx=2, pady=2)
printBtn = Button(toolbar, text="Print", command=doNothing)
printBtn.pack(side=LEFT, padx=2, pady=2)
 
toolbar.pack(side=TOP, fill=X)
 
# ***** Status Bar *****
 
status = Label(root, text="Preparing to do nothing...", bd=1, relief=SUNKEN, anchor=W)
status.pack(side=BOTTOM, fill=X)
 
root.geometry('500x300')
root.mainloop()
Table of Contents