Tkinter 中插件的共有方法

Reference

就像插件的属性一样,因为这些插件都直接或间接继承了Widget类或者BaseWidget类,所以他们都有一些共同的方法。

所有组件的共有方法, 包括窗口

1. 配置Configuration

# grammar
# 1. 配置属性
# 等同于 w['option'] = value, 或在创建时设置
w.config(option=value)
# 2. 获取属性值
# 等同于,value = w['option']
value = w.cget("option")
# 3. 查看插件有哪些属性(key)
k = w.keys()
# 4. 查看当前的所有配置
print(w.config())

# example
import tkinter as tk
root = tk.Tk()
btn = tk.Button(root, text='click')
btn.config(bg='blue', fg='black')
btn.pack(side=tk.LEFT)

print(btn.cget('bg'), btn.cget('text'), btn['fg'])
print(btn.keys())
print(btn.config())
tk.mainloop()

2. 事件处理相关 Event processing

mainloop()
w.mainloop()
w.quit()
w.wait_variable(var)
w.wait_visibility(window)
w.wait_window(window)
w.update()
w.update_idletasks()

3. 事件回调函数相关Event callbacks

w.bind(event, callback)
w.unbind(event)
w.bind_class(event, callback)
w.bindtags()
w.bindtags(tags)

4. Alarm handlers and other non-event callbacks

id = w.after(time, callback)
id = w.after_idle(callback)
w.after_cancel(id)

5. 窗口管理Window management

w.lift(aboveThis=None)
w.lower(belowThis=None)
w.winfo_width()
# Get the width of this widget, in pixels. 
w.winfo_height() 
# Get the height of this widget, in pixels. 
w.winfo_reqwidth()
# Returns the “natural” width for this widget. The natural size is the minimal size needed to display the widget’s contents, including padding, borders, etc. 
w.winfo_reqheight()
w.winfo_id()
# Get a system-specific window identifier for this widget. 

窗口组件的共有方法

Wm_XXX Functions

wm_aspect(minNumer=None, minDenom=None, maxNumer=None, maxDenom=None)
Controls the aspect ratio. See aspect for details.

wm_attributes(*args)
Sets or gets window attributes. See attributes for details.

wm_client(name=None)
Sets or gets the WM_CLIENT_MACHINE property. See client for details.

wm_colormapwindows(*wlist)
Sets or gets the WM_COLORMAP_WINDOWS property. See colormapwindows for details.

wm_command(value=None)
Sets or gets the WM_COMMAND property. See command for details.

wm_deiconify()
Displays the window. See deiconify for details.

wm_focusmodel(model=None)
Sets or gets the focus model. See focusmodel for details.

wm_frame()
Returns a window identifier corresponding for the windows outermost parent. See frame for details.

wm_geometry(newGeometry=None)
Sets or gets the window geometry. See geometry for details.

wm_grid(baseWidth=None, baseHeight=None, widthInc=None, heightInc=None)
See grid for details.

wm_group(pathName=None)
Adds the window to a window group. See group for details.

wm_iconbitmap(bitmap=None)
Sets or gets the icon bitmap. See iconbitmap for details.

wm_iconify()
Turns the window into an icon. See iconify for details.

wm_iconmask(bitmap=None)
Sets or gets the icon bitmap mask. See iconmask for details.

wm_iconname(newName=None)
Sets or gets the icon name. See iconname for details.

wm_iconposition(x=None, y=None)
Sets or gets the icon position hint. See iconposition for details.

wm_iconwindow(pathName=None)
Sets or gets the icon window. See iconwindow for details.

wm_maxsize(width=None, height=None)
Sets or gets the maximum size. See maxsize for details.

wm_minsize(width=None, height=None)
Sets or gets the minimum size. See minsize for details.

wm_overrideredirect(boolean=None)
Sets or gets the override redirect flag. See overrideredirect for details.

wm_positionfrom(who=None)
See positionfrom for details.

wm_protocol(name=None, func=None)
Registers a callback function for the given protocol. See protocol for details.

wm_resizable(width=None, height=None)
Sets or gets the resize flags. See resizable for details.

wm_sizefrom(who=None)
See sizefrom for details.

wm_state(newstate=None)
Sets or gets the window state. See state for details.

wm_title(string=None)
Sets or gets the window title. See title for details.

wm_transient(master=None)
Makes window a transient window for a given master. See transient for details.

wm_withdraw()
Removes the window from the screen. See withdraw for details.
Table of Contents