[Tutor] putting accent on letters while user is typing in Entrybox (Tkinter)
The accents which i want to be automatically converted and put upon letters is circumflex and another one which shape is like the opposite of circumflex. the user types in entrybox and it searches in db which i've created before and has no problem. the words in my db have unicode characters and they are accented. the user himself can type accented characters too, but i want to make it automatically converted so the user doesn't have to have a special keyboard to write. when x is pressed after these letters (g,j,c,h,u), i want that x to be removed and replaced with a circumflex above those letters. here is the full code if needed: import sqlite3 as sqlite import tkinter as tk from tkinter import ttk #GUI Widgets class EsperantoDict: def __init__(self, master): master.title("EsperantoDict") master.iconbitmap("Esperanto.ico") master.resizable(False, False) master.configure(background='#EAFFCD') self.style = ttk.Style() self.search_var = tk.StringVar() self.search_var.trace("w", lambda name, index, mode: self.update_list()) self.style = ttk.Style() self.style.configure("TFrame", background='#EAFFCD') self.style.configure("TButton", background='#C6FF02') self.style.configure("TLabel", background='#EAFFCD') self.frame_header = ttk.Frame(master, relief=tk.FLAT) self.frame_header.config(style="TFrame") self.frame_header.pack(side=tk.TOP, padx=5, pady=5) self.logo = tk.PhotoImage(file=r'C:\EsperantoDict\eo.png') self.small_logo = self.logo.subsample(10, 10) ttk.Label(self.frame_header, image=self.small_logo).grid(row=0, column=0, stick="ne", padx=5, pady=5, rowspan=2) ttk.Label(self.frame_header, text='EsperantoDict', font=('Arial', 18, 'bold')).grid(row=0, column=1) self.frame_content = ttk.Frame(master) self.frame_content.config(style="TFrame") self.frame_content.pack() self.entry_search = ttk.Entry(self.frame_content, textvariable=self.search_var, width=30) self.entry_search.bind('', self.entry_delete) self.entry_search.bind('', self.entry_insert) self.entry_search.grid(row=0, column=0, padx=5) self.entry_search.focus() self.button_search = ttk.Button(self.frame_content, text="Search") self.photo_search = tk.PhotoImage(file=r'C:\EsperantoDict\search.png') self.small_photo_search = self.photo_search.subsample(3, 3) self.button_search.config(image=self.small_photo_search, compound=tk.LEFT, style="TButton") self.button_search.grid(row=0, column=2, columnspan=1, sticky='nw', padx=5) self.listbox = tk.Listbox(self.frame_content, height=30, width=30) self.listbox.grid(row=1, column=0, padx=5) self.scrollbar = ttk.Scrollbar(self.frame_content, orient=tk.VERTICAL, command=self.listbox.yview) self.scrollbar.grid(row=1, column=1, sticky='nsw') self.listbox.config(yscrollcommand=self.scrollbar.set) self.listbox.bind('<>', self.enter_meaning) self.textbox = tk.Text(self.frame_content, relief=tk.GROOVE, width=60, height=30, borderwidth=2) self.textbox.config(wrap='word') self.textbox.grid(row=1, column=2, sticky='w', padx=5) # SQLite self.db = sqlite.connect(r'C:\EsperantoDict\test.db') self.cur = self.db.cursor() self.cur.execute('SELECT Esperanto FROM Words') for row in self.cur: self.listbox.insert(tk.END, row) for row in range(0, self.listbox.size(), 2): self.listbox.itemconfigure(row, background="#f0f0ff") self.update_list() def update_list(self): search_term = self.search_var.get() for item in self.listbox.get(0, tk.END): if search_term.lower() in item: self.listbox.delete(0, tk.END) self.listbox.insert(tk.END, item) # SQLite def enter_meaning(self, tag): for index in self.listbox.curselection(): esperanto = self.listbox.get(index) results = self.cur.execute("SELECT English FROM Words WHERE Esperanto = ?", (esperanto)) for row in results: self.textbox.delete(1.0, tk.END) self.textbox.insert(tk.END, row) def entry_delete(self, tag): self.entry_search.delete(0, tk.END) return None def entry_insert(self, tag): self.entry_search.delete(0, tk.END) self.entry_search.insert(0, "Type to Search") return None def main(): root = tk.Tk() esperantodict = EsperantoDict(root) root.mainloop() if __name__ == '__main__': main() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] help with reading a string?
Hi, I am a beginner comp sci teacher at an all-girls high school. The girls are learning how to read from a txt file and search for a string. Here is a sample of text from the txt file: TX,F,1910,Mary,895 TX,F,1910,Ruby,314 TX,F,1910,Annie,277 TX,F,1910,Willie,260 TX,F,1910,Ruth,252 TX,F,1910,Gladys,240 TX,F,1910,Maria,223 TX,F,1910,Frances,197 TX,F,1910,Margaret,194 How do they read the number after a certain searched name and then add up the numbers after each time the name occurs? I would really like to help them with this code but I am at a loss. Thank you, Crystal ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help with reading a string?
On 16/07/18 23:28, Crystal Frommert wrote: > are learning how to read from a txt file and search for a string. > > Here is a sample of text from the txt file: > TX,F,1910,Mary,895 > TX,F,1910,Ruby,314 > TX,F,1910,Annie,277 > > How do they read the number after a certain searched name and then add up > the numbers after each time the name occurs? > > I would really like to help them with this code but I am at a loss. Which bit are you at a loss over? The general approach is: open the file for reading read each line. search the line for your target string extract the number at the end of the file add the number to the running total None of those are intrinsically difficult, so which bit are you having problems with? Have you got any code that you tried and it didn't work? That would be most helpful in determining where you need to adjust your approach. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] putting accent on letters while user is typing in Entrybox (Tkinter)
On 16/07/18 18:29, Ali M wrote: > The accents which i want to be automatically converted and put upon letters > is circumflex and another one which shape is like the opposite of > circumflex. It doesn't really matter what the shapes are provided the result is a unicode character. You just want to substitute one character for another > def update_list(self): > search_term = self.search_var.get() You have the string of characters here so you need to scan the string and detect the special character pairs, do a look up and swap in the unicode chars you want. I strongly suggest you do that in a method def edit_input(self, input) for index,char in enumerate(input) return result You can then proceed to process the new string as required. search_term = self.edit_input(search_term) > for item in self.listbox.get(0, tk.END): > if search_term.lower() in item: > self.listbox.delete(0, tk.END) > self.listbox.insert(tk.END, item) > -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help with reading a string?
Hi Crystal, and welcome! My response is further below, after your question. On Mon, Jul 16, 2018 at 05:28:37PM -0500, Crystal Frommert wrote: > Hi, I am a beginner comp sci teacher at an all-girls high school. The girls > are learning how to read from a txt file and search for a string. > > Here is a sample of text from the txt file: > TX,F,1910,Mary,895 > TX,F,1910,Ruby,314 > TX,F,1910,Annie,277 > TX,F,1910,Willie,260 > TX,F,1910,Ruth,252 > TX,F,1910,Gladys,240 > TX,F,1910,Maria,223 > TX,F,1910,Frances,197 > TX,F,1910,Margaret,194 > > How do they read the number after a certain searched name and then add up > the numbers after each time the name occurs? Which version of Python are you using? The answer may depend on the exact version, but something like this ought to be good: # Code below is not tested, please forgive any bugs when you try it... # Process the lines of some text file. total = 0 with open("path to the file.txt", "r") as f: # Read the file line-by-line. for line in f: # Split each record into five fields. # Please pick more meaningful field names for a, b, c! a, b, c, name, score = line.split(",") # Choose only records for a specific person. # Here I hard-code the person's name, in practice # you ought to use a variable. if name == "Sarah": # Convert the score from a string to an int, # and add it to total. total = total + int(score) # Dropping back to the extreme left automatically closes # the file. print(total) Remember that in Python, indentation is not optional. Some extra features: * You can re-write the line "total = total + int(score)" using the more compact notation "total += int(score)" * Instead of the "with open(...) as f", you can do it like this: f = open("path to the file.txt") # the "r" parameter is optional for line in f: # copy for loop from above to here # (don't forget to reduce the indentation by one level) ... # Explicitly close the file. f.close()# A common error is to forget the parentheses! # And finally print the total. print(total) Hope this helps, and please don't hesitate to ask if you have any questions. Keep your replies on the mailing list so others may contribute answers and learn from the responses. Regards, Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor