Python Forum
cant save data to text file.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
cant save data to text file.
#1
hi
Im trying to save all the generated numbers from my generator  to a text file with file save dialog but it only saves the last line and the text file does not have *.txt for some reason. Could any one help me out?

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')


    def funktion(self):
        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:
            #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):
        
        #file = filedialog.asksaveasfilename(initialdir = "/",title = "Select file",filetypes = (("txt files","*.txt"),("all files","*.*")))
        file = filedialog.asksaveasfile(mode='wt', filetypes = (("txt files","*.txt"),("all files","*.*")))
        value4 = (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()
Reply


Messages In This Thread
cant save data to text file. - by darktitan - Oct-26-2017, 05:54 PM
RE: cant save data to text file. - by Larz60+ - Oct-26-2017, 09:30 PM
RE: cant save data to text file. - by darktitan - Oct-27-2017, 03:42 AM
RE: cant save data to text file. - by Larz60+ - Oct-27-2017, 08:26 AM
RE: cant save data to text file. - by darktitan - Oct-27-2017, 03:11 PM
RE: cant save data to text file. - by wavic - Oct-27-2017, 03:20 PM
RE: cant save data to text file. - by Larz60+ - Oct-27-2017, 04:33 PM
RE: cant save data to text file. - by darktitan - Oct-27-2017, 04:46 PM
RE: cant save data to text file. - by Larz60+ - Oct-27-2017, 04:49 PM
RE: cant save data to text file. - by darktitan - Oct-27-2017, 05:01 PM
RE: cant save data to text file. - by Larz60+ - Oct-27-2017, 08:37 PM
RE: cant save data to text file. - by darktitan - Oct-28-2017, 02:55 AM
RE: cant save data to text file. - by Larz60+ - Oct-28-2017, 04:24 AM
RE: cant save data to text file. - by darktitan - Oct-28-2017, 06:59 AM
RE: cant save data to text file. - by Larz60+ - Oct-28-2017, 10:35 AM
RE: cant save data to text file. - by darktitan - Oct-28-2017, 12:22 PM
RE: cant save data to text file. - by Larz60+ - Oct-28-2017, 04:56 PM
RE: cant save data to text file. - by darktitan - Oct-28-2017, 05:56 PM
RE: cant save data to text file. - by Larz60+ - Oct-29-2017, 12:53 AM
RE: cant save data to text file. - by Larz60+ - Oct-29-2017, 01:55 AM
RE: cant save data to text file. - by darktitan - Oct-29-2017, 03:05 AM
RE: cant save data to text file. - by Larz60+ - Oct-29-2017, 03:20 AM
RE: cant save data to text file. - by darktitan - Oct-29-2017, 03:23 AM
RE: cant save data to text file. - by Larz60+ - Oct-29-2017, 03:27 AM
RE: cant save data to text file. - by darktitan - Oct-29-2017, 03:34 AM
RE: cant save data to text file. - by Larz60+ - Oct-29-2017, 03:48 AM
RE: cant save data to text file. - by darktitan - Oct-29-2017, 03:57 AM
RE: cant save data to text file. - by Larz60+ - Oct-29-2017, 05:02 AM
RE: cant save data to text file. - by darktitan - Oct-29-2017, 06:31 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Open/save file on Android frohr 0 337 Jan-24-2024, 06:28 PM
Last Post: frohr
  how to save to multiple locations during save cubangt 1 560 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  save values permanently in python (perhaps not in a text file)? flash77 8 1,249 Jul-07-2023, 05:44 PM
Last Post: flash77
  Save and Close Excel File avd88 0 3,083 Feb-20-2023, 07:19 PM
Last Post: avd88
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,132 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Save multiple Parts of Bytearray to File ? lastyle 1 962 Dec-10-2022, 08:09 AM
Last Post: Gribouillis
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,695 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 7,017 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  Save data frame to .csv df.to.csv() mcva 1 1,545 Feb-03-2022, 07:05 PM
Last Post: mcva
Sad Want to Save Print output in csv file Rasedul 5 10,993 Jan-11-2022, 07:04 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020