[PYTHON] Getting Started with Tkinter 2: Buttons

4. Button widget

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

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 image.png

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.

4.1 activebackground Specify the background color during clicking

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) image.png

4.2 activeforeground Specify the foreground color 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) image.png

4.3 bd Specify the edge size

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 image.png

4.4 bg Specify background color

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 image.png

4.5 command Execute the specified program

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 image.png

4.6 fg Specify the foreground color.

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")

image.png

4.7 Specify the font

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 image.png You can confirm that the font can be specified.

4.8 height, width Specify the size of the button

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 image.png

4.9 Place an image on the image button

I will write it later

4.10 justify Specify the position to move to when it is a multi-line character string

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 image.png

4.11 padx, pady Specify the inner blank

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 image.png

4.12 relief Specify the style of the abyss

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 image.png

4.13 state Change button attributes

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 image.png

4.14 underline Underline

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 image.png

4.15 wraplength Wrap characters to any size

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 image.png

That was the button widget.

Next time I would like to learn about canvas.

Recommended Posts

Getting Started with Tkinter 2: Buttons
Getting started with Android!
1.1 Getting Started with Python
Getting Started with Golang 2
Getting started with apache2
Getting Started with Golang 1
Getting Started with Python
Getting Started with Django 1
Getting Started with Optimization
Getting Started with Golang 3
Getting Started with Numpy
Getting started with Spark
Getting Started with Python
Getting Started with Pydantic
Getting Started with Golang 4
Getting Started with Jython
Getting Started with Django 2
Translate Getting Started With TensorFlow
Getting Started with Python Functions
Getting Started with Go Assembly
Getting Started with PKI with Golang ―― 4
Getting Started with Python Django (1)
Getting Started with Python Django (4)
Getting Started with Python Django (3)
Getting Started with Python Django (6)
Getting Started with Django with PyCharm
Python3 | Getting Started with numpy
Getting Started with Python Django (5)
Getting Started with Python responder v2
Getting Started with Git (1) History Storage
Getting started with Sphinx. Generate docstring with Sphinx
Getting Started with Python Web Applications
Getting Started with Python for PHPer-Classes
Getting Started with Sparse Matrix with scipy.sparse
Getting Started with Julia for Pythonista
Getting Started with Python Basics of Python
Getting Started with Cisco Spark REST-API
Getting started with USD on Windows
Getting Started with Python Genetic Algorithms
Getting started with Python 3.8 on Windows
Getting Started with Python for PHPer-Functions
Getting Started with CPU Steal Time
Getting Started with python3 # 1 Learn Basic Knowledge
Getting Started with Flask with Azure Web Apps
Getting Started with Python Web Scraping Practice
MVC with Tkinter
Getting Started with Python for PHPer-Super Basics
Getting Started with Python Web Scraping Practice
Getting started with Dynamo from Python boto
Getting Started with Lisp for Pythonista: Supplement
Getting Started with Heroku, Deploying Flask App
Getting Started with TDD with Cyber-dojo at MobPro
Grails getting started
MongoDB Basics: Getting Started with CRUD in JAVA
Getting Started with Drawing with matplotlib: Writing Simple Functions
Getting started with Keras Sequential model Japanese translation
[Translation] Getting Started with Rust for Python Programmers
Django Getting Started Part 2 with eclipse Plugin (PyDev)
Getting started with AWS IoT easily in Python
Getting Started with Python's ast Module (Using NodeVisitor)
Materials to read when getting started with Python