Run Label with tkinter [Python]

Run Label with tkinter. Please refer to [tkinter] Try using Label etc. for the basic part such as how to arrange Frame and Label.

Sample code

A program that starts moving in the opposite direction when both ends of label are about to come out from both ends of Frame.

from tkinter import ttk
import tkinter

def move(flag,i):
    if(flag==True):
        i-=1
    else:
        i+=1
    if(i==0):
        flag=False
    elif(i+label.winfo_reqwidth()==400):
        flag=True
    label.place(x=i,y=150)
    label.after(4,lambda: move(flag,i))

w,h="400","300"
root=tkinter.Tk()
ttk.Style().configure("TP.TFrame", background="snow")
f=ttk.Frame(master=root,style="TP.TFrame",width=w,height=h)
f.pack()
fontsize=20
label=ttk.Label(master=root,text="Labeltext",font=("Meiryo",fontsize),foreground="red",background="green")
i=400
flag=True
move(flag,i)
root.mainloop()

Execution result

move.gif

Commentary

To reproduce how the Label "moves", recursively execute the function that moves __1pixel to the left (right) __. That is, the move function is called within the move function that moves 1 pixel label. label.after (n, f) executes the function f after n milliseconds, so label.after (4, lambda: move (flag, i)) sets the x coordinate of label to 1. Execute the move function again after 4 milliseconds on the line (line 14) just before the end of the move function to be changed. Doing so creates an infinite loop of the move function and makes the Label appear to work.

[Bonus] Add arguments to label.after () and label.bind ()

A bind method that can execute the function f when an object is clicked (left-click because it is <1>, right-click if <3>), such as label.bind (<1>, f). In the after method that can execute the function g after n milliseconds for a certain object like label.after (n, g) introduced earlier, the function name is put in the second argument, but if it is as it is Cannot pass arguments. If you write label.after (n, move (flag, i)), an error will occur. This is because move (flag, i) () is actually called.

I would like to introduce two ways to solve this. __ (1) Notation with lambda (anonymous function) __ That is, it should be label.after (n, lambda: move (flag, i)). For lambda, please refer to Python lambda is easy to understand.

__ (2) Nesting functions __ Nested = "nested". In Python you can define another function within a function. Here, as introduced earlier, label.after (n, move (flag, i)) actually says not move (flag, i) but move (flag, i) (). So, you can forcibly create that shape. I don't want to recommend this because the code tends to be dirty (while saying that Twitter search results are sent like a certain video site [python] It's a secret that some spaghettis contain many nested functions).

The above sample code works correctly if you do the following.

from tkinter import ttk
import tkinter

def move(i):
    def x():
        global i
        global flag
        if(i==0):
            flag=False 
        elif(i+label.winfo_reqwidth()==400):
            flag=True
        if(flag==True):
            i-=1
        else:
            i+=1
        label.place(x=i,y=150)
        label.after(4,move(i))
    return x

(Omission)

i=400
flag=True
label.after(1,move(i))
root.mainloop()

dirty.

Recommended Posts

Run Label with tkinter [Python]
Run prepDE.py with python3
Run Blender with python
Run iperf with python
Programming with Python and Tkinter
Run python with PyCharm (Windows)
Run Python with CloudFlash (arm926ej-s)
Let's run Excel with Python
Run DHT22 with RasPi + Python
Run Rotrics DexArm with python API
Run mruby with Python or Blender
Run XGBoost with Cloud Dataflow (Python)
Run Aprili from Python with Orange
Run python3 Django1.9 with mod_wsgi (deploy)
Until you run python with apache
[Python] Creating multiple windows with Tkinter
Play video with sound with python !! (tkinter / imageio)
FizzBuzz with Python3
Scraping with Python
Run servo with Python on ESP32 (Windows)
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
MVC with Tkinter
Develop Windows apps with Python 3 + Tkinter (Preparation)
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Run a Python web application with Docker
Excel with Python
Microcomputer with Python
[Python] Tkinter template
Cast with python
Create a frame with transparent background with tkinter [Python]
GUI image cropping tool made with Python + Tkinter
Python> Run with run-time arguments> Use import argparse
[Python] I made a Youtube Downloader with Tkinter.
How to use tkinter with python in pyenv
Run VMware vSphere 6 vSphere API with Python script (pyvmomi)
Run Flask on CentOS with python3.4, Gunicorn + Nginx.
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1