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.
Related
How to disable the button in tkinter window when two passwords does not match?
My work:
from tkinter import *
from functools import partial
root = Tk()
root.geometry('280x100')
root.title('Tkinter Password')
def validation_pw(ep,cp):
if ep.get() == cp.get():
Label(root, text = "Confirmed").grid(column=0, row=5)
else:
Label(root, text = "Not matched").grid(column=0, row=5)
# check_button['state'] = DISABLED <============================
ep = StringVar()
cp = StringVar()
Label1 = Label(root, text = "Enter Password").grid(column=0, row=0)
pwEnty = Entry(root, textvariable = ep, show = '*').grid(column=1, row=0)
# Confirmed password label
Label2 = Label(root, text = "Confirm Password").grid(column=0, row=1)
pwconfEnty = Entry(root, textvariable = cp, show = '*').grid(column=1, row=1)
validation_pw = partial(validation_pw, ep,cp)
check_button = Button(root, text = "check", command = validation_pw).grid(column=0, row=4)
root.mainloop()
It shows if two passwords are not matched.
Now, I want to disable the check button if two passwords are not matched. I want the user cannot try the passwords anymore if failure.
So in the function validation_pw, I add check_button['state'] = DISABLED.
However, an error pops out
TypeError: 'NoneType' object does not support item assignment
How to fix this issue? Thanks!
Your checkbutton is actually None, because it's the result of the grid function.
To fix it, first declare the button, next grid it.
Before:
check_button = Button([...]).grid(column=0, row=4) # Result of Button.grid function
print(check_button) # None
After:
check_button = Button([...])
check_button.grid(column=0, row=4)
print(check_button) # Button object ...
You get the error of NoneType because at one point it was assigned nothing. This is because you used .grid on the same line as the button.
Fixed code:
check_button = Button(root, text = "check", command = validation_pw)
check_button.grid(column=0, row=4)
I'm trying to create a function in tkinter where I can print out what the user writes in a Entry box. I'm able to print out ask_an_entry_get, but when I try to print what_is_answer_entry_get
, I get nothing my empty spaces.
Please find out the problem here. Also I'm using the Entry widget, along with the get() function, to get input from the user.
def answer_quizmaker_score():
print(ask_an_entry_get)
print(what_is_answer_entry_get)
I made a lot of global variables so I could use them all around my code.
global what_is_answer_entry
what_is_answer_entry = Entry(root4)
what_is_answer_entry.pack()
I then used the get() function to retrieve what the user typed.
global what_is_answer_entry_get
what_is_answer_entry_get = what_is_answer_entry.get()
This is the exact process I did for both ask_an_entry_get and what_is_answer_entry_get. However for some reason only ask_an_entry_get is printed, while what_is_answer_entry_get is printing nothing in the console.
from tkinter import *
root = Tk()
root.geometry("500x500")
txt1 = StringVar()
txt2 = StringVar()
def txt_printer():
print(txt1.get())
print(txt2.get())
x = Entry(root, textvariable=txt1, width=20)
x.place(x=0, y=0)
y = Entry(root, textvariable=txt2, width=20)
y.place(x=0, y=50)
btn_print = Button(root, text="print", command=txt_printer)
btn_print.place(x=0, y=100)
# Or if you want to show the txt on window then:
def txt_on_window():
lb1 = Label(root, text=txt1.get())
lb1.place(x=0, y=200)
lb2 = Label(root, text=txt2.get())
lb2.place(x=0, y=235)
btn_print_on_window = Button(root, text="print on screen", command=txt_on_window)
btn_print_on_window.place(x=0, y=150)
root.mainloop()
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)
Please help. I am creating a little GUI dictionary and am trying to link the interface to a database. I can enter data (word and definition) into the database correctly but I cannot seem to look up words in the database so that the appropriate definition displays in the output box on my GUI.
Module 1 (main program):
from tkinter import *
import libraryentrySQL
import sqlite3
import os
def click():
entered_text = entry.get() #collect text from text entry box
output.delete(0.0,END) #clears text box - start clearing from 0.0 (from line 0) to END (after last character)
new_db = sqlite3.connect('dictionary.db')
c=new_db.cursor()
try:
definition = c.execute("SELECT definition FROM Dictionary WHERE word=%s", (entered_text))
except:
definition = "No word found in dictionary, try again!"
output.insert(END, definition) #this inserts the contents of variable 'definition' at the beginning (END) - because it was cleared before, END is the at the start
def clickentry(): #this function is run when the 2nd button (entry is pressed)
def definition_submitted(word, definition):
new_db = sqlite3.connect('dictionary.db')
c=new_db.cursor()
c.execute("INSERT INTO Dictionary VALUES (?, ?)", (word, definition))
new_db.commit()
new_db.close()
definition_window = libraryentrySQL.DefinitionWindow(window, definition_submitted) #this creates the object 'definition window' and passes to it 'the window variable'
#so that it can have a canvas
#and also passes the function 'definition_submitted' so that as the new word and definition are entered
#in the this object (second window) it can be passed into the function and the dictionary updated
window = Tk()
window.title("My Little Dictionary")
#Create the Label
Label(window, text="Enter the word you want defining:").grid(row=0, column=0, sticky=W)
#create entry box
entry=Entry(window, width=20, bg="light green")
entry.grid(row=1, column=0, sticky=W)
#create submit button
Button(window, text="Submit", width=5, command=click).grid(row=2, column=0, sticky=W)
#create second label
Label(window, text="\nDefinition").grid(row=3, column=0, sticky=W)
#create text box
output=Text(window, width=75, height=6, wrap=WORD, background="light green")
output.grid(row=4, column=0, sticky=W)
#create submit button to open enter new definition window
Button(window, text="Enter a New Definition", width=20, command=clickentry).grid(row=5, column=0, sticky=W)
#Create the Dictionary.db if not already present
if not os.path.isfile("dictionary.db"):
new_db = sqlite3.connect('dictionary.db')
c=new_db.cursor()
c.execute('''CREATE TABLE Dictionary
(word text,
definition text)''')
c.execute('''INSERT INTO Dictionary VALUES
('Algorithm', 'Step by step instructions to complete a task')''')
new_db.commit()
new_db.close()
window.mainloop()
module 2 (enter word and definition window):
from tkinter import *
class DefinitionWindow(Toplevel):
def __init__(self, root, click_callback):
Toplevel.__init__(self, root)
self.click_callback = click_callback
self.title("Library entry")
#Create the Label
Label(self, text="Enter the word you want to add:").grid(row=0, column=0, sticky=W)
#create entry box
self.word_entry=Entry(self, width=20, bg="light green")
self.word_entry.grid(row=1, column=0, sticky=W)
#create second label
Label(self, text="\nDefinition").grid(row=2, column=0, sticky=W)
#create entry box
self.definition_entry = Entry(self, width=50, bg="light green")
self.definition_entry.grid(row=3, column=0, sticky=W)
#create submit button
Button(self, text="Submit", width=5, command=self.clicked).grid(row=4, column=0, sticky=W)
def clicked(self):
self.click_callback(self.word_entry.get(), self.definition_entry.get()) #when this function is called (on submit button click) it takes the entered
#word and definition and assigns it to click_callback, which is an attribute of DefinitionWindow??
self.destroy() #after the word and definition are added to the call_back variable, the frame containing this instance of definition window is closed
What am I doing wrong? I know it is the "SELECT" SQL command which is not correct. Any help would be gratefully received. Thanks
This is not how SQL queries in Python work.
The execute method returns not a value but a cursor.
When nothing was found, no exception is raised, and the cursor is just empty.
Handling all exceptions blindy in the except block will hide any programming errors.
Furthermore, the parameters marker is not %s but ?.
Finally, a Python tuple with a single value must include a comma to differentiate it from a single expression:
c = new_db.cursor()
c.execute("SELECT definition FROM Dictionary WHERE word = ?", (entered_text,))
for row in c:
definition = row[0]
break
else:
definition = "No word found in dictionary, try again!"
output.insert(END, definition)
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)