Oct-29-2017, 03:05 AM
(Oct-29-2017, 01:55 AM)Larz60+ Wrote: [ -> ]Ok, this code works:
from tkinter import * import tkinter as tk from tkinter import filedialog import sys, os import io class StatusBar(Frame): def __init__(self, master): Frame.__init__(self, master) # self.file = io.open('märkning.txt', 'wt', encoding='utf8') self.file_open = False self.file = None def funktion(self): print('getting funked up') value1 = (a1.get()) value2 = (a2.get()) value3 = (a3.get()) # value4 = (a4.get()) for parts in range(value3): print('{}-{}-{}-{}\n'.format(value1, value2, parts + 1, parts + 1)) # self.file.writelines('{}-{}-{}-{}\n'.format(value1, value2, parts+1, parts+1)) a4.set('{}-{}-{}-{}\n'.format(value1, value2, parts + 1, parts + 1)) # Vad ska programet göra när den stängs av. def On_exit(self): try: if self.file_open: print('closing') # stänger ner data filer och sparar data. self.file.close() # stäng av prgramet self.master.destroy() except: self.master.destroy() # Koden är en del av entry's endast nummer restriktion def validate(self, action, index, value_if_allowed, prior_value, text, validation_type, trigger_type, widget_name): if text in '0123456789.-+': try: float(value_if_allowed) return True except ValueError: return False else: return False def file_save(self): if not self.file_open: print('opening') # file = filedialog.asksaveasfilename(initialdir = "/",title = "Select file",filetypes = (("txt files","*.txt"),("all files","*.*"))) self.file = filedialog.asksaveasfile(mode='a+', filetypes=(("txt files", "*.txt"), ("all files", "*.*"))) self.file_open = True value4 = (a4.get()) print('writing') self.file.write(a4.get()) # if file: # file.writelines(value4) # file.close() if __name__ == "__main__": root = tk.Tk() status = StatusBar(root) root.protocol("WM_DELETE_WINDOW", status.On_exit) Frame = tk.Frame(root, borderwidth=10) Frame.grid(column=0, row=0, sticky=(N, W, E, S)) # Koden är en del av entry's endast nummer restriktion vcmd = (Frame.register(status.validate), '%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W') a1 = StringVar() a2 = StringVar() a3 = IntVar() a4 = StringVar() text1 = Label(Frame, text="Kabelnamn:", font=("Helvetica", 12, "bold")).grid(column=0, row=0, sticky=(W), columnspan=2) Kabelnamn_entry = tk.Entry(Frame, font=('Helvetica', 12, 'bold'), textvariable=a1) Kabelnamn_entry.grid(column=0, row=1, sticky=(W, E), columnspan=2) Kabelnamn_entry.delete(0, END) text2 = Label(Frame, text="Kabelnummer:", font=("Helvetica", 12, "bold")).grid(column=0, row=2, sticky=(W), columnspan=2) Kabelnummer_entry = tk.Entry(Frame, font=('Helvetica', 12, 'bold'), textvariable=a2) Kabelnummer_entry.grid(column=0, row=3, sticky=(W, E), columnspan=2) Kabelnummer_entry.delete(0, END) text3 = Label(Frame, text="Parter:", font=("Helvetica", 12, "bold")).grid(column=0, row=4, sticky=(W), columnspan=2) part_entry = tk.Entry(Frame, font=('Helvetica', 12, 'bold'), textvariable=a3, validate='key', validatecommand=vcmd) part_entry.grid(column=0, row=5, sticky=(W, E), columnspan=2) part_entry.delete(0, END) button1 = tk.Button(Frame, text="Make", command=status.funktion, width=16) button1.grid(column=0, row=6, sticky=(W, E), columnspan=4) button2 = tk.Button(Frame, text="Spara", command=status.file_save, width=16) button2.grid(column=0, row=7, sticky=(W, E), columnspan=4) root.update() root.resizable(width=False, height=False) root.mainloop()The file is opened as a+ (so it picks up from the last session),
if you want new file for each session, change the mode to 'w'.
You don't need the t, as text is the default mode.
I added a file_open switch, and only open the file once, and then check is the flag is set at the and, and if so, close.
I changed the save routine a bit, removed the close, and write from a4 directly.
Hope this is what you wanted.
not really it still saves the last line which was my problem all along since the beginning. What i have been trying to do is to make it save all the lines it generet.
for example i generete this list
Quote:save-this-1-1
save-this-2-2
save-this-3-3
save-this-4-4
save-this-5-5
save-this-6-6
save-this-7-7
save-this-8-8
save-this-9-9
save-this-10-10
then i want to save all this lines and no just one line.