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
Related
I want to switch the screen using the tkinter module.
Is it possible to switch screens without frame?
This is the code I did.
#frame1.py
from tkinter import *
root=Tk()
root.title('page1')
def goto_p2():
root.destroy()
import frame2
Button(root, text="goto page1", command=goto_p2).pack()
mainloop()
.
#frame2.py
from tkinter import *
root=Tk()
root.title('page1')
def goto_p1():
root.destroy()
import frame1
Button(root, text="goto page2", command=goto_p1).pack()
mainloop()
Here you go, example code how it can be done.
import tkinter as tk
from tkinter import Button,Label
def clear_frame():
for widget in window.winfo_children():
widget.destroy()
def screen_two():
clear_frame()
button_2 = Button(window, text="Go to screen one", command=lambda: screen_one())
button_2.pack(pady=10)
label_2 = Label(window, text="Label on window two")
label_2.pack(pady=10)
def screen_one():
clear_frame()
button_1 = Button(window, text="Go to screen two", command=lambda: screen_two())
button_1.pack(pady=10)
label_1 = Label(window, text="Label on window one")
label_1.pack(pady=10)
window = tk.Tk()
window.title("Test")
window.geometry('250x100')
screen_one()
window.mainloop()
[EDITED]
Function clear_frame() will destroy every widget in frame ( basicly it will clear screen). Besides clear_frame(), there are two more functions, screen_two() and screen_one(). Both of those functions are doing the same thing, calling clear_frame() function, and displaying one button and one label. (Only difference is text inside label and text inside button).
When code runs, it will create window with geometry of 250x100. It will call screen_one() function and run mainloop().
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()
I am programming a menu into my first python tkinter menu window. I have done it all right (I think) but the menu doesn't appear on my tkinter window.
My code is:
from tkinter import *
def f1():
label = Label(window, text="Wassup CHUNGUS!!!")
label.grid(row=0, column=0, sticky=W)
global window
window = Tk()
window.title("CHUNGUS")
label2 = Label(window, text="!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
label2.grid(row=2, column=0, sticky=W)
menubar = Menu(window)
firstmenu = Menu(menubar, tearoff=0)
firstmenu.add_command(label="EXIT", command=window.destroy)
firstmenu.add_command(label="CHUNGUS", command =f1)
menubar.add_cascade(label="Menu", menu=firstmenu)
window.mainloop()
Can I have some help?
This is quite easy. You have not included window.config(menu=menubar). You should put it before the window.mainloop()
so:
window.config(menu=menubar)
window.mainloop()
Just put the code window.config(menu=menubar) before window.mainloop()
you forgot to config the code to menu if you understand window.config(menu=menubar)
and also a tip ig in this code you don't need global window if for this code im sayin
The simplest way to achieve a drop-down list within Tkinter is to use the OptionMenu widget built into tkinter.
from Tkinter import *
master = Tk()
variable = StringVar(master)
variable.set("one") # default value
w = OptionMenu(master, variable, "one", "two", "three")
w.pack()
mainloop()
from tkinter import *
root=Tk()
lab1=Label(root,text="Restaurant Management System")
lab1.config(font=("Courier", 44))
lab1.pack(side=TOP)
lab2=Label(root,text="Meals")
e2=Entry(root)
lab2.grid(row=0)
e2.grid(row=0,column=1)
root.mainloop()
both entry and label widget is not coming in the console when i use grid().But when i use pack() it appears.What is the reason? Im using python 3.7
You should probably avoid using grid and pack in the same tk container.
import tkinter as tk
if __name__ == '__main__':
root = tk.Tk()
lab1 = tk.Label(root, text="Restaurant Management System")
lab1.config(font=("Courier", 44))
lab1.grid(row=0, columnspan=2)
lab2 = tk.Label(root, text="Meals")
e2 = tk.Entry(root)
lab2.grid(row=1, column=0)
e2.grid(row=1, column=1)
root.mainloop()
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