How to insert a character in a tkinter Entry widget - python

I'm creating a Can Message prototype, the format for this it's something like this
xx.xx.xx.xx
The x's are characters from user input, and the points need to be inserted automatically. The user type the first 2 characters then a point it is inserted in the entry box, visually. Then the user type 2 more characters ...
I accomplished something with the insert method, but if I want to delete the point.. maybe I want to change the first 2 characters or whatever, I can't, it won't let me delete it, only if I use ctr+a.
This can message entry box it's from a larger program, but to make things easier, I tested just this one thing in a test file, below you can see.
your
from tkinter import *
root = Tk("")
def callback(*args):
if len(entrybox_var.get()) == 2:
entrybox.insert(2, ".")
entrybox_var = StringVar()
entrybox_var.trace("w", callback)
entrybox = Entry(root, textvariable= entrybox_var)
entrybox.grid()
root.mainloop()
text

Related

Can I display test options in a tkinter Combobox, but still have it specify values?

I have a Combobox that I want to have text options for, but when that option is chosen I want the integer value to be returned to the tk.IntVar value. can I do this?
selected_diff = tk.IntVar()
nps_difficulty_combo_s = ttk.Combobox(tab3,text = ("Easiest","Moderate","Moderately Strenuous","Strenuous",
"Very Strenuous"), value=[1,2,3,4,5], width=25,
textvariable=selected_diff)
I tried this, but it does not display the text as options.
Thank you.
What you want to do isn't supported by the ttk combobox. When you do text=("Easiest", ...), tkinter sees text as a shorthand for the textvariable option.
The simple solution is to create a mapping with a dictionary. You can then have the combobox display the keys of the dictionary, which you can later use to look up the value in the dictionary.
To solve this I just used the test variables instead of the numbers and changed the capture variable to a tk.StringVar().

Python Tkinter Entry List Impossible?

Is it utterly impossible to receive a list from user input in Tkinter? Something along the lines of an HTML textarea box - where a user can (1) copy and paste a list of things into a text box, and then (2) tkinter translates the input list into a list of strings, then (3) can assign them to a value and do fun python stuff etc
I have reasonable faith I can accomplish parts (2) and (3), but I'm stuck on (1).
I have explored Entry, which basically accomplishes that but awkwardly and with poor visibility onto the pasted items in the tiny Entry box. I have explored Listbox, which doesn't allow user input in the way of generating a new list from nothing?
The running example is: if I want to input some groceries into a variable, I can copy-paste a text list and paste as one item (rather than separately) --
eg: ["apples", "oranges", "raspberries"] clicks submit VS ["apples"] clicks submit ["oranges"] clicks submit ["raspberries"] clicks submit
-- Anyone have any recommendations for that elusive textarea-like input box for tkinter? Do I just wrestle with the Entry tiny box?
You want a tkinter.Text
import tkinter as tk
# proof of concept
root = tk.Tk()
textarea = tk.Text(root)
textarea.pack()
root.mainloop()
You can retrieve the text with textarea.get in the normal way
result = textarea.get(1.0, 'end') # get everything
result = textarea.get(1.0, 'end-1c') # get exactly what the user entered
# (minus the trailing newline)

How do I correctly check if text box is empty or not?

When I enter nothing in the text and click enter, it counts the length of the text as 1, which means there is something there. (Presumably a space), and entering an actual space in the text box increases the count by 1. I want to know how to check if the textbox is empty or not. If nobody enters anything in the textbox I want a way to see that.
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
def submitted(*args): #Progressbar is set to be fully completed and states information recorded
print(len(t.get("1.0", END)))
if len(t.get("1.0", END))==1:
messagebox.showinfo("Error", "No information entered in description.")
#Sets title and creates gui
root=Tk()
#Creates text widget
t = Text(root, width=20, height=10)
t.grid(column=0,row=1)
#Submitting calls submitted function to set progressbar to 100 and statemessage box has been completed
subbttn= ttk.Button(root, text="Submit", command=submitted)
subbttn.grid(column=1, row=1, sticky=(S, W, E))
If you print out the content of the empty text box, you will see that there is a "\n", newline character. This is what is causing you to see that there is a length of 1 for an empty text box.
If you wanted to see if it was empty, you could check if there is just one new line. Or, like you are already doing, check if len(t.get("1.0", END) is one.
if t.get("1.0", END)=="\n":
instead of:
if len(t.get("1.0", END))==1:
I am working on a python program and none of the above solutions works for me except this one: len(t.get("1.0", END))>=1. It spots the existence or not of one or more characters in the text box.

Tkinter Find and Replace Function for Notepad

I'm new at Tkinter, and python. I've been experimenting with a notepad script I've made. I'm working on a find / replace command. But I've had no luck. Here is what I've tried so far:
def replace():
def replaceall():
findtext = str(find.get(1.0, END))
replacetext = str(replace.get(1.0, END))
alltext = str(text.get(1.0, END))
alltext1 = all.replace(findtext, replacetext)
text.delete(1.0, END)
text.insert('1.0', alltext1)
replacebox =Tk()
replacebox.geometry("230x150")
replacebox.title("Replace..")
find = Text(replacebox, height=2, width=20).pack()
replace = Text(replacebox, height=2, width=20).pack()
replaceallbutton = Button(replacebox, text="Replace..", command=replaceall)
replaceallbutton.pack()
(this is just the function I am defining for the replace command)
The 'text' variable is on the large canvas which contains the menu's and the main text widget.
Any help is appreciated
So far I've been creating this notepad in 2.7.8, so the Tkinter import is 'Tkinter.'
What I'm shooting for is having the first box have the text to find and the second box have the text to be replaced. Upon pressing the replace button, the function replaceall() should begin.
Are there any obvious mistakes in my function, or is it just deeply flawed? Any help is appreciated.
The most obvious mistake is that you are creating a second instance of Tk. If you need a popup window you should create an instance of Toplevel. You should always have exactly one instance of Tk running.
The second problem is related to the fact you are using a Text widget for the find and replace inputs. When you do a get with a second index of END, the string you get back will always have a newline whether the user entered one or not. If you want exactly and only what the user typed, use "end-1c" (end minus one character).
Finally, there's no reason to get all the text, replace the string, and then re-insert all the text. That will work only as long as you have no formatting or embedded widgets or images in the text widget. The text widget has a search command which can search for a pattern (either string or regular expression), and you can use the returned information to replace the found text with the replacement text.

updating a text widget with text from entry widgets - python

I am using tkinter with python to produce a quote gui. I have 3 entry widgets (code, price and quantity) and a "Add Line" button. When the button is pressed, I want it to take the text from each of the entry widgets and update it in the scrollable "text" widget that is located in the same window (so the user can review each line in the overall quote before submitting the final product). Any ideas?
For info, once the text widget is full of all the lines that the user wants to include in the quote, pressing another button (eg Submit) will write all of the lines into a preformatted Word document. I have his part sorted, but cannot find how to do the first part above. Any ideas.
To get the values from an Entry widget, use the get method. To insert values into a text widget use the insert method. These are both documented, and there are examples all over the internet.
def add_line():
code = codeEntry.get()
price = priceEntry.get()
quantity = quantityEntry.get()
quote.insert("end","code: %s price: %s quantity: %s\n" % (code,price,quantity))

Categories

Resources