How do I give focus to a python Tkinter text widget? - python

I'd like to be able to open the App GUI and have it automatically place the cursor into a particular text widget. Best case scenario is: as soon as the app is launched someone can start typing without having to click on the text widget. This is just a small example displaying the issue:
from Tkinter import *
root = Tk()
Window = Frame(root)
TextWidget = Text(Window)
TextWidget.pack()
Window.pack()
root.mainloop()

You use the focus_set method. For example:
from Tkinter import *
root = Tk()
Window = Frame(root)
TextWidget = Text(Window)
TextWidget.pack()
Window.pack()
TextWidget.focus_set()
root.mainloop()

Related

My browse window in tkinter goes under the toplevel tkinter window where topmost enabled

I will try to simplify my question here. My problem is like, on my first window of tkinter I have a button that opens another tkinter window. You can say it a second window to keep it on top I use win2.attributes('-topmost', True). Then I have a browse button to import file from the computer but when I click it goes under the window 2.
Following is my code.
from tkinter import *
from tkinter.filedialog import askopenfilename
root = Tk()
root.title("Base Window")
root.geometry("300x300")
def browse():
file_to_open = askopenfilename()
def new_window():
win2 = Toplevel(root)
win2.title("Window 2")
win2.geometry("300x300")
win2.attributes('-topmost', True)
Button2 = Button(win2, text="Browse", command=browse).pack()
button1 = Button(root, text="New window", command=new_window).pack()
root.mainloop()
My browse window is going under the window 2. Can you please suggest me the best way to keep it on top. I am sharing the screenshot of the problem as well
Starting code
Click on new window
Click browse button and the browse window is hidden
Cheers
You need to set the parent option of askopenfilename() to the toplevel window win2:
from tkinter import *
from tkinter.filedialog import askopenfilename
root = Tk()
root.title("Base Window")
root.geometry("300x300")
def browse(parent):
file_to_open = askopenfilename(parent=parent)
def new_window():
win2 = Toplevel(root)
win2.title("Window 2")
win2.geometry("300x300")
win2.attributes('-topmost', True)
# pass 'win2' to browse()
Button2 = Button(win2, text="Browse", command=lambda:browse(win2)).pack()
button1 = Button(root, text="New window", command=new_window).pack()
root.mainloop()

Opening a new window using a link in tkinter

How do I open a new window using a link in tkinter .
(For eg : in a login window i want to add a link that says "New user ? click here" and when I click on "click here" it takes me to the register window .
Please help me
enter image description here
[1]: https://i.stack.imgur.com/K5GV0.png
Please click the above link to see the image
Creating new toplevel windows works almost exactly the same as creating new widgets.
Toplevel windows are created using the Toplevel function:
t = Toplevel(parent)
Unlike regular widgets, you don't have to "Grid" a toplevel fo it to appear on screen. Once you've created a toplevel you can add children widgets within and grid them like in the main window. In other words toplevel behaves exactly like the automatic created root window.
To destroy a window use the method:
window.destroy()
You can open new windows in tkinter with the tkinter.Toplevel() command.
import tkinter as tk
class Gui:
"""Gui class"""
def __init__(self):
self.root = tk.Tk()
self.new_window = tk.Button(master=self.root, text="Open new window", width=20, pady=4, command=self.new_window)
self.new_window.pack()
self.root.mainloop()
def new_window(self):
"""Create a new top level window"""
new_window = tk.Toplevel()
tk.Label(master=new_window, text="This is a new window").pack()
if __name__ == '__main__':
Gui()
You can create a function to open a new window and then bind it to that Label, for example:
import tkinter as tk
def newWindow():
# Window object (top level)
newWindow = Toplevel(master)
# Title
newWindow.title("New Window 1")
# Geometry
newWindow.geometry("300x300")
root = tk.Tk()
label = tk.Label(text="Hello!", width=50, height=10, master=root)
label.pack()
label.bind("<Button-1>", newWindow)

Is there away to hide the text inside a button/label in Tkinter?

I'm a Python beginner that trying to learn my way through Tkinter, and I need your help.
Let say I create a simple button like this:
import tkinter as tk
window = tk.Tk()
button = tk.Button(text="Hello World!")
button.pack()
window.mainloop()
Is there a way that I can hide and then display the text again? Given that I can create two more buttons that will do the job of hiding and displaying. I have tried to use button.pack_forget(), but it will hide the entire button instead.
Any help would be appreciated.
To make it look like the buttons text vanishes,you can make the text color as same as the background color via cget method.
import tkinter as tk
def hide_text():
color = button['bg']
button.config(foreground=color, activeforeground=color)
window = tk.Tk()
button = tk.Button(text="Hello World!",command=hide_text)
button.pack()
window.mainloop()
Approach 2 ttk.Button:
import tkinter as tk
from tkinter import ttk
def hide_text():
button.config(text='')
window = tk.Tk()
button = ttk.Button(text="Hello World!",width=100,command=hide_text)
button.pack()
window.mainloop()
Approach3 using style:
import tkinter as tk
from tkinter import ttk
def change_button_style(event):
widget = event.widget
if widget['style'] == 'TButton':
widget.configure(style='VanishedText.TButton')
else:
event.widget.config(style='TButton')
BACKGROUND = '#f0f0f0'
FOREGROUND = '#000000'
window = tk.Tk()
window.configure(bg=BACKGROUND)
style = ttk.Style()
style.theme_use('default')
button = ttk.Button(text="Hello World!",style='VanishedText.TButton')
button.bind('<ButtonPress-1>',change_button_style)#,command=change_button_style
button.pack()
style.map('VanishedText.TButton',
foreground =[('disabled',BACKGROUND),
('!disabled',BACKGROUND),
('pressed',BACKGROUND),
('!pressed',BACKGROUND),
('active',BACKGROUND),
('!active',BACKGROUND)],
background =[('disabled',BACKGROUND),
('!disabled',BACKGROUND),
('pressed',BACKGROUND),
('!pressed',BACKGROUND),
('active',BACKGROUND),
('!active',BACKGROUND)],
focuscolor=[('disabled',BACKGROUND),
('!disabled',BACKGROUND),
('pressed',BACKGROUND),
('!pressed',BACKGROUND),
('active',BACKGROUND),
('!active',BACKGROUND)])
window.mainloop()
You Can Simply Config And Set Text :
import tkinter as tk
from tkinter import ttk
win=tk.Tk()
def hidetext():
button.config(text="")
button=ttk.Button(win,text="FooBar",command=hidetext)
button.pack()
win.mainloop()

How to prevent Toplevel() from closing both windows python?

I was testing the Toplevel() feature of python Tkinter and I was wondering if a user closes one window how do I keep the other one from closing as well?
from Tkinter import *
root = Tk()
top = Toplevel()
root.mainloop()
Calling transient, closing toplevel will not close the all windows.
from Tkinter import *
root = Tk()
top = Toplevel()
top.title('TopLevel window')
top.transient(root) # <------
root.mainloop()

How to create a multiline entry with tkinter?

Entry widgets seem only to deal with single line text. I need a multiline entry field to type in email messages.
Anyone has any idea how to do that?
You could use the Text widget:
from tkinter import *
root = Tk()
text = Text(root)
text.pack()
root.mainloop()
Or with scrolling bars using ScrolledText:
from tkinter import *
from tkinter.scrolledtext import ScrolledText
root = Tk()
ScrolledText(root).pack()
root.mainloop()
Just use Text() widget.
For example:
import tkinter as tk
root = tk.Tk()
text = tk.Text(root)
text.pack()
root.mainloop()
Output:

Categories

Resources