Home>

I want to write text that guides in light letters in the input box for entering numbers so that you can understand "This is where you enter numbers".

Corresponding source code

`` `Python
import tkinter

Random output of numbers

import random
D = random.randint (0,5)

Window creation

root = tkinter.Tk ()
root.title ("Fortune-telling")
root.minsize (640, 480)
root.option_add ("* font", ["MS Gothic", 22])

Image loading

getu = tkinter.PhotoImage (file = "Daikichi.png")
ka = tkinter.PhotoImage (file = "bad.png")
sui = tkinter.PhotoImage (file = "Kichi.png")
moku = tkinter.PhotoImage (file = "Kokichi.png")

Image display

canvas = tkinter.Canvas (bg = "plum", width = 640, height = 480)
canvas.place (x = 0, y = 0)
img = tkinter.PhotoImage (file = "shrine.png")
canvas.create_image (320, 240, image = img)

Label 01

question = tkinter.Label (text = "Check your fortune today with the number you entered. \ NEnter a number from 0 to 10.", bg = "plum")
question.place (x = 27, y = 20)

Label 02

entry = tkinter.Entry (width = 2, bd = 4, bg = "pink")
entry.place (x = 300, y = 140)

Label 03

button = tkinter.Button (text = "OK button", bg = "peachpuff")
button.place (x = 240, y = 400)

Omikuji begins

def btn_click ():
a = float (entry.get ())
if a>10:
again_text = tkinter.Label (text = "Enter a number from 0 to 10", fg = "red", bg = "plum")
again_text.place (x = 105, y = 20)
question.destroy ()
return
if a<0:
again_text = tkinter.Label (text = "Enter a number from 0 to 10", fg = "red", bg = "plum")
again_text.place (x = 105, y = 20)
question.destroy ()
return
if a + D == 8 or a + D == 9:
again_text = tkinter.Label (text = "text =", fg = "red", bg = "plum"))
again_text.place (x = 100, y = 20)
res_text = tkinter.Label (text = "Your fortune today is Daikichi.", Fg = "red", bg = "plum")
res_text.place (x = 115, y = 20)
res_text = tkinter.Label (text = "You did it!", Bg = "plum")
res_text.place (x = 250, y = 425)
canvas.delete ("all")
question.destroy ()
entry.destroy ()
button.destroy ()
canvas.create_image (320, 240, image = getu)
if 10<= a + D<= 15:
again_text = tkinter.Label (text = "text =", fg = "red", bg = "plum"))
again_text.place (x = 100, y = 20)
res_text = tkinter.Label (text = "Your fortune today is bad.", Fg = "darkviolet", bg = "plum")
res_text.place (x = 120, y = 20)
res_text = tkinter.Label (text = "Zannen", bg = "plum")
res_text.place (x = 250, y = 425)
canvas.delete ("all")
question.destroy ()
entry.destroy ()
button.destroy ()
canvas.create_image (320, 240, image = ka)
if 4<= a + D<= 7:
again_text = tkinter.Label (text = "text =", fg = "red", bg = "plum"))
again_text.place (x = 100, y = 20)
res_text = tkinter.Label (text = "Your fortune today is good.", Fg = "green", bg = "plum")
res_text.place (x = 120, y = 24)
res_text = tkinter.Label (text = "Ohhhhh", bg = "plum")
res_text.place (x = 240, y = 430)
canvas.delete ("all")
question.destroy ()
entry.destroy ()
button.destroy ()
canvas.create_image (320, 245, image = sui)
if 0<= a + D<= 3:
again_text = tkinter.Label (text = "text =", fg = "red", bg = "plum")
again_text.place (x = 100, y = 20)
res_text = tkinter.Label (text = "Your fortune today is Kokichi.", Fg = "sienna", bg = "plum")
res_text.place (x = 120, y = 20)
res_text = tkinter.Label (text = "Good!", Bg = "plum")
res_text.place (x = 243, y = 425)
canvas.delete ("all")
question.destroy ()
entry.destroy ()
button.destroy ()
canvas.create_image (320, 240, image = moku)
button ["command"] = btn_click
root.mainloop ()

What I tried

In Tkinter, I put the label "Number" at the same coordinates as the input box. It seems that the input box is overwritten, so I can no longer input.

Supplementary information (FW/tool version, etc.)

Windows 10
Python 3.7.1

  • Answer # 1

    Original answer

    Perhaps what you are imagining is a so-called placeholder (like the image below).

    Unfortunately, TKinter's Entry doesn't handle placeholders by default.

    Although in English, here is an article about the Entry class that allows you to create beautiful Placeholders. The image displayed is a screenshot of the code in the article executed.


    Postscript

    In the original article, some of the indents are tabs and may not be executed as they are.
    If you want to play with the coordinates of PlaceHolder (right justified, left justified, etc.), you may need to modify the article class further.

    For the time being, I will paste only the code that I moved above for reference. (I just fixed the indentation)

    from tkinter import ttk
    class PlaceholderEntry (ttk.Entry):
        def __init __ (self, container, placeholder, * args, ** kwargs):
            super () .__ init__ (container, * args ,, ** kwargs)
            self.placeholder = placeholder
            self.insert ("0", self.placeholder)
            self.bind ("<FocusIn>", self._clear_placeholder)
            self.bind ("<FocusOut>", self._add_placeholder)
        def _clear_placeholder (self, e):
            if self ["style"] == "Placeholder.TEntry":
                self.delete ("0", "end")
                self ["style"] = "TEntry"
        def _add_placeholder (self, e):
            if not self.get ():
                self.insert ("0", self.placeholder)
                self ["style"] = "Placeholder.TEntry"
    root = tk.Tk ()
    style = ttk.Style (root)
    style.configure ("Placeholder.TEntry", foreground = "# d5d5d5")
    entry = PlaceholderEntry (root, "Sample placeholder")
    entry.pack ()
    root.mainloop ()