Tkinter按钮(Button)

Python - Tkinter Button按钮组件是用来添加一个Python应用程序中的按钮。这些按钮可以显示文字或图像,表达按钮的目的。当你按一下按钮时,您可以附加到一个按钮的函数或方法,该方法自动调用。

按钮组件是用来添加一个Python应用程序中的按钮。这些按钮可以显示文字或图像,表达按钮的目的。当你按一下按钮时,您可以附加到一个按钮的函数或方法,该方法自动调用.

语法:

这里是一个简单的语法来创建这个widget:

w = Button ( master, option=value, ... )

参数:

  • master: 这代表了父窗口.

  • options: 下面是这个小工具最常用的选项列表。这些选项可以作为键 - 值对以逗号分隔.

OptionDescription
activebackgroundBackground color when the button is under the cursor.
activeforegroundForeground color when the button is under the cursor.
bdBorder width in pixels. Default is 2.
bgNormal background color.
commandFunction or method to be called when the button is clicked.
fgNormal foreground (text) color.
fontText font to be used for the button's label.
heightHeight of the button in text lines (for textual buttons) or pixels (for images).
highlightcolorThe color of the focus highlight when the widget has focus.
imageImage to be displayed on the button (instead of text).
justifyHow to show multiple text lines: LEFT to left-justify each line; CENTER to center them; or RIGHT to right-justify.
padxAdditional padding left and right of the text.
padyAdditional padding above and below the text.
reliefRelief specifies the type of the border. Some of the values are SUNKEN, RAISED, GROOVE, and RIDGE.
stateSet this option to DISABLED to gray out the button and make it unresponsive. Has the value ACTIVE when the mouse is over it. Default is NORMAL.
underlineDefault is -1, meaning that no character of the text on the button will be underlined. If nonnegative, the corresponding text character will be underlined.
widthWidth of the button in letters (if displaying text) or pixels (if displaying an image).
wraplengthIf this value is set to a positive number, the text lines will be wrapped to fit within this length.

 

方法:

以下是这个小工具的常用方法:

MedthodDescription
flash()Causes the button to flash several times between active and normal colors. Leaves the button in the state it was in originally. Ignored if the button is disabled.
invoke()Calls the button's callback, and returns what that function returns. Has no effect if the button is disabled or there is no callback.

例子:

自行尝试下面的例子:

import Tkinter
import tkMessageBox

top = Tkinter.Tk()

def helloCallBack():
   tkMessageBox.showinfo( "Hello Python", "Hello World")

B = Tkinter.Button(top, text ="Hello,Python!少壮不努力,老大学编程.-易百在线教程 - www.yiibai.com", command = helloCallBack)

B.pack()
top.mainloop()

这将产生以下结果:

 

转载于:https://www.cnblogs.com/tkinter/p/5628801.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值