How can tkinter entry widget to "tab" automatically after certain digits/characters? - python

I am writing a python tkinter trying to have 2 entry widgets. After entering 5 digits in the first entry widget, I want the 6th digit could be jumped to next entry widget automatically. How can I rewrite it to make it come true?
import tkinter as tk
root=tk.Tk()
canvas1=tk.Canvas(root,width=400,height=400,bg='#FFFFFF')
canvas1.pack()
entry1=tk.Entry(root,width=8)
canvas1.create_window(10,100,window=entry1,anchor='nw')
entry1.focus_set()
entry1=tk.Entry(root,width=8)
canvas1.create_window(100,100,window=entry1,anchor='nw')
root.mainloop()

Entry validation is what you are looking for. Try the following code:
import tkinter as tk
def on_validate(P):
if len(P) == 5: # The 6th entry is taken up by the 2nd entry widget
entry2.focus_set()
return True
root = tk.Tk()
canvas1 = tk.Canvas(root, width=400, height=400, bg='#FFFFFF')
canvas1.pack()
entry1 = tk.Entry(root, width=8, validate="key")
entry1['validatecommand'] = (entry1.register(on_validate), '%P')
canvas1.create_window(10, 100, window=entry1, anchor='nw')
entry1.focus_set()
entry2 = tk.Entry(root, width=8)
canvas1.create_window(100, 100, window=entry2, anchor='nw')
root.mainloop()

Related

Python- How to place two buttons left-right and next to each other in the middle of screen?

First, I read the related discussion:
How can I put 2 buttons next to each other?.
However, I still confused about it. My codes:
#Import the required Libraries
from tkinter import *
from tkinter import ttk
import random
win = Tk()
win.geometry("750x250")
def clear():
entry.delete(0,END)
def display_num():
for i in range(1):
entry.insert(0, random.randint(5,20))
entry= Entry(win, width= 40)
entry.pack()
button1= ttk.Button(win, text= "Print", command=display_num)
button1.pack(side= LEFT)
button2= ttk.Button(win, text= "Clear", command= clear)
button2.pack(side=LEFT)
win.mainloop()
Now I got
I want two buttons in the middle of screen just below the Entry (white box).
How to fix it? Thanks!
You can make another tk.Frame which is arranged horizontally and pack it below; for example:
entry= Entry(win, width= 40)
entry.pack()
buttons = ttk.Frame(win)
buttons.pack(pady = 5)
button1= ttk.Button(buttons, text= "Print", command=display_num)
button1.pack(side = LEFT)
button2= ttk.Button(buttons, text= "Clear", command= clear)
button2.pack()
Alternatively you can use the grid layout manager.
entry= Entry(win, width= 40)
entry.grid(row = 0, column = 0, columnspan = 2)
button1= ttk.Button(win, text= "Print", command=display_num)
button1.grid(row = 1, column = 0)
button2= ttk.Button(win, text= "Clear", command= clear)
button2.grid(row = 1, column = 1)
Working with pack means working with parcels, therefore Imagine a rectangle around your widgets while we discuss further more. By default your values look like this:
widget.pack(side='top',expand=False,fill=None,anchor='center')
To get your widget in the spot you like you will need to define it by these parameters. the side determinates in which direction it should be added to. Expand tells your parcel to consume extra space in the master. fill tells your widget to strech out in its parcel in x or y or both direction. you can also choose to anchor your widget in your parcel in east,west,north,south or a combination of it.
from tkinter import *
from tkinter import ttk
import random
win = Tk()
win.geometry("750x250")
def clear():
entry.delete(0,END)
def display_num():
for i in range(1):
entry.insert(0, random.randint(5,20))
entry= Entry(win)
entry.pack(fill='x')
button1= ttk.Button(win, text= "Print", command=display_num)
button1.pack(side= LEFT,expand=1,anchor='ne')
button2= ttk.Button(win, text= "Clear", command= clear)
button2.pack(side=LEFT,expand=1,anchor='nw')
win.mainloop()
To learn more about orginizing widgets and the geometry management of tkinter, see my answer here.

Entry in notebook - Tkinter

I have a problem with input in a notebook in TKinter. The problem is in this to add Entry to not book I tried add Entry to root and to the notebook but still is not working, so here is the code:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title("Lizard Notebook")
root.geometry('600x400+50+50')
root.resizable(True, True)
root.minsize(400, 400)
notebook = ttk.Notebook(root)
notebook.pack(expand=True)
frame1 = ttk.Frame(notebook, width=1000, height=500)
frame2 = ttk.Frame(notebook, width=1000, height=500)
frame1.pack(fill='both', expand=True)
frame2.pack(fill='both', expand=True)
notebook.add(frame1, text='Notebook')
notebook.add(frame2, text='Options')
entry = ttk.Entry(root, width=40)
entry.focus_set()
entry.pack()
root.mainloop()
Your question is difficult to understand but the code example is good and one solution is to change width and height of frame1 and frame2.
frame1 = ttk.Frame(notebook, width=600, height=352)
frame2 = ttk.Frame(notebook, width=600, height=352)
This places the entry tool beneath the notebook.
EDIT
As #aws noted in the comment: you don't need frameX.pack() when you use notebook.add(frameX, ...)
If you want to display Entry inside Notebook then you should use frame1 instead of root
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title("Lizard Notebook")
root.geometry('600x400+50+50')
root.resizable(True, True)
root.minsize(400, 400)
notebook = ttk.Notebook(root)
notebook.pack(expand=True, fill='both')
frame1 = ttk.Frame(notebook)
frame2 = ttk.Frame(notebook)
notebook.add(frame1, text='Notebook')
notebook.add(frame2, text='Options')
entry = ttk.Entry(frame1, width=40)
entry.focus_set()
entry.pack()
root.mainloop()
If you want to display Entry below Notebook then your width,height creates too big notebook and it uses all space in window and it can't display Entry - and you have to manually resize window to see Entry.
Better use fill='both' in
notebook.pack(expand=True, fill='both')
and it will automatically use all space but it will also display Entry.
mport tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title("Lizard Notebook")
root.geometry('600x400+50+50')
root.resizable(True, True)
root.minsize(400, 400)
notebook = ttk.Notebook(root)
notebook.pack(expand=True, fill='both')
frame1 = ttk.Frame(notebook)
frame2 = ttk.Frame(notebook)
notebook.add(frame1, text='Notebook')
notebook.add(frame2, text='Options')
entry = ttk.Entry(root, width=40)
entry.focus_set()
entry.pack()
root.mainloop()

How do I make my button know what`s in the entry box and then print that value in a new window?

I want after you write something in the entry box and then you press the button a new window to pop up and
the number that was written in the entry box to be printed in the new window as " Your height is: "value" but after many tries I still don`t understand how to do it.
my code:
import tkinter as tk
root = tk.Tk()
root.geometry("250x130")
root.resizable(False, False)
lbl = tk.Label(root, text="Input your height", font="Segoe, 11").place(x=8, y=52)
entry = tk.Entry(root,width=15).place(x=130, y=55)
btn1 = tk.Button(root, text="Enter", width=12, height=1).place(x=130, y=85) #command=entrytxt1
root.mainloop()
This is what I got:
import tkinter as tk
root = tk.Tk()
root.resizable(False, False)
def callback():
# Create a new window
new_window = tk.Toplevel()
new_window.resizable(False, False)
# `entry.get()` gets the user input
new_window_lbl = tk.Label(new_window, text="You chose: "+entry.get())
new_window_lbl.pack()
# `new_window.destroy` destroys the new window
new_window_btn = tk.Button(new_window, text="Close", command=new_window.destroy)
new_window_btn.pack()
lbl = tk.Label(root, text="Input your height", font="Segoe, 11")
lbl.grid(row=1, column=1)
entry = tk.Entry(root, width=15)
entry.grid(row=1, column=2)
btn1 = tk.Button(root, text="Enter", command=callback)
btn1.grid(row=1, column=3)
root.mainloop()
Basically when the button is clicked it calls the function named callback. It creates a new window, gets the user's input (entry.get()) and puts it in a label.

Tkinter in fullscreen mode makes Text widget not work

When using Tkinter as a regular window, the text widget works as intended and can be typed in.
from Tkinter import *
root = Tk()
T = Text(root, height=2, width=30)
T.pack()
T.insert(END, "Just a text Widget\nin two lines\n")
mainloop()
However if we make this application fullscreen:
from Tkinter import *
root = Tk()
root.attributes("-fullscreen", True)
T = Text(root, height=2, width=30)
T.pack()
T.insert(END, "Just a text Widget\nin two lines\n")
mainloop()
And try to type in the textbox, it won't work and the keys will instead appear in the terminal. Why is this and is there a solutoin?
import tkinter as tk
from tkinter import *
root = tk.Tk()
root.overrideredirect(True)
root.overrideredirect(False)
root.attributes('-fullscreen',True)
canvas= Canvas(root)
canvas.create_text(300, 50,text="This is a window", fill="black", font=('Helvetica 15 bold'))
canvas.pack()
This seems to be working with me

Setting focus to specific TKinter entry widget

I'd like to set the focus of my program to a specific entry widget so that I can start entering data straight-away - how can I do this?
My current code
from Tkinter import *
root = Tk()
frame=Frame(root,width=100,heigh=100,bd=11)
frame.pack()
label = Label(frame,text="Enter a digit that you guessed:").pack()
entry= Entry(frame,bd=4)
entry.pack()
button1=Button(root,width=4,height=1,text='ok')
button1.pack()
root.mainloop()
Use entry.focus():
from Tkinter import *
root = Tk()
frame=Frame(root,width=100,heigh=100,bd=11)
frame.pack()
label = Label(frame,text="Enter a digit that you guessed:").pack()
entry= Entry(frame,bd=4)
entry.pack()
entry.focus()
button1=Button(root,width=4,height=1,text='ok')
button1.pack()
root.mainloop()
I tried this way and it is ok
entry= Entry(root)
entry.focus()
entry.pack

Categories