Text widget tkinter - python

I have a bit of a problem. I have a text widget for user input instead of an entry widget and cannot seem to fetch the data from it. For example with an entry widget, you use .get() but what would you use for a text widget?
Thanks for your help...
Label(insertscreen, text="Please enter the Recipe ID").grid(row=0, sticky=W)
Label(insertscreen, text="Please enter the Recipe Name").grid(row=1, sticky=W)
Label(insertscreen, text="Please enter the Method of the Recipe").grid(row=2, sticky=W)
Label(insertscreen, text="Please enter the First Ingredient").grid(row=3, sticky=W)
Label(insertscreen, text="Please enter the Second Ingredient").grid(row=4, sticky=W)
Label(insertscreen, text="Please enter the Third Ingredient").grid(row=5, sticky=W)
Label(insertscreen, text="Please enter the Fourth Ingredient").grid(row=6, sticky=W)
Label(insertscreen, text="Please enter the Fifth Ingredient").grid(row=7, sticky=W)
Label(insertscreen, text="Please enter the Cooking Time, in minutes").grid(row=8, sticky=W)
Enter1= Entry(insertscreen)
Enter1.grid(row=0, column=1)
Enter2= Entry(insertscreen)
Enter2.grid(row=1, column=1)
Enter3= Text(insertscreen, width = 50, height = 10)
Enter3.grid(row=2, column=1)
Enter4= Entry(insertscreen)
Enter4.grid(row=3, column=1)
Enter5= Entry(insertscreen)
Enter5.grid(row=4, column=1)
Enter6= Entry(insertscreen)
Enter6.grid(row=5, column=1)
Enter7= Entry(insertscreen)
Enter7.grid(row=6, column=1)
Enter8= Entry(insertscreen)
Enter8.grid(row=7, column=1)
Enter9= Entry(insertscreen)
Enter9.grid(row=8, column=1)
#Entering the new recipe into the database
def submit_recipe():
global Enter1, Enter2, Enter3, Enter4, Enter5, Enter6, Enter7, Enter8, Enter9, new_db
ID = Enter1.get()
Name = Enter2.get()
Method = Enter3.get()
Ing1 = Enter4.get()
Ing2 = Enter5.get()
Ing3 = Enter6.get()
Ing4 = Enter7.get()
Ing5 = Enter8.get()
TimeofRec = Enter9.get()

The text widget uses get but as the widget manages multiple lines of text it has selectors to specify regions of the content. See the tkdocs site for a tutorial on the use of this widget and the manual page for the full details. An example to get the first line of text:
firstline = textWidget.get("1.0", "1.0 lineend")

The two most common ways to retrieve text from a Text widget are as follows:
1) grab all the text in the widget:
all_text = TextWidget.get('1.0', Tkinter.END)
2) grab the text that is currently selected:
ranges = TextWidget.tag_ranges(Tkinter.SEL)
if ranges:
selected_text = TextWidget.get(*ranges)

Related

tkinter print values on "enter" press

I need to print values on Screen When "Enter is pressed"
but not able to do so...
Currently its done with onClick method on button click...
How can Implement it?
I have tried
root.bind('<Return>', getvals) but didn't work i get error
return self.func(*args) TypeError: getvals() takes 0 positional
arguments but 1 was given
from tkinter import *
root = Tk()
def getvals():
print("Submitting form")
print(f"{namevalue.get(), phonevalue.get(), gendervalue.get(), emergencyvalue.get(), paymentmodevalue.get(), foodservicevalue.get()} ")
root.geometry("644x344")
#Heading
Label(root, text="Welcome to Harry Travels", font="comicsansms 13 bold", pady=15).grid(row=0, column=3)
#Text for our form
name = Label(root, text="Name")
phone = Label(root, text="Phone")
gender = Label(root, text="Gender")
emergency = Label(root, text="Emergency Contact")
paymentmode = Label(root, text="Payment Mode")
#Pack text for our form
name.grid(row=1, column=2)
phone.grid(row=2, column=2)
gender.grid(row=3, column=2)
emergency.grid(row=4, column=2)
paymentmode.grid(row=5, column=2)
# Tkinter variable for storing entries
namevalue = StringVar()
phonevalue = StringVar()
gendervalue = StringVar()
emergencyvalue = StringVar()
paymentmodevalue = StringVar()
foodservicevalue = IntVar()
#Entries for our form
nameentry = Entry(root, textvariable=namevalue)
phoneentry = Entry(root, textvariable=phonevalue)
genderentry = Entry(root, textvariable=gendervalue)
emergencyentry = Entry(root, textvariable=emergencyvalue)
paymentmodeentry = Entry(root, textvariable=paymentmodevalue)
# Packing the Entries
nameentry.grid(row=1, column=3)
phoneentry.grid(row=2, column=3)
genderentry.grid(row=3, column=3)
emergencyentry.grid(row=4, column=3)
paymentmodeentry.grid(row=5, column=3)
#Checkbox & Packing it
foodservice = Checkbutton(text="Want to prebook your meals?", variable = foodservicevalue)
foodservice.grid(row=6, column=3)
#Button & packing it and assigning it a command
Button(text="Submit to Harry Travels", command=getvals ).grid(row=7, column=3)
#enter press displays the value of the entry
# root.bind('<Return>', getvals)
root.mainloop()
Use:
def getvals(event=None):
and un-comment
root.bind('<Return>', getvals)
That's it ...
P.S. The 'trick' is to allow one parameter in getvals, but preset it with a default value in case there is no parameter passed to the function. This way can getvals be used for both button click and key-press.

Problems with tkinter grid layout in python

I'm doing the Angela Yu 100 days of code python course and im having trouble with the grid layout in tkinter package. I don't understand why my Entry objects aren't directly under each other. I used the same padding and size values as in the course and i tried to change them to look better but the password entry and the add button are more on the left then other entries while they have the same column number in the grid function. Thank u for ur help ^^
Here is the code i used for interface:
window = Tk()
window.title("Password Manager")
window.config(padx=50, pady=50)
logo = PhotoImage(file="logo.png")
image = Canvas(width=200, height=200, highlightthicknes=0)
image.create_image(100, 100, image=logo)
image.grid(column=1, row=0)
website = Label(text="Website:")
website.grid(column=0, row=1)
email = Label(text="Email/Username:")
email.grid(column=0, row=2)
password = Label(text="Password:")
password.grid(column=0, row=3)
website_blank = Entry(width=36)
website_blank.grid(column=1, row=1, columnspan=2)
website_blank.focus()
email_blank = Entry(width=36)
email_blank.grid(column=1, row=2, columnspan=2)
email_blank.insert(0, "aleksander.jaloszynski#gmail.com")
password_blank = Entry(width=21)
password_blank.grid(column=1, row=3)
generate_password = Button(text="Generate Password")
generate_password.grid(column=2, row=3)
add = Button(text="Add", width=36, command=save)
add.grid(column=1, row=4, columnspan=2)
window.mainloop()
And here is how it looks:
enter image description here

How to create a drop down list using tkinter and set it in a specific row/column? (Python)

I'm having some difficulties using tkinter to create a drop down list.
I'm not looking for creating a menu i want to make a little app to let me add new entries to an excel file.
Some of the entries are directly typed by the user but others must be selected from a drop down list.
I don't know how to set up this lists so that they appear at the right row/ column.
here is my code:
import tkinter as tk
window = tk.Tk()
window.title("Ajouter une nouvelle entrée")
window.geometry('400x350')
lbl1 = tk.Label(window, text="Numéro OF")
e1=tk.Entry()
lbl1.grid(column=0, row=0)
e1.grid(column=1,row=0)
lbl2 = tk.Label(window, text="Produit/carte")
e2=tk.Entry()
e2.grid(column=1,row=1)
lbl2.grid(column=0, row=1)
lbl3 = tk.Label(window, text="Famille")
lbl3.grid(column=0, row=2)
lbl4 = tk.Label(window, text="Test")
lbl4.grid(column=0, row=3)
lbl5 = tk.Label(window, text="Constats technique")
e5= tk.Entry()
e5.grid(column=1,row=4)
lbl5.grid(column=0, row=4)
lbl6 = tk.Label(window, text="Type de panne")
lbl6.grid(column=0, row=5)
lbl7 = tk.Label(window, text="Solutions")
e7= tk.Entry()
e7.grid(column=1,row=6)
lbl7.grid(column=0, row=6)
lbl8 = tk.Label(window, text="Composant changé")
e8= tk.Entry()
e8.grid(column=1,row=7)
lbl8.grid(column=0, row=7)
lbl9 = tk.Label(window, text="Qui?")
lbl9.grid(column=0, row=8)
lbl10 = tk.Label(window, text="Quand")
e10= tk.Entry()
e10.grid(column=1,row=9)
lbl10.grid(column=0, row=9)
lbl11 = tk.Label(window, text="Etat")
lbl11.grid(column=0, row=10)
lbl12 = tk.Label(window, text="Commentaires")
e12=tk.Entry()
e12.grid(column=1,row=11)
lbl12.grid(column=0, row=11)
window.mainloop()
you can see that the lables that have an entry to their right side don't need to be modified but for lbl3, lbl4, lbl6,lbl9, and lbl11 i need drop down lists.
for example for lbl3 i need a list in column 1 row 2 with this 4 entries:
'XS2444', 'XB3248', 'DEf23', DEf24' and 'DEf2500'.
You will see it more clearly when you open the window.
Moreover for lbl6 i have a list of more than 100 items to put in the drop down list, I don't know how to add them automatically so that i don't have to enter every item.
Thank you very much you will be truly helping!

type input from button for two different text input fields

I have 2 text input fields in my Tkinter program. I want to enter numbers into them using buttons, but when I give click the buttons to enter into one input field, it automatically gets printed in both input fields. I want to enter data separately based on where the cursor is
def btnClick(numbers):
global operator
operator=operator + str(numbers)
account_number_var.set(operator)
pin_number_var.set(operator)
function for button click
b1 = Button(win, text='1',font=('Helvetica 13 bold'), width=15, height=5,bd=5,command=lambda:btnClick(1))
b1.grid(row=40, column=0)
b2 = Button(win, text='2',font=('Helvetica 13 bold'), width=15, height=5,bd=5,command=lambda:btnClick(2))
b2.grid(row=40, column=1)
b3 = Button(win, text='3',font=('Helvetica 13 bold'), width=15, height=5,bd=5,command=lambda:btnClick(3))
I want to use condition like "if the cursor is in first text field, set there only and if in the second set only in 2nd text field."
account_number_var = tk.StringVar()
account_number_entry = tk.Entry(win, textvariable=account_number_var)
account_number_entry.focus_set()
pin_number_var = tk.StringVar()
account_pin_entry = tk.Entry(win, show('*'), text='PIN Number',textvariable=pin_number_var)
account_number_entry=Entry(win,textvariable=account_number_var,width=24,bd=8,insertwidth=1,relief=GROOVE,bg='powder blue')
account_number_entry.grid(row = 15, column = 1,ipady=5)
# Account pin entry here
account_pin_entry = Entry(win, textvariable=pin_number_var, width=22,show='*',bd=8,insertwidth=1,relief=GROOVE,bg='powder blue')
account_pin_entry.grid(row=15, column=2,padx=8,ipady=5)
b1 = Button(win, text='1',font=('Helvetica 13 bold'), width=15, height=5,bd=5,command=lambda:btnClick(1))
b1.grid(row=40, column=0)

How to make a python search with user entry case insensitive

Hi I have a python search which works well and then I have a tkinter GUI which accepts a user input. The user input is then used to query the database using python. It matches records together depending on the input for example it would match blue to blue when I need it to be able to match blue to Blue etc. Does anybody know how to do this, here is an example of what I have already(N.B it is a made up scenario);
def Search():
global E1, E2, E3, E4, file
#Open the file in read mode
file = sqlite3.connect('H:\\matching.db')
#Welcome message
print ("Please type in the information required")
window = Tk()
window.title ("Search for matches")
def QuitSearch():
window.destroy()
#Create text that will be shown to the user
window.configure(bg="red")
Label(window, text="Please enter the colour of your hair").grid(row=0)
Label(window, text="Please enter the colour of your eyes").grid(row=1)
Label(window, text="Please enter your gender").grid(row=2)
Label(window, text="Please enter your shoe size").grid(row=3)
#Create where the user will input
E1= Entry(window)
E2= Entry(window)
E3= Entry(window)
E4= Entry(window)
#Assigning the input boxes to area on the screen
E1.grid(row=0, column=1)
E2.grid(row=1, column=1)
E3.grid(row=2, column=1)
E4.grid(row=3, column=1)
button = Button(window, text = "Submit information", command=Submit, fg="yellow", bg="black")
button.grid(row=3, column=2, padx = 5)
quitbutton = Button (window, text ="QUIT", command=QuitSearch, fg ="red")
quitbutton.grid(row=3, column=3, padx=5)
#The submit function allows the data inserted by the user in Search to be submitted and to search the database
def Submit():
global E1, E2, E3, E4, file
#Retaining user input
eyecolour = E1.get()
haircolour = E2.get()
gender = E3.get()
shoesize = E4.get()
#The search
cursor = file.execute ('''SELECT ID, forename, surname, FROM people
WHERE eyecolour =? and haircolour=? and gender=? and shoesize=? ''', (eyecolour, haircolour, gender, shoezize))
window=Tk()
def QuitOutputScreen():
window.destroy()
for row_number, row in enumerate(cursor):
Label(window, text ="ID = "+ str(row[0])).grid(row=1, column = row_number)
Label(window, text ="Forename = "+str(row[1])).grid(row=2, column = row_number)
Label(window, text ="Surname = "+(row[2])).grid(row=3, column = row_number)
Label(window, text ="Search complete ").grid(column=11)
quitbutton = Button (window, text ="QUIT", command=QuitOutputScreen, fg ="red")
quitbutton.grid(row=11, column=11, padx=5)
file.close()
This looks more like a question that concerns the DBMS (sqlite, in this case), rather than Python itself.
You can solve the problem using case-insensitive search query in sqlite. You can find some explanation at How to set Sqlite3 to be case insensitive when string comparing? .
Within you python code you can store and search using all lower or upper case, by using the string lower and upper function or you could use regular expressions and re.IGNORE_CASE to do your searching.
For DB queries you can either store everything in a fixed case or tell the DB engine to ignore case.

Categories

Resources