Tuesday, April 22, 2025

Tkinter - Labels and Entry Fields

In this tutorial we will learn how to have Entry fields in Tkitner Canvas. Users need explanations what to type in entry fields so we must create Labels.

First, we will create Canvas with lightgreen background. Position for Canvas is in top_win of size 800 x 800:

can = Canvas(top_win, width = 400, height = 400, bg = "lightgreen")  

 And than, Labels. We will position it using place() method:

user_name = Label(top_win, text = "Username: ")
user_name.place(x = 20, y = 40)

user_pass = Label(top_win, text = "Password: ")
user_pass.place(x = 20, y = 80)

Once we have Labels, it's time to create Entry fields. We also need to place them:

entry_name = Entry(top_win).place(x = 100, y = 40)

entry_pass = Entry(top_win).place(x = 100, y = 80)

After we are done with element positioning, it's time for Canvas using grid values:

can.grid(row = 0, column = 0)

This is full working source:

from tkinter import *
import tkinter.messagebox as mb
import os

top_win = Tk()
top_win.geometry("800x800")

can = Canvas(top_win, width = 400, height = 400, bg = "lightgreen")      

user_name = Label(top_win, text = "Username: ")
user_name.place(x = 20, y = 40)

user_pass = Label(top_win, text = "Password: ")
user_pass.place(x = 20, y = 80)

submit_buton = Button(top_win, text = "Login")
submit_buton.place(x = 20, y = 120)

entry_name = Entry(top_win).place(x = 100, y = 40)

entry_pass = Entry(top_win).place(x = 100, y = 80)

can.grid(row = 0, column = 0)
top_win.mainloop()

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...