How can I disable keyboard input entries when using Entry on Tkinter in python
I was coding for a calculator project in python. So I need to make a screen like text box using Entry.
I couldn't remove keyboard inputs from the Entry field.
You can set the state of Entry widget to DISABLED.
Example:-
win = tk.Tk()
ent = Entry(win, state=DISABLED)
ent.pack()
you can disable keyboard characters from an Entry field in Tkinter using:
from tkinter import *
root=Tk()
txtDisplay = Entry(root, width=28, justify=RIGHT)
txtDisplay.grid(row=0, column=0, columnspan=5, pady=1)
txtDisplay.bind("<Key>", lambda e: "break") # Disable characters from keyboard
root.mainloop()
Related
I want to create hyperlink in order to open new window. Is it possible to make hyperlink to open new window in tkinter? And if it's possible, how to create it? Below this are my codes.
guidelines = Label(self, text="You can check the guidelines here", fg="blue", cursor="hand2")
guidelines.place(x=400,y=250)
guidelines.bind("<Button-1>", lambda e: callback())
Actually I've searched create hyperlink, but it's only for opening a web page. And there are no references to open new window in tkinter
You'll need to create a Toplevel window. Tkinter can't be compared to a browser, all content is inside some level of window of frame.
In your case, you want to have a new window, so this would be tkinter.Toplevel()
The Toplevel can be configured like the main window.
import tkinter as tk
def show_guide_lines(root):
window_guide_lines = tk.Toplevel(root)
window_guide_lines.title('Guide Lines')
window_guide_lines.geometry('400x200')
guide_line_text = '''
Some Guildelines
- 1
- 2
- 3
'''
tk.Label(window_guide_lines, text=guide_line_text).pack()
window_guide_lines.tkraise()
window_guide_lines.focus_force()
root = tk.Tk()
root.title('Main Window')
guidelines = tk.Label(root, text="You can check the guidelines here", fg="blue", cursor="hand2")
guidelines.pack(padx=20, pady=20)
guidelines.bind("<Button-1>", lambda e: show_guide_lines(root))
root.mainloop()
In my Python program somehwere in between i pop-up a Tkinter GUI to get user Input. User selects option from a Tkinter.ttk Combobox. From here once the user closes the Tkinter window i want the selection made by user to be used further in the code. But upon close unable to get the user selection back into the code.
Please help.
One way is to create a StringVar() in the root window and then associate it with the Combobox() in the Toplevel() window. The combobox will change the value of the StringVar() and you can read it affter popup window is closed.
from tkinter import *
from tkinter import ttk
root = Tk()
combotext = StringVar()
def get_input():
popup = Toplevel()
box = ttk.Combobox(popup, textvariable=combotext, state='readonly')
box['values'] = ("Camembert", "Brie", "Tilsit", "Stilton")
combotext.set('Choose')
box.pack(padx=20, pady=20)
Button(root, text='Get input', command=get_input).pack(padx=90, pady=10)
Label(root, textvariable=combotext).pack(pady=(0,10))
root.mainloop()
Is there any way to put the textvariable in another variable and not have to use the ".get()"? I've been doing a lot of sifting thorugh tutorials and articles for what I realize is a very small issue but I'm probably misunderstanding something pretty key so i'm hoping someone can help me develop some intuition for the entry widget and .get() method.
Below is part of a script that I've been working on where I want to take the text entered in the entry box and use it later. I can use it if I use search_word.get(), but I don't why I can't do something like New_variable=search_word.get() so that from that point on I can just use "New_variable".
import tkinter as tk
from tkinter import *
from tkinter import ttk
Text_input_window = Tk()
Text_input_window.geometry('600x350+100+200')
Text_input_window.title("Test")
label_1=ttk.Label(Text_input_window, text="Enter word to search:", background="black", foreground="white")
label_1.grid(row=1, column=0, sticky=W)
search_word=StringVar()
entry_1=ttk.Entry(Text_input_window,textvariable=search_word, width=40, background="white")
entry_1.grid(row=2, column=0, sticky=W)
New_variable=StringVar()
New_variable=search_word.get()
def click():
print(New_variable)
print(search_word.get())
Text_input_window.destroy()
btn_1=ttk.Button(Text_input_window, text="submit", width=10, command=click)
btn_1.grid(row=3, column=0, sticky=W)
Text_input_window.mainloop()
Problem is not .get() but how all GUIs works.
mainloop() starts program so new_variable = search_word.get() is executed before you even see window - so it tries to get text before you put text in Entry.
You have to do it inside click() which is executed after you put text in entry and click button.
import tkinter as tk
# --- functions ---
def click():
global new_variable # inform function to use external/global variable instead of creating local one
#new_variable = entry.get() # you can get it directly from `Entry` without StringVar()
new_variable = search_word.get()
root.destroy()
# --- main ---
new_variable = '' # create global variable with default value
root = tk.Tk()
search_word = tk.StringVar()
entry = tk.Entry(root, textvariable=search_word)
entry.pack()
btn = tk.Button(root, text="submit", command=click)
btn.pack()
root.mainloop() # start program
# --- after closing window ---
print('new_variable:', new_variable)
print('search_word:', search_word.get()) # it seems it still exists
# print('entry:', entry.get()) # `Entry` doesn't exists after closing window so it gives error
Is there any way to put the textvariable in another variable and not have to use the ".get()"?
No, there is not. Tkinter variables are objects, not values. Anytime you want to use a value from a tkinter variable (StringVar, IntVar, etc) you must call the get method.
I am trying to experiment and get the button to only display the label when the button is clicked instead it is opening up another GUI window. The main frame is called secret message. Within this when i click onto the button it should then replace the empty place with the label in row=2.
Could someone explain to me how i can raise the label rather than just opening up a new window. All code is functional but i want another way around this, i am new to python.
from tkinter import *
def topLevel():
top=Toplevel()
top.geometry("300x200")
root=Tk()
root.title("Secret Message")
button1 = Button(text="Push this button to see hidden message!", width =60, command=topLevel)
button1.grid(row=1, column=0)
label1 = Label(width=50, height=10, background="WHITE", text= "There is no secret!")
label1.grid(row=2, column=0)
root.mainloop()
You question title has nothing to do with your question.
To update the geometry of your label you simple need to tell the function where you want the label on the container you set up your label in. In this case you do not define the container so the widgets default to the root window.
Here is a working example that will update the label geometry when you press the button.
from tkinter import *
root=Tk()
root.title("Secret Message")
def grid_label():
label1.config(text="There is no secret!")
Button(root, text="Push this button to see hidden message!", width=60, command=grid_label).grid(row=1, column=0)
label1 = Label(root, width=50, height=10, background="WHITE")
label1.grid(row=2, column=0)
root.mainloop()
I found the below code :
def callback(ev):
ev.widget.select_range(0, 'end')
root = Tk()
t = Text(root, height=10, width=40)
t.pack()
t.bind('<Command-a>', callback) //WORKS for ENTRY
root.mainloop()
I'm basically trying to make cmd + a or Ctrl + a (Windows) work for Text in Tkinter.
Error (When I give the command : cmd-a in text):
'Text' object has no attribute 'select_range'
The code is ok except that you are inventing methods on the Text widget. However, if you look at the bindings on the widget class (Text) there are some virtual events defined
>>> '<<SelectAll>>' in root.bind_class('Text')
True
So in your handler for the keyboard event, use event_generate to raise a SelectAll virtual event.
import tkinter as tk
def select_all(ev):
ev.widget.event_generate('<<SelectAll>>')
root = tk.Tk()
txt = tk.Text(root)
txt.pack()
txt.bind('<Control-A>', select_all)
Text class does not have select_range() function, that is why you got that error message. But you can use bind_class() to bind events to the Text class widgets. Here is a dirty demo:
import tkinter as tk
def simulate_contral_a(e):
e.widget.tag_add("sel","1.0","end")
root = tk.Tk()
root.bind_class("Text","<Control-a>", simulate_contral_a)
T = tk.Text(root, height=2, width=30)
T.pack()
T.insert(tk.END, "Press Ctrl+a\nto select me\n")
root.mainloop()
Run this MCVE above and press Ctrl + a to see its effect: