Entry in notebook - Tkinter - python

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()

Related

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

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()

tkinter centering two or more widgets

So I created a frame in which I want to put two widgets with them being centered on the x-axis. When it's one object pack() centers it automatically. But I can't figure out two widgets. I tried with grid() but there is free space left on the right which makes it look unsymetrical as seen in the image.
how could I get what is seen on the right? (I'd prefer it being dont with pack() but if there is a solution with grid() and/or place() as well i'd appreciate those as well!)
here's the code for the left picture
from tkinter import *
from tkinter import font
root = Tk()
root.geometry("500x500")
frame = Frame(root, bg="white", highlightbackground="black", highlightthickness=2)
frame.place(relwidth=0.5, relheight=0.5, relx=0.5, rely=0.5, anchor=CENTER)
label = Label(frame, bg="lime", text="label", font=font.Font(size=20))
label.grid(column=0, row=0)
button = Button(frame, bg="yellow", text="pressbutton", font=font.Font(size=20))
button.grid(column=1, row=0)
root.mainloop()
You can use frame.pack() to easily position the frame in the top, middle of its parent.
from tkinter import *
from tkinter import font
root = Tk()
root.geometry("500x500")
frame = Frame(root, bg="white", highlightbackground="black", highlightthickness=2)
frame.pack()
label = Label(frame, bg="lime", text="label", font=font.Font(size=20))
label.grid(column=0, row=0)
button = Button(frame, bg="yellow", text="pressbutton", font=font.Font(size=20))
button.grid(column=1, row=0)
root.mainloop()
You can put the label and button in another frame, and use pack() on that frame:
from tkinter import *
from tkinter import font
root = Tk()
root.geometry("500x500")
frame = Frame(root, bg="white", highlightbackground="black", highlightthickness=2)
frame.place(relwidth=0.5, relheight=0.5, relx=0.5, rely=0.5, anchor=CENTER)
frame2 = Frame(frame)
frame2.pack() # default side='top'
label = Label(frame2, bg="lime", text="label", font=font.Font(size=20))
label.pack(side='left', fill='both')
button = Button(frame2, bg="yellow", text="pressbutton", font=font.Font(size=20))
button.pack(side='left')
root.mainloop()

Tkinter Frame Disappears after adding a button [duplicate]

This question already has an answer here:
How to stop Tkinter Frame from shrinking to fit its contents?
(1 answer)
Closed 4 years ago.
I have two frames in the root. And I want to add a button in one of the frames. Both frames have different background colours. When I try to add a button in any of them, the frame that contains the button disappears.
Without button
from tkinter import *
root = Tk()
root.geometry("1600x800+0+0")
root.title("ABC")
Rf = Frame(root, width=100, height=800, bg="black")
Rf.pack(side=RIGHT)
Lf = Frame(root, width=1500, height=800, bg="green")
Lf.pack(side=LEFT)
root.mainloop()
Which results in...
With button
But after adding the button, with the following code...
from tkinter import *
root = Tk()
root.geometry("1600x800+0+0")
root.title("ABC")
Rf = Frame(root, width=100, height=800, bg="black")
Rf.pack(side=RIGHT)
Lf = Frame(root, width=1500, height=800, bg="green")
Lf.pack(side=LEFT)
b1 = Button (Rf, text="Load", fg="red", bg="black")
b1.pack(side=LEFT)
root.mainloop()
I get...
Now the button is visible but the frame and background colours are gone. What am I doing wrong?
Thanks for your help!
Your frame is actually disappearing it is just resize and that is why you cant see it. Add Rf.pack_propagate(False) to your frame it will prevent the frame from resizing when a new widget is added.
from tkinter import *
root= Tk()
root.geometry("1600x800+0+0")
root.title("ABC")
Rf=Frame(root,width=100, height=800, bg="black")
Rf.pack_propagate(False)
Rf.pack(side=RIGHT)
Lf=Frame(root,width=1500, height=800, bg="green")
Lf.pack(side=LEFT)
b1 = Button (Rf, text= "Load", fg= "red", bg="black")
b1.pack(side=LEFT)
root.mainloop()

Spinbox getting vanished when i go to other frame and come back to the same

I have created a spinbox on frame1 and going to frame2 by clicking NEXT, From frame2 going back to frame 1 by clicking BACK button. By the time spinbox widget getting disappear. what could be the reason?
from Tkinter import *
def swap_frame(frame):
frame.tkraise()
root = Tk()
root.geometry("900x650+220+20")
root.title("Testing")
root.configure(borderwidth="1", relief="sunken",cursor="arrow",background="#BCC3B9",highlightcolor="black")
root.resizable(width=False, height=False)
frame2 = Frame(root, width=900, height=650)
frame1 = Frame(root, width=900, height=650)
#item 1 spinbox
Platform = Spinbox(values=("SX-16F", "SX-12VP", "SX-16VP", "VSRM-A", "NRNT-A", "FX-8", "DX-48V"), width="32")
Platform.place(x=500, y=200, relheight=0.05)
Button1=Button(frame1, text="Next", width =10, height= 2, bg= "#dbd8d7", command=lambda:swap_frame(frame2))
Button1.place(x=580, y=580)
Button3=Button(frame2, text="Back", width =10, height= 2, bg= "#dbd8d7", command=lambda:swap_frame(frame1))
Button3.place(x=580, y=580)
frame2.grid(row=0, column=0)
frame1.grid(row=0, column=0)
root.mainloop()
Pass the master to your Spinbox widget. It currently defaults to Tk window which is only initially lifted, hence it gets blocked by both frames when either one is lifted. Replace:
Platform = Spinbox(values=(...), ...)
with:
Platform = Spinbox(frame1, values=(...), ...)

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

Categories