An introduction to cross-platform GUI software made with Python / Tkinter! (And many Try and Error)! (In the middle of writing)

Introduction ...

The title is long and Sumimasen !!!!! It's difficult to summarize what you want to write ... Someone please give me a draft ... (By the way, it's been a long time since then. Please use the table of contents.) Also, please note that it is ** in the middle of writing **.

What is Python / Tkinter?

Source:Free encyclopedia "Wikipedia"

Tkinter is a standard library (widget toolkit) for building and manipulating GUIs from Python.

Tcl/The Tk part of Tk is made available in Python, and Tcl is used as much as possible./It is made to match Tk.
This makes it possible to easily create an application with a GUI screen from Python, which is a scripting language.

(The following is omitted) 

It is written on Wikipedia, but well, to put it simply

GUI creation library that comes standard with Python

Is. The important thing is the "standard". You can create an application just by installing python (or using Pyinstaller) (without using other libraries). If anything, you can use Pyarmor to protect your source code.

Tkinter reference

window

First, import the module ...

#With Python2...
import Tkinter #tkinter body
import ttk #tkinter extension(?)
#With Python3...
import tkinter #tkinter body
from tkinter import ttk #tkinter extension(?)

Note that the module names are different between Python 2 and 3. By the way, if you write as follows, you can handle a few cars.

try:
    import Tkinter as tkinter
    import ttk
except:
    import tkinter
    from tkinter import ttk

By the way, "ttk", which has appeared from a while ago, is an extension (?) Of tkinter. You can use this to change the style (theme).

Main window

Next, create the main window.

root = tkinter.Tk()#Read carefully after this

However, if this is left as it is, the application name on the taskbar will be displayed as "Tk" on OS such as Ubuntu, so it is not good.

root = tkinter.Tk(className="[app name]")

And.

Sub window

sub = tkinter.TopLevel()

And.

full screen

root.attributes("-fullscreen", True)

When returning

root.attributes("-fullscreen", False)

Widget

button

def callback():
    print("Hello!!")

button = ttk.Button(root, text="[Text to display]",command=callback)

For command, specify the ** name ** of the function to be executed when the button is pressed. If you want to specify an argument,

def callback(hogehoge):
    print(hogehoge)

button = ttk.Button(root, text="[Text to display]",command=lambda: callback("Hello!!!"))

Frame (frame)

frame = ttk.Frame(root)

Well ... frame is used by putting it in the root part of windows and other widgets. If you like, you can change the color and shape. (I will add it later.) An example of utilization is shown below.

def callback(hogehoge):
    print(hogehoge)

frame = ttk.Frame(root)
button = ttk.Button(frame, text="[Text to display]",command=lambda: callback("Hello!!!"))

Label (one line of unselectable text)

label = ttk.Label(root, text="[Text to display]")

Widget placement

In tkinter, you need to ** deploy ** after defining the widget. There are three placement methods.

Packing method (pack)

In the packing method (pack), the widget is placed in an empty space of the window. (vocabulary...)

[Widget].pack([option])

[Widget] is an instance of the widget, (If you're crazy about instances and functions, check out the other Python introductory articles.) The following can be specified for [Option](it does not matter if nothing is specified): ・ Side You can set which direction to place it. Configurable value: "right" right, "left" left, "bottom" bottom, "top" top ・ Fill You can set which direction to fill. Configurable value: "both"-both directions "x"-x direction (vertical?) "y" -y direction (horizontal?) (Honestly, I'm worried, so I'd appreciate it if you could experiment and tell me.) ・ Anchor You can set where to place it. (Similar to side, but more specific) Configurable value: Well ... it's like this. (Quoted from here)   nw --- n --- ne   |      |   w  c   e   |      |   sw --- s --- se ・ Expand You can set whether to fill in when blanks are created (for example, when the window size changes) Configurable value:   True or False (It's because of my mind that the annoying odor is exposed ... it's true ...)

References etc.

http://www.nct9.ne.jp/m_hiroi/light/python3.html#python3_tkinter

Recommended Posts

An introduction to cross-platform GUI software made with Python / Tkinter! (And many Try and Error)! (In the middle of writing)
When I try to execute the make command of Makefile with os / exec of golang, the second and subsequent executions result in an error.
What to do if you run python in IntelliJ and end with an error
Try scraping the data of COVID-19 in Tokyo with Python
Try to make it using GUI and PyQt in Python
Try to automate the operation of network devices with Python
I tried to make GUI tic-tac-toe with Python and Tkinter
[Python] Visualize Arashi's lyrics with WordCloud and try to understand what I wanted to convey to fans in the 20th year of formation.
IPynb scoring system made with TA of Introduction to Programming (Python)
How to know the internal structure of an object in Python
[Python] PCA scratch in the example of "Introduction to multivariate analysis"
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
Difference in behavior of transparent Frame made with tkinter in pyinstaller [Python]
[Introduction to Python] I compared the naming conventions of C # and Python.
Output the contents of ~ .xlsx in the folder to HTML with Python
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
I made an npm package to get the ID of the IC card with Raspberry Pi and PaSoRi
Put Cabocha 0.68 on Windows and try to analyze the dependency with Python
Comparing the basic grammar of Python and Go in an easy-to-understand manner
How to get the date and time difference in seconds with python
Try to image the elevation data of the Geographical Survey Institute with Python
[Introduction to Python] How to sort the contents of a list efficiently with list sort
[Introduction to Python] Thorough explanation of the character string type used in Python!
I made a program to check the size of a file in Python
[Introduction to Python] What is the method of repeating with the continue statement?
An example of the answer to the reference question of the study session. In python.
Try to separate the background and moving object of the video with OpenCV
Try logging in to qiita with Python
The story of an error in PyOCR
[CleanArchitecture with Python] Apply CleanArchitecture step by step to a simple API and try to understand "what kind of change is strong" in the code base.
(Diary 1) How to create, reference, and register data in the SQL database of Microsoft Azure service with python
An engineer who has noticed the emo of cryptography is trying to implement it in Python and defeat it
I'm an amateur on the 14th day of python, but I want to try machine learning with scikit-learn
[Python] I want to know the variables in the function when an error occurs!
[python] What to do when an error occurs in send_keys of headless chrome
I want to send Gmail with Python, but I can't because of an error
To output a value even in the middle of a cell with Jupyter Notebook
I made an appdo command to execute a command in the context of the app
Try out the touch of data-driven testing with Selenium Python Bindings and py.test
To automatically send an email with an attachment using the Gmail API in Python
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
GUI image cropping tool made with Python + Tkinter
Try to solve the man-machine chart with Python
Introduction to Python with Atom (on the way)
How to use tkinter with python in pyenv
Example of reading and writing CSV with Python
Run the output code with tkinter, saying "A, pretending to be B" in python
[Python] I made an app to practice the subtle voice distinction of English words.
How to identify the element with the smallest number of characters in a Python list?
Extract images and tables from pdf with python to reduce the burden of reporting
I tried to automate the article update of Livedoor blog with Python and selenium.
An easy way to pad the number with zeros depending on the number of digits [Python]
What to do if the print command itself causes an error in Maya python
I compared the speed of the reference of the python in list and the reference of the dictionary comprehension made from the in list.
[Introduction to Python] Summary of functions and methods that frequently appear in Python [Problem format]
[Note] How to write QR code and description in the same image with python
[Introduction to Sound] Let's arrange the introduction to sounds of python and R ♬-Listen to the sound of the explosion of Nikkei 255-
[Python] Explain the difference between strftime and strptime in the datetime module with an example
[Cloud9] Try to build an environment with django 1.11 of Python 3.4 without understanding even 1 mm
How to count the number of occurrences of each element in the list in Python with weight