'Python: Looking for ways to improve code on note taking script
I have only dabbled in coding before. Made this notepad programme to use while at work taking calls. Any advise on how this could be improved would be appreciated. I know there has to be a better way of doing the functions. I tried making it so the text it is pasting in to the text field was dealt with by function arguments, watched a few videos and read the documentation but couldn't get it to work. Any and all advice appreciated. thank you.
from tkinter import *
window=Tk()
restrict_time = '''Restricted time:
Restriction reason:
'''
booked_with_meeting_same = '''Please go straight to property meeting
name:
Contact number:
No. OF PETS:
Loft access:
'''
booked_with_meeting_dif = '''Please go straight to property meeting
Name:
Contact number:
No. OF PETS:
Loft access:
Booked with -
Name:
Contact number:
company name:
'''
key_collection = '''Restricted time:
Restriction reason:
Please collect keys from
company name:
address:
Booked with -
Name:
Contact number:
'''
key_in_safe = '''Key safe at property -
Location of key safe:
code for key safe:
Where to return keys:
Booked with -
Name:
Number:
Company name:
'''
new_build = '''New build:
RESTRICTED TIME:
INSTRUCTION:
SAT NAV ADDRESS:
DEVELOPMENT NAME:
BUILDER’S NAME:
NAME & NUMBER OF PERSON BOOKED WITH:
DIF AVAILABLE:
PLOT No:
NAME & NUMBER OF PERSON IS MEETING:
'''
bottomframe = Frame(window)
bottomframe.pack( side = BOTTOM )
text_box = Text(
height=20,
width=70
)
text_box.pack(expand=True)
def save_text():
global text_box
text_file = open("testfile.txt", "a")
text_file.write(text_box.get(1.0, END))
text_file.close()
def bwmws():
global text_box
text_file = open("testfile.txt", "a")
text_file.write(text_box.get(1.0, END))
text_file.close()
text_box.delete(1.0,END)
text_box.insert('end', booked_with_meeting_same)
def bwmwd():
global text_box
text_file = open("testfile.txt", "a")
text_file.write(text_box.get(1.0, END))
text_file.close()
text_box.delete(1.0,END)
text_box.insert('end', booked_with_meeting_dif)
def kc():
global text_box
text_file = open("testfile.txt", "a")
text_file.write(text_box.get(1.0, END))
text_file.close()
text_box.delete(1.0,END)
text_box.insert('end', key_collection)
def kis():
global text_box
text_file = open("testfile.txt", "a")
text_file.write(text_box.get(1.0, END))
text_file.close()
text_box.delete(1.0,END)
text_box.insert('end', key_in_safe)
def nb():
global text_box
text_file = open("testfile.txt", "a")
text_file.write(text_box.get(1.0, END))
text_file.close()
text_box.delete(1.0,END)
text_box.insert('end', new_build)
Button1= Button(bottomframe, text= "b/W & m/w same", command=lambda: bwmws())
Button2= Button(bottomframe, text= "b/w & m/w different", command=lambda: bwmwd())
Button3= Button(bottomframe, text= "Key Collection", command=lambda: kc())
Button4= Button(bottomframe, text= "Key In Safe", command=lambda: kis())
Button5= Button(bottomframe, text= "New Build", command=lambda: nb())
Button6= Button(bottomframe, text= "Restrict Time", command=lambda: text_box.insert('1.0', restrict_time))
Button7= Button(bottomframe, text='save text', command= lambda: save_text())
Button1.pack(side = LEFT)
Button2.pack(side = LEFT)
Button3.pack(side = LEFT)
Button4.pack(side = LEFT)
Button5.pack(side = LEFT)
Button6.pack(side = LEFT)
Button7.pack(side = LEFT)
window.title('Note template')
window.geometry("600x400+20+40")
window.mainloop()
Solution 1:[1]
One code smell you can remove is not using lambda when adding a command, as it serves no real purpose.
command= lambda: save_text()
I'm guessing you are using lambda so the the function won't be called when executing the script. However the Button widget does this by default as long as you point to the function object, without calling it.
command=save_text
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | LBJ |
