程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Tkinter - Chapter 5 button control properties

編輯:Python

The first 5 Chapter Button control (Button)
5.1 Introduction to button control
The button is a standard tkinter Control . Text or pictures can be displayed on the button as prompt information . Only one font and color can be used for text . The main function of the button is to click , You can call the specified callback function , Complete specific functions . such as , close window 、 To execute an order, etc .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1 = tk.Button(root,text="Quit",command=root.destroy)
b1.pack()
root.mainloop()

result :

This applet shows the basic functions of the button control . After clicking the button , Callback the set function . The function of this program is to exit the application , And close the window .
5.2 Button properties
The properties of the button control are as follows :

attribute describe activebackground The background color when the button is pressed . The default is the system specified color .activeforeground The foreground color when the button is pressed . The default is the system specified color .anchor How to anchor text or pictures . The default is CENTER( Centered mode ). You can choose the following methods :
N, NE, E, SE, S, SW, W, NW, or CENTER.background
bg Button background color . The default is the system specified color bitmapbitmap Form display key . If set images attribute , It ignores bitmap attribute .borderwidth
bd The width of the edge of the button . It's usually 1~2 Pixel values .command Set the callback function . When the button is pressed , This function will be called . If this property is not set , When the button is pressed , Nothing will happen .compound Show both text and picture on the button . The default mode is if an image is provided , Only pictures will be displayed . however , If the option is set to :
CENTER: Overlay text in the middle of the picture
BOTTOM: Show text below the picture
LEFT: Show text on the left side of the picture
RIGHT: Show text on the right side of the picture
TOP: Show text at the top of the picture
NONE: Don't show text cursor When the mouse moves past the button , Show cursor default Values are normal,active and disabled Three .disabledforeground When the button is disabled , The color of the words on time font The font of the text on the button . Only one font can be selected to display .foreground
fg The color of the text or bitmap on the button height Set the height of the button . If display text , Numerical values are literal units . If it is a display picture , Values are in pixels . If not set , The system automatically calculates the height of the button .highlightbackground When the button loses focus , Displays the highlighted color of the button border highlightcolor When the button gets the focus , Displays the highlighted color of the button border highlightthickness Set the width of the highlighted border image Set the picture displayed by the button . If this option is set , Will replace text or bitmap Options .justify When a button has multiple lines of text , Set alignment of text . The settable values are :
LEFT,RIGHT,CENTERoverrelief When the mouse moves past , The button shows the relief effect . If not set , Will use relief The value in .padx In the horizontal direction , Fill between button border and text or image (pad)pady In the vertical direction , Fill between button border and text or image (pad)relief Button 3D Beautify the effect . Usually , When the button is pressed , yes SUNKEN effect , Release yes RAISED effect . Other options include :GROOVE,RIDGE as well as FLATstate Button status , Include :NORMAL, ACTIVE perhaps DISABLEDtakefocus Identify whether the user can use Tab Key select button . The value is True,False perhaps Nonetext The text displayed on the button . If used bitmap perhaps image, This option is ignored textvariable take tkinter Variables are associated with buttons . If the contents of a variable change , The text of the button is also updated .underline Identify which character to underline . The default value is -1, There are no characters in the logo, and the underline is displayed below width Set the width of the button . Reference resources height attribute wraplength Determine how long the text on the button exceeds , The text will be folded into multiple lines . Unit is pixel . The default value is 0.

5.2.1 activebackground and activeforeground

Define the button in ACTIVE In the state of , Background color and text color . These two properties can be used to highlight text , Or some special effect .
Code
import tkinter as tk
root=tk.Tk()
root.geometry(‘300x240’)
b1 = tk.Button(root,text=“Quit”,activebackground=‘blue’,
activeforeground=‘yellow’,command=root.destroy)
b1.pack()
root.mainloop()
result :

5.2.2 anchor
anchor How to control the display of text . See 3.3.4 section . Look below anchor=tk.SW Example .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Button(root,text='Quit',anchor=tk.SW,
height=5,width=20,command=root.destroy)
b1.pack()
root.mainloop()

result :

The text on the button , Not the usual centered display , It is displayed in the lower left corner . This is it. anchor The role of .
5.2.3 background(bg)
Set the background color of the button .bg Is an abbreviation .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Button(root,text='Quit',height=3,width=20,
bg='yellow',command=root.destroy)
b1.pack()
root.mainloop()

result :

explain : The background color is set to yellow .
5.2.4 bitmap
Show pictures on buttons . This bitmap No windows Commonly used in the system bitmap. Not recommended bitmap. If you want to use pictures , have access to image Property to set , Support gif、png And so on .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Button(root,bitmap='@x.xbm',
bg='yellow', command=root.destroy)
b1.pack()
root.mainloop()

result :

5.2.5 borderwidth(bd)
Define the border width of the button .bd Is an abbreviation .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Button(root,text=' sign out ',height=3,width=20,
bd=10,bg='yellow',command=root.destroy)
b1.pack()
root.mainloop()

result :

5.2.6 command
command Is used to set the callback function , That is to say, after pressing the button , Call specific functions to realize certain functions . The previous example simply called the system destroy() function , In real use , It must call more complex functions .
One of the key problems is that parameters are passed . stay command in , There are no parentheses , You cannot explicitly pass parameters to a callback function . The important reason for this is , When the button is instantiated , It doesn't make sense to just pass parameters to the callback function . The parameters of the callback function should be dynamically confirmed in the program , Instead of simply confirming at the time of instantiation .
So how to pass parameters to the callback function ? A simple way is to use global variables . But this method is not the best . There are more global variables , The maintainability of the program is a kind of destruction , It also brings potential bug.
The second method is to use classes . Define the callback function and corresponding parameters in the class , Use internal variables of the class , Can reduce the impact on other programs . This method is the most common .

Another way is to use anonymous functions lambda Methods .lambda Developers can define simple functions without naming them . such as :
a=lambda x,y,z:xyz
print(a(1,2,3))

And what you get is 123=6. In the callback function of the button , You can also define similar anonymous callback functions , So as to meet the function of transferring parameters .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b2=tk.Label(root,text=' Test text ')
b2.pack()
b3 = tk.Entry(root,width=20)
b3.pack()
def change_b2(label,text):
label['text']=text.get()
b1=tk.Button(root,text=' Change label text ',height=3,
width=20,bg='yellow',
command=lambda :change_b2(b2,b3))
b1.pack()
root.mainloop()

result :


You can see , Use lambda You can pass parameters directly to the callback function . In fact, the implementation mechanism is that the button is to lambda As a callback function , But when actually called , Replace with the handler we expect .

5.2.7 compound
See Chapter 4 on compound Instructions for usage .
5.2.8 cursor
When the mouse passes the button , The cursor displayed . Cursor shape description 3.3.6 section .
5.2.9 default
The value is ACTIVE,NORMAL and DISABLED.
5.2.10 disabledforeground
When the button is disabled , The color of the text on the button .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Button(root,text=' Do not use buttons ',
height=3,width=20,state=tk.DISABLED,
disabledforeground='blue',
command=root.destroy)
b1.pack()
root.mainloop()

result :

5.2.11 font
Set the font of the text on the button , See 3.3.3 Section .
5.2.12 foreground and fg
Set the color of the text on the button . This is the text color when the button status is normal .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Button(root,text=' Normal button ',
height=3,width=20,
fg='yellow',
command=root.destroy)
b1.pack()
root.mainloop()

result :

5.2.13 highlightbackground,highlightcolor and highlightthickness
The color of the border when the corresponding button does not get focus (highlightbackground)、 The color of the border when the button gets focus (highlightcolor) And the width of the border (highlightthickness). But for button controls , These three parameters do not work .
5.2.14 image
Show pictures on buttons . When using image Attribute ,text And bitmap Properties are masked .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
p = tk.PhotoImage(file="a.gif")
b1=tk.Button(root,height=80,width=100,
image=p,command=root.destroy)
b1.pack()
root.mainloop()

result :

5.2.15 justify
The alignment of multiple lines of text displayed on the button . See Chapter IV for specific instructions .
5.2.16 overrelief
Set the... When the mouse passes the button 3D effect . Value and relief equally . When the mouse pointer is in the button area , The corresponding... Will be displayed according to the settings 3D effect

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Button(root,height=4,width=10,text=' sign out ',
bd=10,command=root.destroy,
overrelief='sunken')
b1.pack()
root.mainloop()

result :


When the mouse moves over the button , Will change 3D appearance .
5.2.17 padx and pady
The inner margin between the text or image and the button border .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Button(root,text=' sign out ',
padx=10,pady=10,command=root.destroy)
b1.pack()
b2=tk.Button(root,text=' sign out ',
command=root.destroy)
b2.pack()
root.mainloop()

result :

The first button is set padx and pady The effect of . The second button is not set padx and pady. You can find that the text of the first button is farther from the border than that of the second . This is it. padx and pady The role of .
5.2.18 relief
Button on the 3D effect . In general , When the button is pressed sunken effect , When released raised effect . You can change the effect of button release by setting this property , But it does not affect the effect when pressing .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Button(root,text=' sign out ',bd=10,
command=root.destroy,relief='solid')
b1.pack()
root.mainloop()

result :

5.2.19 state
Set the state of the button . There are three states :NORMAL,ACTIVE and DISABLED.NORMAL and ACTIVE It's the same thing , All buttons can be used normally . and DISABLED Will prohibit the use of buttons , The button will be grayed out , And cannot press . See for specific usage 5.2.10 Examples in .

5.2.20 takefocus
Set whether you can use Tab To select the button .True To be able to use Tab Key selected ,False For not . The following code is an example that can be selected . By Tab Selected button , A virtual box will appear on the button . Now press the space bar , You can activate the function of this button . Without this imaginary frame , You can only use the mouse click to activate the function of the button .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Button(root,text=' sign out ',bd=5,
command=root.destroy,takefocus=True)
b1.pack()
root.mainloop()

result :

5.2.21 text
Used to set the text on the button . Many previous chapters have used examples , I won't repeat it here .
5.2.22 textvariable
Used to change the text on the button .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
btn_text=tk.StringVar()
def btn_change():
btn_text.set(' Change the text ')
btn_text.set(' sign out ')
b1=tk.Button(root,bd=5, command=btn_change,
textvariable=btn_text)
b1.pack()
root.mainloop()

result :


5.2.23 underline
The logo is underlined under that letter . It is also valid for Chinese .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Button(root,bd=5, command=root.destroy,
text=' sign out ',underline=1)
b1.pack()
root.mainloop()

result :

5.2.24 width and height
Set the width of the button . For text, the width of the text , For the image, it is in pixels . For specific usage, see the examples in the previous chapters .

5.2.25 wraplength
And in Chapter 4 wraplength In the same way , Referable .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved