Tkinter 之Button标签

一、参数说明

语法 作用
Button(root,text='xxxx') 按钮图标显示内容
Button(root,text='xxxx',height=2) 组件的高度(所占行数)
Button(root,text='xxxx',width=20) 组件的宽度(所占字符个数)
Button(text='xxxx',fg='blue') 按钮字体颜色
Button(root,text='xxxx',activebackground='grey') 按钮按下时的前景字体颜色
Button(root,text=‘xxxxx’,justify=tk.LEFT) 多行文本的对齐方式
Button(root,text='xxxx',pady=10) 文本上下两侧的空格数
Button(root,text='xxxx',padx=10) 文本左右两侧的空格数(默认为1)
Button(root,text='xxxx',command=函数) 按钮触发执行的命令(函数)
Button(root,text='xxxx',state=tk.DISABLED) 按钮禁止点击
Button(root,text='xxxx',underline=1) 文字下划线(默认为-1)
Button(root,text='xxxx',author=tk.CENTER) 文字位置
Button(root,text='xxxx',textvariable=var) 动态设置文本

二、代码示例

import tkinter as tk

window = tk.Tk()

def main():
    global window
    # 设置主窗体大小
    winWidth = 600
    winHeight = 400
    # 获取屏幕分辨率
    screenWidth = window.winfo_screenwidth()
    screenHeight = window.winfo_screenheight()
    # 计算主窗口在屏幕上的坐标
    x = int((screenWidth - winWidth)/ 2)
    y = int((screenHeight - winHeight) / 2)
    
    # 设置主窗口标题
    window.title("Button参数说明")
    # 设置主窗口大小
    window.geometry("%sx%s+%s+%s" % (winWidth, winHeight, x, y))
    # 设置窗口宽高固定
    window.resizable(0,0)
    # 设置窗口图标
    window.iconbitmap("./image/icon.ico")
    
    """ button参数列表

        STANDARD OPTIONS

            activebackground, activeforeground, anchor,
            background, bitmap, borderwidth, cursor,
            disabledforeground, font, foreground
            highlightbackground, highlightcolor,
            highlightthickness, image, justify,
            padx, pady, relief, repeatdelay,
            repeatinterval, takefocus, text,
            textvariable, underline, wraplength

        WIDGET-SPECIFIC OPTIONS

            command, compound, default, height,
            overrelief, state, width
        """
    var = tk.StringVar()
    var.set("提交")
    tk.Button(window, text="提交", width=30, pady=5,fg="#f00", bg="#bbb",
              borderwidth=0, activeforeground="yellow", activebackground="#666"
              ,underline=-1, command=click, textvariable=var).pack()
    window.mainloop()

def click():
    print("click")

if __name__ == '__main__':
    main()

  

三、效果图

 Tkinter 之Button标签

 

 

 

 

上一篇:Tkinter 之Menu菜单标签


下一篇:tkFileDialog无法在Windows上将结果转换为Python列表