How to make default text in entry that disappears [duplicate] - python

This question already has answers here:
Entry box text clear when pressed Tkinter
(2 answers)
Closed 6 years ago.
For instance:
from Tkinter import *
root = Tk()
e1 = Entry(root)
e1.insert(END, "ex. new file") #would like to make this text disappear when clicked
e1.grid(row=0, column=0)
root.mainloop()
Where the text "ex. newfile" disappears when clicked upon, leaving a blank entry field.

Create a boolean flag that monitors if the entry has been accessed; set it to False,
Bind "<Button-1>" to a function that clears the entry if it has not been accessed yet, and changes the flag to True.

Added
def delete_text(event):
if default_text:
e1.delete(0, END)
default_text = False
default_text = True
e1.bind("<Button-1>", delete_text)
Thanks to DYZ and effbot

Related

Tkinter Entry box default text [duplicate]

This question already has answers here:
Adding placeholders to tkinter Entry widget in a procedural way
(4 answers)
Closed 1 year ago.
How can I program a code in python Tkinter for entry box default text?
I want to add a default text that disappears as soon I click on the entry box.
To do that you simply would insert the default text into the Entry and then bind the Entry to getting focus and delete all that is in the entry and unbind the widget so that it doesn't delete everything in it the next time user clicks it.
import tkinter as tk
def clear_default(event):
event.widget.delete(0, 'end')
event.widget.unbind('<FocusIn>')
root = tk.Tk()
entry = tk.Entry()
entry.pack(padx=10, pady=10)
entry.insert(0, 'Default Text')
entry.bind('<FocusIn>', clear_default)
root.mainloop()
Tho if it is meant to be a default not just simply a placeholder, then you could instead of deleting when the widget receives focus, select all that is in the widget, so that when user starts typing the selection is deleted. This will be also helpful if the user wants to edit their entry:
entry.bind('<FocusIn>', lambda e: e.widget.select_range(0, 'end'))

Clicker game in python not working [duplicate]

This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 4 years ago.
I have the code off a clicker below and I'm trying to solve how to show to the number of clicks on the window in tkinter. I'm new to python. If I click on the button the number of clicks remains the same. I don't know if the text doesn't updates or if the increase() function doesn't work. Please help me solve this.
from tkinter import *
clicks = 0
def increase(clicks):
clicks += 1
root = Tk()
root.geometry('500x300')
label=Label(text="Clicks:")
show=Label(text=clicks)
btc = Button(text="Click me", command=increase(clicks))
label.pack()
show.pack()
btc.pack()
root.mainloop()
You need to set the label to have a variable of a particular kind called an IntVar()
Then use .set() and .get() to change the variable value and get its value. When it is changed then the label is automatically.
I suggest you have a look at this link.
Working Code:
from tkinter import *
def increase():
clicks.set(clicks.get() + 1)
root = Tk()
root.geometry('500x300')
label=Label(root, text="Clicks:")
clicks = IntVar()
show=Label(root, textvariable=clicks)
btc = Button(root, text="Click me", command=increase)
label.pack()
show.pack()
btc.pack()
root.mainloop()

How to hide password in Tkinter [duplicate]

This question already has answers here:
How to create a password entry field using Tkinter
(5 answers)
Closed 3 years ago.
def lock():
global pas,lk
lk=Tk.Frame(main)
lk.pack(fill="both", anchor="center", expand=1)
Tk.Label(lk, text="", font="arial 75").pack(anchor="center", side="top")
fpas=Tk.LabelFrame(lk, text="Zadejte heslo:")
fpas.pack(anchor="center")
pas=Tk.Entry(fpas)
pas.pack()
Tk.Button(lk, text="OK", command=check).pack(side="top")
Tk.Button(lk, text="Vypnout", command=lkend).pack(side="top")
return
This is a part of my code and I want to change it. When I write my password into Tk.Entry called pas, I don´t want to see numbers, but a only specific symbol, such as * or another symbol. Thanks for the answer.
P.S: Please don´t evaluate my code, I know that I can write it better, but I only ask for how to hide a password, not for my tkinter skills :-D
Just use the show option of the Entry widget:
pas = Tk.Entry(fpas, show='*')

How to open a new window by clicking a button using Tkinter in python? [duplicate]

This question already has answers here:
How can I open a new window when the user clicks the button?
(2 answers)
Closed 6 years ago.
I want to make a gui application in python for that I was making this type of code.
I have already tired many codes but i was not able to make it up to the requirement.
What's stopping you from doing it, please refer the original post here. But basic code:
import Tkinter as tk
def create_window():
window = tk.Toplevel(root)
root = tk.Tk()
b = tk.Button(root, text="Create new window", command=create_window)
b.pack()
root.mainloop()

Calling a function based on a Listbox current selection "curselection()" in Tkinter [duplicate]

This question already has answers here:
Getting a callback when a Tkinter Listbox selection is changed?
(3 answers)
Closed 9 years ago.
I have a listbox on a GUI in Tkinter. I would like to implement a routine where if a listbox item is selected a function is called (based on this selection) to modify the gui (add another adjacent listbox). Then if that selection changes, the gui reverts back to its default view. Can this be done? Seems you would need to associate a function to a listbox selection, not sure how to do this or if its possible... Does anyone have the secret?
Its possible to add "select" buttons to the bottom of my listbox, but I wanted to avoid this extra work for user and save space on the GUI.
Thanks to all in advance! Daniel
The listbox will fire the virtual event <<ListboxSelect>> whenever the selection changes. If you bind to it, your function will be called whenever the selection changes, even if it was changed via the keyboard.
Ok nevermind, the link below answers my question with the example below:
http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm
from Tkinter import *
root = Tk()
def callback(event):
print "clicked at", event.x, event.y
frame = Frame(root, width=100, height=100)
frame.bind("<Button-1>", callback)
frame.pack()
root.mainloop()
(replace frame with listbox widget of course)
Works !

Categories

Resources