tkinter学习三

顶层窗口协议
from
tkinter import * root = Tk() # explicit root trees = [('The Larch!', 'light blue'), ('The Pine!', 'light green'), ('The Giant Redwood!', 'red')] for (tree, color) in trees: win = Toplevel(root) # new window win.title('Sing...') # set border 改变标题 win.protocol('WM_DELETE_WINDOW', lambda:None) # ignore close win.iconbitmap('py-blue-trans-out.ico') # not red Tk 改变图标 msg = Button(win, text=tree, command=win.destroy) # kills one win 点击可以关闭一个窗口 msg.pack(expand=YES, fill=BOTH) msg.config(padx=10, pady=10, bd=10, relief=RAISED) msg.config(bg='black', fg=color, font=('times', 30, 'bold italic')) root.title('Lumberjack demo') Label(root, text='Main window', width=30).pack() Button(root, text='Quit All', command=root.quit).pack() # kills all app 点击可以关闭所有窗口 root.mainloop()

tkinter学习三_第1张图片

配置组件外观(Configuring Widget Appearance)

from tkinter import *
root = Tk()
labelfont = ('times', 20, 'bold') # family, size, style  字体系列、大小、类型
widget = Label(root, text='Hello config world')
widget.config(bg='black', fg='yellow') # yellow text on black label 在黑色标签上显示黄色文本
widget.config(font=labelfont) # use a larger font  使用更大的字体
widget.config(height=3, width=20) # initial size: lines,chars 初始化大小、行间距、字符间距
widget.pack(expand=YES, fill=BOTH)   
root.mainloop()

小知识:

边框和浮凸:

一个“bd=N”组件选项可用于设置边框宽度,而relief=S选项能够制定一种边框类型,S可以是FLAT(扁平)、SUNKEN(凹陷)、GROOVE(凹槽)、SOLID(加粗)或RIDGE(脊状)

光标:

cursor选项能够改变当鼠标经过组件时的鼠标外观,例如:state=DISABLED(禁止)选项一般会在屏幕上禁用组件功能(禁用时,组件变成灰色),NORMAL(正常)状态就不会禁用组件功能,有些主件也支持READONLY(只读)属性,设置为该属性的组件能够正常显示,但是不能够进行修改。

留白:

很多主件(如按钮、标签和文本)周围都可以添加额外的空间,可通过padx=N和pady=N选项实现。

例子:

from tkinter import *
widget = Button(text='Spam', padx=10, pady=10)
widget.pack(padx=20, pady=20)
widget.config(cursor='gumby')
widget.config(bd=8, relief=RAISED)
widget.config(bg='dark green', fg='white')
widget.config(font=('helvetica', 20, 'underline italic'))
mainloop()

顶层窗口,通过Toplevel主件,tkinter可以创建任意数量的独立窗口。

import sys
from tkinter import Toplevel, Button, Label
win1 = Toplevel() # two independent windows
win2 = Toplevel() # but part of same process
Button(win1, text='Spam', command=sys.exit).pack()
Button(win2, text='SPAM', command=sys.exit).pack()
Label(text='Popups').pack() # on default Tk() root window 根窗口
win1.mainloop()
Label(text='Popups').pack() # on default Tk() root window
#默认根窗口
root = Tk()
Label(root, text='Popups').pack() # on explicit Tk() root window
root.mainloop()
#显示根窗口
import tkinter
from tkinter import Tk, Button
tkinter.NoDefaultRoot()
win1 = Tk() # two independent root windows
win2 = Tk()
Button(win1, text='Spam', command=win1.destroy).pack()
Button(win2, text='SPAM', command=win2.destroy).pack() 
win1.mainloop()

 

from tkinter import *
from tkinter.messagebox import *
def callback():
    if askyesno('Verify', 'Do you really want to quit?'):
        showwarning('Yes', 'Quit not yet implemented')
    else:
        showinfo('No', 'Quit has been cancelled')
errmsg = 'Sorry, no Spam allowed!'
Button(text='Quit', command=callback).pack(fill=X)
Button(text='Spam', command=(lambda: showerror('Spam', errmsg))).pack(fill=X)
mainloop()

tkinter学习三_第2张图片 tkinter学习三_第3张图片 

你可能感兴趣的:(tkinter学习三)