Tkinter Tutorial: button (2)

Source: Internet
Author: User
Tags tkinter tutorial

This article Reprinted from: http://blog.csdn.net/jcodeer/article/details/1811300

# Tkinter: button (2)

'''5. Specify the width and height of the button.
Width: width
Heigth: Height
Three methods are used:
1. Specify the width and height when creating the Button Object
2. Use the attribute width and height to specify the width and height.
3. Use the configure method to specify the width and height
'''
From tkinter import *
Root = TK ()
B1 = button (root, text = '30x1', width = 30, Height = 2)
B1.pack ()

B2 = button (root, text = '30x2 ')
B2 ['width'] = 30
B2 ['height'] = 3
B2.pack ()

B3 = button (root, text = '303 ')
B3.configure (width = 30, Height = 3)
B3.pack ()

Root. mainloop ()
# The above three methods are also suitable for other controls
'''6. Set the display position of the button text on the control.
Anchor:
Values: n (north), S (south), w (West), E (East), NE, NW, Se, SW, the location of the logo on the map.
The width and height attributes are used to display different attributes.
'''
From tkinter import *
Root = TK ()

# Simple is beautiful!
For a in ['n','s, 'E', 'w', 'ne, 'nw ', 'se', 'sw']:
Button (root,
TEXT = 'anchor ',
Anchor =,
Width = 30,
Height = 4). Pack ()
# Use the following code if you are not familiar with it.
# Button (root, text = 'anchor', width = 30, Height = 4). Pack ()
# Button (root, text = 'anchor', anchor = 'center', width = 30, Height = 4). Pack ()
# Button (root, text = 'anchor', anchor = 'n', width = 30, Height = 4). Pack ()
# Button (root, text = 'anchor', anchor = 's', width = 30, Height = 4). Pack ()
# Button (root, text = 'anchor', anchor = 'E', width = 30, Height = 4). Pack ()
# Button (root, text = 'anchor', anchor = 'w', width = 30, Height = 4). Pack ()
# Button (root, text = 'anchor', anchor = 'ne ', width = 30, Height = 4). Pack ()
# Button (root, text = 'anchor', anchor = 'nw ', width = 30, Height = 4). Pack ()
# Button (root, text = 'anchor', anchor = 'se', width = 30, Height = 4). Pack ()
# Button (root, text = 'anchor', anchor = 'sw ', width = 30, Height = 4). Pack ()

Root. mainloop ()
'''7. Change the foreground color and background color of the button.
FG: foreground color
BG: Background Color
'''
From tkinter import *
Root = TK ()
BFG = button (root, text = 'change foreground ', fg = 'red ')
BFG. Pack ()

BBG = button (root, text = 'change backgroud ', BG = 'blue ')
BBG. Pack ()

Root. mainloop ()

'''8. Set the button border
BD (bordwidth): The default value is 1 or 2 pixels.
'''
# Create five buttons with the following border widths:, and 8
From tkinter import *
Root = TK ()
For B in [0, 1, 2, 3, 4]:
Button (root,
TEXT = string (B ),
BD = B). Pack ()
Root. mainloop ()

'''9. Set the button style
Relief/raised/sunken/groove/Ridge
'''
From tkinter import *
Root = TK ()
For R in ['raised', 'sunken', 'groove', 'ridge ']:
Button (root,
TEXT = r,
Relief = r,
Width = 30). Pack ()
Root. mainloop ()

'''10. Set the button status
Normal/active/disabled
'''
From tkinter import *
Root = TK ()
Def stateprint ():
Print 'state'
For R in ['normal', 'active', 'Disabled ']:
Button (root,
TEXT = r,
State = r,
Width = 30,
Command = stateprint). Pack ()
Root. mainloop ()
# In this example, the three buttons are set to stateprint In the callback function. Only the normal and active callback functions are activated in the running program, while the disable button does not.
# When a button is required to take effect, you can set its state to the disabled attribute.

'''11. Bind the button and variable
Set the button property in textvariable
'''
From tkinter import *
Root = TK ()
Def changetext ():
If B ['text'] = 'text ':
V. Set ('change ')
Print 'change'
Else:
V. Set ('text ')
Print 'text'
V = stringvar ()
B = button (root, textvariable = V, command = changetext)
V. Set ('text ')
B. Pack ()
Root. mainloop ()

'''
Bind the variable V to the button. When the value of V changes, the text displayed by the button also changes.
'''

Tkinter Tutorial: button (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.