This chapter introduces button widgets. Button widgets are widgets that allow you to enter user decisions quickly and intuitively for a limited number of choices. Below are the Button widget options.
--4.0 Button Widget basic structure
This widget can be used by instantiating it with tkinter.Button (). The display method can be placed with .pack () as before.
4.0_BasicButtonWidget
import tkinter as tk
pop = tk.Tk()
#Stores button instance information in a variable called button
# tk.Button(Target window, text="Character string to be displayed on the button")
button = tk.Button(pop, text="Hello")
#Output button
button.pack()
pop.mainloop()
Execution result
Now that the window is too small, let's make it a little bigger.
4.0_modify_x_y
import tkinter as tk
pop = tk.Tk()
# .geometry("Vertical x horizontal")You can specify the size with * x is X
pop.geometry("200x200")
# 4.Only list the lines that contain additional options when instantiating from 1.
button = tk.Button(pop, text="Hello")##This line
button.pack()
pop.mainloop()
You should now be able to resize the window. From now on, only the lines that contain additional options will be listed. I'll post all the code if there are significant changes overall.
Specifies the background color while the button is pressed. It can be specified by ʻactive background =" color "`. You can specify the color by using a character string such as "red" or "blue", or you can use the hexadecimal notation using #.
4.1_Button_activebackground
#After the option","You can add various options by using.
#active background outputs the background color when the button is pressed.
button = tk.Button(pop, text="Hello",activeforeground="red")
Execution result (background color changes while clicking)
Foreground version of activebackground. You can specify the color of the object in front while you click. In this case, you can specify a color for the string Hello.
4.2_Button_activeforeground
#activeforeground outputs the foreground color when the button is pressed.
button = tk.Button(pop, text="Hello",activeforeground="red")
Execution result (Foreground color changes while clicking)
Specify the size of the edge of the button (unit is pixel). If this option is not specified, it will be initialized with 2px when it is generated. Let's try to display some buttons
4.3_Button_bd
#Specifies the size of the button edge
button = tk.Button(pop, text="0px",bd="0")
button1 = tk.Button(pop, text="2px",bd="2")
button2 = tk.Button(pop, text="5px",bd="5")
button3 = tk.Button(pop, text="10px",bd="10")
button4 = tk.Button(pop, text="20px",bd="20")
button.pack()
button1.pack()
button2.pack()
button3.pack()
button4.pack()
Execution result
Change the background color of the button. This is an action-independent option, unlike activeforeground and activebackground.
4.4_Button_bg
#Specifies the background color in the normal state. It does not depend on the action.
button = tk.Button(pop, text="Hello",bg="red")
Execution result
Executes the program specified by command. Enter the name of the function you want to execute in command. In the example below, we want to use a function called output, so command = output
is now complete.
4.5_Button_command
#Output hello tkinter to the console
def output():
print("hello tkinter!")
#You can execute the specified program.
#Enter only the function name in command.
button = tk.Button(pop, text="Hello",command=output)
Execution result
For the background color, I used the command bg. This time it is fg. fg specifies the color of the content in the foreground. So in this case you can specify a color for the string hello. Like bg, fg is an action-independent specification method.
4.6_Button_fg
#Specify the foreground color(* Does not depend on action)
button = tk.Button(pop, text="Hello",fg="red")
Refers to the font installed on the computer and adds the font to any character string. Let's try generating multiple buttons to see if the font is actually specified.
4.7_Button_font
#Specify the font
button1 = tk.Button(pop, text="Hello")
button2 = tk.Button(pop, text="Hello",font="gabriola")
button3 = tk.Button(pop, text="Hello",font="consolas")
button1.pack()
button2.pack()
button3.pack()
Execution result You can confirm that the font can be specified.
You can adjust the size of the button by using height and width. height is the height and width is the width. Only one of them is acceptable.
4.8_Button_width_height
#Specify the size of the button
button = tk.Button(pop, text="Hello",width="20",height="10")
Execution result
I will write it later
Specifies where the string should be placed when there are multiple lines of string. There are three attributes, "left", "center", and "right". Let's actually make some buttons and try them.
4.10_Button_justify
button1 = tk.Button(pop, text="left\nleftleftleft",width="30",justify="left")
button2 = tk.Button(pop, text="center\ncentercenter",width="30",justify="center")
button3 = tk.Button(pop, text="right\nrightright",width="30",justify="right")
button1.pack()
button2.pack()
button3.pack()
Execution result
Specifies a blank space inside the button. padx can be specified for the horizontal axis, and pady can be specified for the vertical axis.
4.11_Button_padding_x_y
#Adds a space inside the button. padx specifies the space on the horizontal axis, and pady specifies the space on the vertical axis.
button = tk.Button(pop, text="Hello",padx='30',pady='50')
Execution result
Specifies the type of button edge. There are 5 types of edges, which can be specified as needed. The attributes are as follows.
--flat: Flat button --sunken: Pressed --raised: The state of popping out to the front --Groove: The state where the edge is buried in the groove --ridge: A state where the pool is rising --solid: Thick groove
4.12_Button_relief
#Specifies the style of the pool
button1 = tk.Button(pop, text="Hello",relief='flat')
button2 = tk.Button(pop, text="Hello",relief='sunken')
button3 = tk.Button(pop, text="Hello",relief='raised')
button4 = tk.Button(pop, text="Hello",relief='groove')
button5 = tk.Button(pop, text="Hello",relief='ridge')
button6 = tk.Button(pop, text="Hello",relief='solid')
button1.pack()
button2.pack()
button3.pack()
button4.pack()
button5.pack()
button6.pack()
Execution result
The state can activate or deactivate the button. There are three types of attributes, but one of them has the same meaning, so I will omit it.
--normal: Buttons that can be pressed --disable: Prevents the button from being pressed
Let's actually try two buttons.
4.13_Button_state
#Specifies the style of the pool
#Change button attributes
button1 = tk.Button(pop, text="normal",state='normal')
button2 = tk.Button(pop, text="active",state='active')
button3 = tk.Button(pop, text="disable",state='disable')
button1.pack()
button2.pack()
button3.pack()
Execution result
Specifies the number to underline, with the first character as the 0th. The default is -1, in which case no character is underlined.
4.14_Button_underline
#Underline any character
button = tk.Button(pop, text="disable",underline='3')
Execution result
Wrap characters in any size you like. Note that it is specified by a number, but it is the number of pixels, not the number of digits of the character. If you want to wrap at 20px, write as follows.
4.15_Button_state
#Wrap characters to any size
button = tk.Button(pop, text="wraplength",wraplength='20')
Execution result
Next time I would like to learn about canvas.
Recommended Posts