Make Tkinter run a command without clicking or pressing a button - python

I am making a simple tkinter popup where you can type a message.
In the textbox itself, I inserted the text "Type your message here" with a grey colour and when clicked, the inserted text is deleted so the user can type in their own message. In addition, the colour of the text typed by the user is set to black.
However, when I was testing I realised that this will only happen if they click the textbox with a mouse button. My question is, is there a way for tkinter to automatically run a command when a condition is changed? For example, if the textbox is empty, the font colour should be set to black.
I tried putting if-statements in the tk.mainloop, but sadly that didn't work.
Any ideas?
this is my (hopefully) simplified version of the code:
from tkinter import *
def changecolor(event):
if textbox.get("1.0", "end-1c") == "Type your message here":
textbox.delete("1.0", "end")
textbox.config(fg='black')
root = Tk()
canvas = Canvas(root, height=400, width=600)
canvas.pack()
textbox = Text(canvas, font=40, fg="grey")
textbox.insert(1.0, "Type your message here")
textbox.bind("<Button-1>", changecolor)
textbox.pack()
root.mainloop()
~finally found out how to format code here.

Take a look at this class I created that does similar to what your code does, do try it on.
from tkinter import *
class PlaceholderText(Text):
def __init__(self,master,placeholder,placeholdercolor='black',fg='grey',**kwargs):
Text.__init__(self,master,**kwargs) #init the text widget
self.placeholder = placeholder
self.fgcolor = fg
self.placeholdercolor = placeholdercolor
self.has_placeholder = False #make flag
self.add() #run the function to add placeholder
self.bind('<FocusIn>',self.clear) #binding to focusin and not button-1
self.bind('<FocusOut>',self.add) #function wil get triggered when widget loses focus
def clear(self,event=None):
if self.get('1.0','end-1c') == self.placeholder and self.has_placeholder: #condition to clear a placeholder
self.delete('1.0','end-1c') #delete the placeholder
self.config(fg=self.fgcolor) #change the color
self.has_placeholder = False #set flag to flase
def add(self,event=None):
if self.get('1.0','end-1c') == '' and not self.has_placeholder: #condition to add placeholder
self.insert('1.0',self.placeholder) #add placeholder
self.has_placeholder = True #set flag to true
self.config(fg=self.placeholdercolor) #change text color to what you specify?
def ret(self,index1,index2):
if self.get('1.0','end-1c') == self.placeholder and self.has_placeholder: #gives none if there is nothing in the widget
return 'None'
else:
return self.get(index1,index2) #else gives the text
root = Tk()
pl = PlaceholderText(root,placeholder='Type something here...')
pl.pack()
e = Entry(root) #dummy widget to switch focus and check
e.pack(padx=10,pady=10)
root.mainloop()
I've explained to through the comments. But keep in mind its not the best of classes yet, you have to do add a lot more methods in to make it more efficient.
Just if your wondering on how to do this without classes, then:
from tkinter import *
has_placeholder = False #make flag
placeholder = 'Type Something Here...' #the text to be inserted
def clear(event=None):
global has_placeholder
if a.get('1.0','end-1c') == placeholder and has_placeholder: #condition to clear a placeholder
a.delete('1.0','end-1c') #delete the placeholder
a.config(fg='grey') #change the color
has_placeholder = False #set flag to flase
def add(event=None):
global has_placeholder
if a.get('1.0','end-1c') == '' and not has_placeholder: #condition to add placeholder
a.insert('1.0',placeholder) #add placeholder
has_placeholder = True #set flag to true
a.config(fg='black') #change text color to normal
root = Tk()
a = Text(root)
a.pack()
add() #add the placeholder initially
a.bind('<FocusIn>',clear) #binding to focus and not button-1
a.bind('<FocusOut>',add)
e = Entry(root) #dummy widget to show focus loss
e.pack()
root.mainloop()
Why not to use classes if the latter method is more easier? This is not reusable, say you want to add one more Text widget, that cannot have such property, while using a custom class with custom class you can have as many as text widgets with same properties you like.
Do let me know if any doubts.

You can simply add a <Key> binding to your text widget and use your changecolor function to determine what state your textbox is in.
#Give a hoot. Don't pollute. :D
import tkinter as tk
txtmsg = "Type your message here"
def changecolor(event):
text = textbox.get("1.0", "end-1c")
#customize accordingly
if text:
if text == txtmsg:
print("text is txtmsg")
else:
print("text is user text")
else:
print("text is empty")
#FYI:
#whether this was a button press or key press DOES NOT have string equality
#if you need to create button vs key conditions
#use tk.EventType.ButtonPress and tk.EventType.KeyPress
#or learn the .value and compare that
print(event.type, type(event.type), event.type.value)
root = tk.Tk()
textbox = tk.Text(root, font=40, fg="grey")
textbox.insert(1.0, txtmsg)
textbox.pack()
#add events
for ev in ['<Key>', '<1>']:
textbox.bind(ev, changecolor)
root.mainloop()

Related

Constantly checking a text box in tkinter

I want a Text label in tkinter to constantly check if its worth some value. I would like it to be in a while loop and exit it when the values match.
My code doesn't work.
while user_input != str(ans):
master = Tk()
master.title("Math")
master.geometry('400x400')
eq = generate_equation(stage=current_stage)
ans = calc(eq)
Label(master=master,text=f"score: {score}").grid(row=0,column=2,sticky=W)
Label(master=master, text=f"{q_num}: {eq}").grid(row=0,column=0 ,sticky=W)
inputtxt = tkinter.Text(master=master,height = 5, width = 20)
inputtxt.grid()
user_input =str(inputtxt.get(1.0,"end-1c"))
mainloop()
Try this:
import tkinter as tk
def check(event:tk.Event=None) -> None:
if text.get("0.0", "end").strip() == "answer":
# You can change this to something else:
text.insert("end", "\n\nCorrect")
text.config(state="disabled", bg="grey70")
root = tk.Tk()
text = tk.Text(root)
# Each time the user releases a key call `check`
text.bind("<KeyRelease>", check)
text.pack()
root.mainloop()
It binds to each KeyRelease and checks if the text in the text box is equal to "answer". If it is, it displays "Correct" and locks the text box, but you can change that to anything you like.
Please note that this is the simplest answer and doesn't account for things like your code adding things to the text box. For that you will need more sophisticated code like this.

How do I enable right click in entry and output widget for pasting and copying respectively in Tkinter?

I'm still working on the translator app, with the help of Python Dictionary. But I have this challenge: I want to be able to right click in the entry widget and paste keys as well as right click in the output widget and copy values. I'm only able to do so with keyboard shortcut; for convenience sake, I want to be able to do so with the mouse. Thanks. Below is the code:
from tkinter import *
import tkinter. messagebox
root=Tk()
root.geometry('250x250')
root.title("Meta' Translator")
root.configure(background="#35424a")
from playsound import playsound
#Entry widget object
textin = StringVar()
#press ENTER key to activate translate button
def returnPressed(event):
clk()
def clk():
entered = ent.get().lower() #get user input and convert to lowercase
output.delete(0.0,END)
if len(entered) > 0:
try:
textin = exlist[entered]
except:
textin = 'Word not found'
output.insert(0.0,textin)
def play():
text = output.get("0.0", "end").strip("\n")
if text == "əsɔ́":
playsound("hoe.mp3")
elif text == "jam":
playsound("axe.mp3")
elif text == "ɨghə́":
playsound("eye.mp3")
else:
# If there is no sound file for the translation:
playsound("eze.mp3")
#heading
lab0=Label(root,text='Translate English Words to Meta\'',bg="#35424a",fg="silver",font=
('none 11 bold'))
lab0.place(x=0,y=2)
#Entry field
ent=Entry(root,width=15,font=('Times 18'),textvar=textin,bg='white')
ent.place(x=30,y=30)
#focus on entry widget
ent.focus()
#Search button
but=Button(root,padx=1,pady=1,text='Translate',command=clk,bg='powder blue',font=('none 18
bold'))
but.place(x=60,y=90)
#press ENTER key to activate Translate button
root.bind('<Return>', returnPressed)
#output field
output=Text(root,width=15,height=1,font=('Times 18'),fg="black")
output.place(x=30,y=170)
#play button
play_button=Button(root,padx=1,pady=1,text='Play',command=play,bg='powder blue',font=('none
10 bold'))
play_button.place(x=100,y=210)
#prevent sizing of window
root.resizable(False,False)
#Dictionary
exlist={
"hat":"ɨ̀də̀m",
"hoe":"əsɔ́",
"honey":"jú",
"chest":"ɨgɔ̂",
"eye":"ɨghə́",
"ear":"ǝ̀tǒŋ",
"axe":"jam"
}
root.mainloop()
Since your code has a lot of dependency, it cannot be run on another system, so here is a common example which you should be able to implement to your code easily:
from tkinter import *
root = Tk()
def popup(event):
try:
menu.tk_popup(event.x_root,event.y_root) # Pop the menu up in the given coordinates
finally:
menu.grab_release() # Release it once an option is selected
def paste():
clipboard = root.clipboard_get() # Get the copied item from system clipboard
e.insert('end',clipboard) # Insert the item into the entry widget
def copy():
inp = e.get() # Get the text inside entry widget
root.clipboard_clear() # Clear the tkinter clipboard
root.clipboard_append(inp) # Append to system clipboard
menu = Menu(root,tearoff=0) # Create a menu
menu.add_command(label='Copy',command=copy) # Create labels and commands
menu.add_command(label='Paste',command=paste)
e = Entry(root) # Create an entry
e.pack(padx=10,pady=10)
e.bind('<Button-3>',popup) # Bind a func to right click
root.mainloop()
I have explained it with comments to understand on-the-go, nothing complicated. The menu just pops up when you right click on the entry as the function is binded to the entry alone. I think, without clipboard_clear() it would append all the items in the tkinter clipboard to the system clipboard.

Prompt user for input if item on drop-down list is selected in Python

I have a GUI in python that, amongst other things, let's the user choose items from a dropdown menu (I used the combobox feature from tkinter).
I wish to have it so that when the item "Custom" is select, an input box appears to ask the user what their custom number would be. I don't want to have to use a button for the box to appear, but somehow have it so that as soon as custom is selected, the input box appears. I tried first with while loops but I can't get it to work, same with if statements :s
I also tried using the askinteger() method that I found here (http://effbot.org/tkinterbook/tkinter-entry-dialogs.htm) but none of the combinations I came up with worked (I am very new at Python and am still learning so excuse my obvious mistakes).
Here is my code for the GUI :
from tkinter import *
from tkinter.ttk import *
from tkinter import filedialog
from tkinter import StringVar
from tkinter import messagebox
from tkinter import simpledialog
class MyGUI:
def __init__(self, master):
self.master = master
master.title("Intent/Interpretation Check")
self.runlabel = Label(master, text="RunID :")
self.runlabel.grid(row=0, column=0)
self.runentry = Entry(master)
self.runentry.grid(row=1, column=0, padx=25)
self.checklabel = Label(master, text="Check type :")
self.checklabel.grid(row=0, column=1)
self.typeselect = Combobox(master)
self.typeselect['values']=("Intent Score", "Interpretation Score")
self.typeselect.grid(row=1, column=1, padx=25)
self.limitlabel = Label(master, text="Fails if score is below :")
self.limitlabel.grid(row=0, column=2, padx=25)
self.limitselect = Combobox(master)
self.limitselect['values']=(1000, 5000, "Custom")
self.limitselect.grid(row=1, column=2, padx=25)
if self.limitselect.get() != "Custom":
self.limit = self.limitselect.get()
pass
else:
self.askinteger("Custom limit", "Please enter a number from 1 to 10000", minvalue=1, maxvalue=10000)
self.submitbutton = Button(master, text="Submit", command=self.checkstatus)
self.submitbutton.grid(row=1, column=3, padx=25, pady=5)
root = Tk()
root.geometry("+600+300")
my_gui = MyGUI(root)
root.mainloop()
Thank you very much in advance !
You need to have a Bool that tells when to show the new input should be shown.
You also need to be constantly polling the ComboBox to see if it's value is equal to "Custom". This is what I came up with in about 3 minutes.
I didn't try to make the GUI look pretty, just a functional example.
from tkinter import *
from tkinter.ttk import *
class Gui:
def __init__(self):
self.root = Tk()
# Set up the Combobox
self.selections = Combobox(self.root)
self.selections['values'] = ['Apples', 'Oranges', 'Blueberries', 'Bananas', 'Custom']
self.selections.pack()
# The Entry to be shown if "Custom" is selected
self.custom_field = Entry(self.root)
self.show_custom_field = False
# Check the selection in 100 ms
self.root.after(100, self.check_for_selection)
def check_for_selection(self):
'''Checks if the value of the Combobox equals "Custom".'''
# Get the value of the Combobox
value = self.selections.get()
# If the value is equal to "Custom" and show_field is set to False
if value == 'Custom' and not self.show_custom_field:
# Set show_field to True and pack() the custom entry field
self.show_custom_field = True
self.custom_field.pack()
# If the value DOESNT equal "Custom"
elif value != 'Custom':
# Set show_field to False
self.show_custom_field = False
# Destroy the custom input
self.custom_field.destroy()
# Set up a new Entry object to pack() if we need it later.
# Without this line, tkinter was raising an error for me.
# This fixed it, but I don't promise that this is the
# most efficient method to do this.
self.custom_field = Entry(self.root)
# If the value IS "Custom" and we're showing the custom_feild
elif value == 'Custom' and self.show_custom_field:
pass
# Call this method again to keep checking the selection box
self.root.after(100, self.check_for_selection)
app = Gui()
app.root.mainloop()
Hope this helps!
EDIT:
To open a new window instead of packing it inside the same window as the Combobox, replace the function check_for_selection with this:
def check_for_selection(self):
value = self.selections.get()
# If the value is equal to "Custom" and show_field is set to False
if value == 'Custom' and not self.show_custom_field:
# Set show_field to True and pack() the custom entry field
self.show_custom_field = True
# Create a new window how we did when we made self.root
self.new_window = Tk()
# Create the Entry that will go in the window. The previous Entry widget from line 16, can be removed
self.custom_field = Entry(self.new_window)
self.custom_field.pack()
# Run the new window like we did the original
self.new_window.mainloop()
# If the value DOESNT equal "Custom"
elif value != 'Custom':
# Destroy the new window that was created if it exists
if self.show_custom_field:
self.new_window.destroy()
# Set show_field to False
self.show_custom_field = False
# If the value IS "Custom" and we're showing the custom_feild
elif value == 'Custom' and self.show_custom_field:
print('yes')
# Call this method again to keep checking the selection box
self.root.after(100, self.check_for_selection)

How to change Tkinter label text on button press

I have this code, and its meant to change the text of the Instruction label when the item button is pressed. It doesn't for some reason, and I'm not entirely sure why. I've tried creating another button in the press() function with the same names and parameters except a different text.
import tkinter
import Theme
import Info
Tk = tkinter.Tk()
message = 'Not pressed.'
#Sets window Options
Tk.wm_title(Info.Title)
Tk.resizable(width='FALSE', height='FALSE')
Tk.wm_geometry("%dx%d%+d%+d" % (720, 480, 0, 0))
#Method run by item button
def press():
message = 'Button Pressed'
Tk.update()
#item button
item = tkinter.Button(Tk, command=press).pack()
#label
Instruction = tkinter.Label(Tk, text=message, bg=Theme.GUI_hl2, font='size, 20').pack()
#Background
Tk.configure(background=Theme.GUI_bg)
Tk.mainloop()
Doing:
message = 'Button Pressed'
will not affect the label widget. All it will do is reassign the global variable message to a new value.
To change the label text, you can use its .config() method (also named .configure()):
def press():
Instruction.config(text='Button Pressed')
In addition, you will need to call the pack method on a separate line when creating the label:
Instruction = tkinter.Label(Tk, text=message, font='size, 20')
Instruction.pack()
Otherwise, Instruction will be assigned to None because that is the method's return value.
You can make message a StringVar to make callback.
message = tkinter.StringVar()
message.set('Not pressed.')
You need to set message to be a textvariable for Instruction:
Instruction = tkinter.Label(Tk, textvariable=message, font='size, 20').pack()
and then
def press():
message.set('Button Pressed')

Simple gui that display a repeated set of images when a button is pushed

I made a very simple gui that has a button and shows an image(.gif). My goal is to output another .gif whenever you press the button. There are 2 .gif files in my file directory and the point is to keep switching between these two whenever you press the button.
#Using python2.7.2
import Tkinter
root = Tkinter.Tk()
try:
n
except:
n = 0
def showphoto(par):
if par%2 == 0:
try:
label2.destroy()
except:
pass
photo = Tkinter.PhotoImage(file="masc.gif")
label2 = Tkinter.Label(image=photo)
label2.image = photo
label2.pack()
else:
try:
label2.destroy()
except:
pass
photo = Tkinter.PhotoImage(file="123.gif")
label2 = Tkinter.Label(image=photo)
label2.image = photo
label2.pack()
myContainer1 = Tkinter.Frame(root, width = 100, height = 100)
myContainer1.pack()
def callback(event):
global n
showphoto(n)
n = n + 1
button1 = Tkinter.Button(myContainer1)
button1["text"]= "Next pic"
button1["background"] = "green"
button1.bind("<Button-1>", callback(n))
button1.pack()
root.mainloop()
The current code just outputs the first image (masc.gif) but when I press the button it doesn't switch to the other image(123.gif). What am I doing wrong?
This can achieved much easier with classes as the class holds all the data necessary without the use of global variables.
import Tkinter as tk
from collections import OrderedDict
class app(tk.Frame):
def __init__(self,master=None, **kwargs):
self.gifdict=OrderedDict()
for gif in ('masc.gif','123.gif'):
self.gifdict[gif]=tk.PhotoImage(file=gif)
tk.Frame.__init__(self,master,**kwargs)
self.label=tk.Label(self)
self.label.pack()
self.button=tk.Button(self,text="switch",command=self.switch)
self.button.pack()
self.switch()
def switch(self):
#Get first image in dict and add it to the end
img,photo=self.gifdict.popitem(last=False)
self.gifdict[img]=photo
#display the image we popped off the start of the dict.
self.label.config(image=photo)
if __name__ == "__main__":
A=tk.Tk()
B=app(master=A,width=100,height=100)
B.pack()
A.mainloop()
Of course, this could be done more generally ... (the list of images to cycle through could be passed in for example), and this will switch through all the images in self.gifs ...
This approach also removes the necessity to destroy and recreate a label each time, instead we just reuse the label we already have.
EDIT
Now I use an OrderedDict to store the files. (keys=filename,values=PhotoImages). Then we pop the first element out of the dictionary to plot. Of course, if you're using python2.6 or earlier, you can just keep a list in addition to the dictionary and use the list to get the keys.
button1 = Tkinter.Button(myContainer1)
button1["text"]= "Next pic"
button1["background"] = "green"
button1.bind("<Button-1>", callback(n))
First, you bind the <Button-1> event to None (that's what callback(n) evaluates to). You should bind it to callback (no parentheses a.k.a the call operator).
Second, I suggest you change callback to not accept any arguments, remove the bind call and create your button as:
button1 = Tkinter.Button(myContainer1, command=callback)

Categories

Resources