TK之button

分为三种类型: Button,CheckButton,RadioButton.

 

Button:The -text option lets the user know what the Button is for, and the -command option makes something happen when the user clicks the Button.

 

Checkbuttons:Using -variable gives you an easy way to find out whether the Checkbutton is checked

 

eg:$mw->Checkbutton(-text => 'Print Header', -variable => /$print_header);

 

 

When creating Radiobuttons, we always create more than one and use the -text , -variable , and -value options:

$group1 = 100; # set default value

foreach (qw/1 10 100 10000 100000 1000000/) {

$mw->Radiobutton(-text => '$' . $_, -variable => /$group1,

-value => $_)->pack(-side => 'left');

}



print "User selected: $group1";






Option

Button

Checkbutton

Radiobutton

-activebackground => color
Sets the color the background should be when the mouse cursor is over the Button. A color is a text string such as "red" .

Figure 4-1

Figure 4-1

Figure 4-1

-activeforeground => color
Sets the color the text should be when the mouse cursor is over the Button.

Figure 4-1

Figure 4-1

Figure 4-1

-anchor => 'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w' | 'nw' | 'center'
Causes the text to stick to the specified position in the Button.

Figure 4-1

Figure 4-1

Figure 4-1

-background => color
Sets the background of the Button to color .

Figure 4-1

Figure 4-1

Figure 4-1

-bitmap => 'bitmapname'
Sets default bitmap or the location of a bitmap file (with @ in front of path). See Chapter 17, "Images and Animations" for more details.

Figure 4-1

Figure 4-1

Figure 4-1

-borderwidth => amount
Changes the width of the edge drawn around the Button and the thickness of the indicator. Emphasizes the -relief of the Button.

Figure 4-1

Figure 4-1

Figure 4-1

-command => callback
Indicates a pointer to a function that will be called when the Button is pressed.

Figure 4-1

Figure 4-1

Figure 4-1

-cursor => 'cursorname'
Indicates that the mouse cursor will change to 'cursorname' when over the Button. See Chapter 23, "Plethora of pTk Potpourri" for details.

Figure 4-1

Figure 4-1

Figure 4-1

-disabledforeground => color
Sets the color the text should be when the Button is disabled ( -state will be 'disabled' ).

Figure 4-1

Figure 4-1

Figure 4-1

-font => 'fontname'
Changes the font of all text on the Button.

Figure 4-1

Figure 4-1

Figure 4-1

-foreground => color
Changes the text color to color .

Figure 4-1

Figure 4-1

Figure 4-1

-height => amount
Sets the height of the Button in characters if text is displayed and the screen distance if an image or bitmap is displayed.

Figure 4-1

Figure 4-1

Figure 4-1

-highlightbackground => color
Sets the color of the area behind the focus rectangle (shows when widget does not have focus).

Figure 4-1

Figure 4-1

Figure 4-1

-highlightcolor => color
Sets the color of the focus rectangle (shows when widget has focus).

Figure 4-1

Figure 4-1

Figure 4-1

-highlightthickness => amount
Sets the thickness of the highlight rectangle around the Button; indicates focus.

Figure 4-1

Figure 4-1

Figure 4-1

-image => $imgptr
$imgptr is a pointer to an Image object made with any supported image format. See Chapter 17, "Images and Animations" for details.

Figure 4-1

Figure 4-1

Figure 4-1

-indicatoron => 0 | 1
Determines whether to display the indicator.
 

Figure 4-1

Figure 4-1

-justify => 'left' | 'right' | 'center'
Sets the direction against which multiline text will justify.

Figure 4-1

Figure 4-1

Figure 4-1

-offvalue => newvalue
Sets the value used when the Button is off. Must be a scalar. Default is 0.
 

Figure 4-1

 
-onvalue => newvalue
Sets the value used when the Button is on. Must be a scalar. Default is 1.
 

Figure 4-1

 
-padx => amount
Adds extra space to the left and right side of the Button inside the Button edge.

Figure 4-1

Figure 4-1

Figure 4-1

-pady => amount
Adds extra space to the top and bottom of the Button inside the Button edge.

Figure 4-1

Figure 4-1

Figure 4-1

-relief =>'flat'|'groove'|'raised'|'ridge'|'sunken'|'solid'
Changes the type of edges drawn around the Button. Default for Checkbutton and Radiobutton is 'flat' .

Figure 4-1

Figure 4-1

Figure 4-1

-selectcolor => color
Sets the color of the indicator when on.
 

Figure 4-1

Figure 4-1

-selectimage => imgptr
Indicates the image to display instead of text when Button is on. Ignored if -image is not used.
 

Figure 4-1

Figure 4-1

-state => 'normal' | 'disabled' | 'active'
Indicates the Button's state of responsiveness. If set to 'disabled' , the Button does not respond.

Figure 4-1

Figure 4-1

Figure 4-1

-takefocus => 0 | 1 | undef
Indicates that the Button will never get focus (0), always get focus (1), or let the application decide ( undef ).

Figure 4-1

Figure 4-1

Figure 4-1

-text => 'text'
Sets the text string displayed on the Button.

Figure 4-1

Figure 4-1

Figure 4-1

-textvariable => /$variable
Points to a variable containing text to be displayed in Button. Button text will change as $variable does.

Figure 4-1

Figure 4-1

Figure 4-1

-underline => n
Underlines the n th character in the text string. Allows keyboard input via that character when Button has the focus. It's important to note that the character is only underlined; any behavior must be supplied by the programmer via a bind command. See Chapter 15, "Anatomy of the MainLoop" for binding details.

Figure 4-1

Figure 4-1

Figure 4-1

-value => newvalue
Sets the value assigned to $variable (with -variable option) when this Radiobutton is selected. Default is 1.
   

Figure 4-1

-variable => /$value
Associates the on/off values with $variable .
 

Figure 4-1

Figure 4-1

-width => amount
Sets the width of the Button in characters if text is displayed and as a screen distance if an image or bitmap is displayed.

Figure 4-1

Figure 4-1

Figure 4-1

-wraplength => amount
Sets the screen distance for the maximum amount of text displayed on one line. The default is 0, which means that text is not wrapped at word boundaries, only at line breaks (newlines).

Figure 4-1

Figure 4-1

 


如何在button中添加图片:



$image = $mw->Photo(-file => "bigredbutton.gif");

$mw->Button(-text => 'Exit', -command => sub { exit },

-image => $image)->pack;





With Radiobuttons, there is no default value, and you need to explicitly assign

$value
with the -value
option


修改button的风格:
flat
No edges are drawn around the Button at all. Makes it look like only text is present in the window.
groove
Gives a slightly depressed look to the edge (as if there were a ditch around the text).
raised
Gives a 3D look with a shadow on the lower and right sides of the Button, which causes it to look higher than the window surface. This is the default.
ridge
Makes it look like a ridge is around the text. The opposite of 'groove' .
solid
Draws a solid line around the widget.
sunken
Gives the 3D effect of being below the surface of the window. The opposite of 'raised'
 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

惹不起的程咬金

来都来了,不赏点银子么

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值