Come fare un'applicazione di elaborazione testi con python
I dizionari saranno molto utili. Questo è Python 3.7 ma funzionerà bene in 2.7 cambiando tkinter in Tkinter e perdendo il ttk. Questo esempio è di definizioni ma potete farlo convertire in quello che volete.
- from tkinter import *
- from tkinter import ttk
- words = {'acre': 'An English measurement for a Lot of land',
- 'aurora': 'A bright hue over the northern hemisphere',
- 'absolom': 'An Israelite King from the Bible',
- 'acrobat': 'Someone who performs dangerous stunts',
- 'aircraft': 'Man made things that fly',
- 'aristotle': 'Some old pencil pusher',
- 'ape': 'A bully',
- 'abba': 'A 1980 glamour band'}
- def calculate(*args):
- prova:
- value = int(word.get())#CHECKS IF Word is a number !
- word_out.set('Word must not be a number !')
- except ValueError:
- try:
- value = str(word.get())
- value = words.get(value)
- if value == None:# CHECKS IF WORD NOT IN DICTIONARY
- word_out.set('Word not listed !')
- else:
- word_out.set(value+'.')
- except ValueError:
- pass
- root=Tk()
- width = root.winfo_screenwidth()# USE if using WINDOWS
- height = root.winfo_screenheight()
- #root.geometry(str(width) + "x" + str(height))# WINDOWS
- root.geometry('900x500')
- root.title("Word Processor")
- mainframe = ttk.Frame(root, padding="13 13 12 12")
- mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
- mainframe.columnconfigure(0, weight=1)
- mainframe.rowconfigure(0, weight=1)
- word = StringVar()
- word_out = StringVar()
- word_entry = ttk.Entry(mainframe, width=12, textvariable = word)
- word_entry.grid(column=1, row=0, sticky=(W, E))
- ttk.Label(mainframe, textvariable = word_out).grid(column=1, row=2, sticky=(W, E))
- ttk.Button(mainframe, text="Calculate", command=calculate).grid(column=2, row=0, sticky=W)
- ttk.Label(mainframe, text="Enter Word").grid(column=0, row=0, sticky=W)
- ttk.Label(mainframe, text="Definition:").grid(column=0, row=2, sticky=E)
- #ttk.Label(mainframe, text="Definition").grid(column=3, row=2, sticky=W)
- for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
- word_entry.focus()
- root.bind('', calculate)
- root.mainloop()
If your dictionaries get too large place them into separate .py files and import them as filename.function() and inside the function() place the dictionary and return it as named.
- inside filename
- def funtioname():
- words = {'words':'values',etc}
- return words
- --------------------------------------------
- imp tkinter etc. then
- import filename
- inside calculate add and replace-->
- value = str(word.get())
- lib = filename.functionname()
- value = lib.get(value)
- do the app